Skip to content

Run prettier #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
tips
.DS_Store
.DS_Store

node_modules/
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.formatOnSave": false,
"[javascript]": {
"editor.formatOnSave": true
}
}
43 changes: 24 additions & 19 deletions LeetcodeProblems/Algorithms/3Sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,33 @@ A solution set is:
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
var threeSum = function (nums) {
var ret = [];
nums.sort(function(a, b) { return a - b });
for(var i = 0; i < nums.length; i++) {
if(i === 0 || i > 0 && nums[i] !== nums[i - 1]) {
var left = i + 1;
var right = nums.length - 1;
while(left < right) {
const sum = nums[left] + nums[right] + nums[i];
if(left > i + 1 && nums[left] === nums[left - 1] || sum < 0) {
left++
} else if(right < nums.length - 1 && nums[right] === nums[right + 1] || sum > 0) {
right--;
} else if(sum === 0) {
ret.push([nums[left], nums[right], nums[i]]);
left++;
right--;
}
}
nums.sort(function (a, b) {
return a - b;
});
for (var i = 0; i < nums.length; i++) {
if (i === 0 || (i > 0 && nums[i] !== nums[i - 1])) {
var left = i + 1;
var right = nums.length - 1;
while (left < right) {
const sum = nums[left] + nums[right] + nums[i];
if ((left > i + 1 && nums[left] === nums[left - 1]) || sum < 0) {
left++;
} else if (
(right < nums.length - 1 && nums[right] === nums[right + 1]) ||
sum > 0
) {
right--;
} else if (sum === 0) {
ret.push([nums[left], nums[right], nums[i]]);
left++;
right--;
}
}
}
}

return ret;
};

Expand Down
31 changes: 14 additions & 17 deletions LeetcodeProblems/Algorithms/Add_Two_Numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,41 @@ Explanation: 342 + 465 = 807.
* this.next = null;
* }
*/
var ListNode = require('../../UtilsClasses/ListNode').ListNode;
var ListNode = require("../../UtilsClasses/ListNode").ListNode;

/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/

var addTwoNumbers = function(l1, l2) {
if(l1 === null)
return (l2 === null) ? new ListNode(0) : l2;
else if(l2 === null)
return l1;

var addTwoNumbers = function (l1, l2) {
if (l1 === null) return l2 === null ? new ListNode(0) : l2;
else if (l2 === null) return l1;

var elem = l1.val + l2.val;
var number = new ListNode(elem % 10);
var carry = (elem >= 10) ? 1 : 0;
var carry = elem >= 10 ? 1 : 0;
l1 = l1.next;
l2 = l2.next;

const head = number;
while(l1 !== null || l2 !== null) {
while (l1 !== null || l2 !== null) {
var elem = carry;
if(l1 !== null) {
if (l1 !== null) {
elem += l1.val;
l1 = l1.next;
}
if(l2 !== null) {
if (l2 !== null) {
elem += l2.val;
l2 = l2.next;
}
number.next = new ListNode((elem % 10));

number.next = new ListNode(elem % 10);
number = number.next;
carry = (elem >= 10) ? 1 : 0;
carry = elem >= 10 ? 1 : 0;
}
if(carry === 1)
number.next = new ListNode(1);
if (carry === 1) number.next = new ListNode(1);
return head;
};

Expand Down
30 changes: 15 additions & 15 deletions LeetcodeProblems/Algorithms/Award_Budget_Cuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,39 @@ Constraints:
[output] double
*/
var cutAwardBadges = function(nums, newBadge) {
var cutAwardBadges = function (nums, newBadge) {
var currentBadge = 0;
for(var i = 0; i < nums.length; i++)
currentBadge += nums[i];
for (var i = 0; i < nums.length; i++) currentBadge += nums[i];

if(currentBadge < newBadge)
return;
if (currentBadge < newBadge) return;

const cap = findCap(nums, currentBadge, newBadge);

var iter = 0;
while(iter >= 0 && nums[iter] > cap) {
while (iter >= 0 && nums[iter] > cap) {
nums[iter] = cap;
iter++;
}

return nums;
}
};

var findCap = function(nums, currentBadge, newBadge) {
nums.sort(function(a, b) { return b - a });
if(nums[nums.length - 1] * nums.length > newBadge)
var findCap = function (nums, currentBadge, newBadge) {
nums.sort(function (a, b) {
return b - a;
});
if (nums[nums.length - 1] * nums.length > newBadge)
return newBadge / nums.length;

var diff = currentBadge - newBadge;
var iter = 0;
while(iter < nums.length - 1 && diff > 0) {
const substraction = nums[iter] - nums[iter + 1]
diff -= (iter + 1) * substraction;
while (iter < nums.length - 1 && diff > 0) {
const substraction = nums[iter] - nums[iter + 1];
diff -= (iter + 1) * substraction;
iter++;
}

return nums[iter] + (-diff) / iter;
}
return nums[iter] + -diff / iter;
};

module.exports.cutAwardBadges = cutAwardBadges;
72 changes: 33 additions & 39 deletions LeetcodeProblems/Algorithms/Backspace_String_Compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,71 +37,65 @@ Can you solve it in O(N) time and O(1) space?
* @param {string} T
* @return {boolean}
*/
var backspaceCompare = function(S, T) {
var backspaceCompare = function (S, T) {
var iterS = S.length - 1;
var iterT = T.length - 1;
while(iterS >= 0 || iterT >= 0) {
if(iterS >= 0 && S.charAt(iterS) === "#") {

while (iterS >= 0 || iterT >= 0) {
if (iterS >= 0 && S.charAt(iterS) === "#") {
var countBack = 0;
while(iterS >= 0 && (countBack > 0 || S[iterS] === "#")) {
if(iterS >= 0 && S[iterS] === "#") {
while (iterS >= 0 && (countBack > 0 || S[iterS] === "#")) {
if (iterS >= 0 && S[iterS] === "#") {
countBack++;
} else {
countBack--;
}

iterS--;
}
} else if(iterT >= 0 && T.charAt(iterT) === "#") {
var countBack = 0;
while(iterT >= 0 && (countBack > 0 || T[iterT] === "#")) {
if(iterT >= 0 && T[iterT] === "#") {
countBack++;
} else {
countBack--;
} else if (iterT >= 0 && T.charAt(iterT) === "#") {
var countBack = 0;
while (iterT >= 0 && (countBack > 0 || T[iterT] === "#")) {
if (iterT >= 0 && T[iterT] === "#") {
countBack++;
} else {
countBack--;
}

iterT--;
}

iterT--;
}
} else {
if(iterS < 0 || iterT < 0 || S.charAt(iterS) !== T.charAt(iterT))
if (iterS < 0 || iterT < 0 || S.charAt(iterS) !== T.charAt(iterT))
return false;

iterS--;
iterT--;
}
}
return iterS < 0 && iterT < 0;

return iterS < 0 && iterT < 0;
};

// Naive Aproach
var backspaceCompare2 = function(S, T) {
// Naive Aproach
var backspaceCompare2 = function (S, T) {
var stackS = [];
for(var i = 0; i < S.length; i++) {
if(S.charAt(i) === "#")
stackS.shift();
else
stackS.unshift(S.charAt(i));
for (var i = 0; i < S.length; i++) {
if (S.charAt(i) === "#") stackS.shift();
else stackS.unshift(S.charAt(i));
}

var stackT = [];
for(var i = 0; i < T.length; i++) {
if(T.charAt(i) === "#")
stackT.shift();
else
stackT.unshift(T.charAt(i));
for (var i = 0; i < T.length; i++) {
if (T.charAt(i) === "#") stackT.shift();
else stackT.unshift(T.charAt(i));
}
while(stackS.length > 0 && stackT.length > 0) {

while (stackS.length > 0 && stackT.length > 0) {
var elemS = stackS.shift();
var elemT = stackT.shift();
if(elemS !== elemT)
return false;

if (elemS !== elemT) return false;
}

return stackS.length === 0 && stackT.length === 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ Explanation: In this case, no transaction is done, i.e. max profit = 0.
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
var maxProfit = function (prices) {
var profit = 0;
var iter = 0;
while(iter < prices.length) {
while(iter < prices.length - 1 && prices[iter] > prices[iter + 1]) {

while (iter < prices.length) {
while (iter < prices.length - 1 && prices[iter] > prices[iter + 1]) {
iter++;
}
const buy = prices[iter];
iter++;
while(iter < prices.length - 1 && prices[iter] < prices[iter + 1]) {
while (iter < prices.length - 1 && prices[iter] < prices[iter + 1]) {
iter++;
}
if(iter < prices.length) {

if (iter < prices.length) {
const sell = prices[iter];
profit = profit + sell - buy;
}
}

return profit;
};

module.exports.maxProfit = maxProfit;
module.exports.maxProfit = maxProfit;
13 changes: 6 additions & 7 deletions LeetcodeProblems/Algorithms/Binary_Gap.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ There aren't any consecutive pairs of 1's in the binary representation of 8, so
* @param {number} N
* @return {number}
*/
var binaryGap = function(N) {
var binaryGap = function (N) {
var maxDist = 0;
var currentDist = 0;
while(N > 0) {
while (N > 0) {
const bit = N % 2;
N >>= 1;
if(bit === 1) {
if (bit === 1) {
currentDist = 1;
while(N > 0 && N % 2 === 0 ) {
while (N > 0 && N % 2 === 0) {
currentDist++;
N >>= 1;
N >>= 1;
}
if(N !== 0 && currentDist > maxDist)
maxDist = currentDist;
if (N !== 0 && currentDist > maxDist) maxDist = currentDist;
}
}
return maxDist;
Expand Down
Loading