Skip to content

EsLint and Tests fix #67

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 1 commit into from
Oct 9, 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
6 changes: 3 additions & 3 deletions LeetcodeProblems/Algorithms/2Sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var twoSum = function (nums, target) {
let map = {};
for (let i = 0; i < nums.length; i++) {
const sum = target - nums[i];
if (map[parseInt(sum)] != 0) {
if (sum in map) {
return [map[sum], i];
} else {
map[nums[i]] = i;
Expand All @@ -43,8 +43,8 @@ var twoSum = function (nums, target) {
//Another method
var twoSum2 = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; i++) {
if (nums[1] + nums[j] === target) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
Expand Down
28 changes: 14 additions & 14 deletions LeetcodeProblems/Algorithms/3SumClosest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ Constraints:
* @param {number} target
* @return {number}
*/
var threeSumClosest = function(nums, target) {
var threeSumClosest = function(nums, target) {
let mid = 1;
let right = nums.length - 1;
let currentSum = nums[0] + nums[mid] + nums[right];
let closest = currentSum;

nums.sort(function(a,b) {return a - b})
nums.sort(function(a,b) {return a - b;});

for(var left = 0 ; left < nums.length - 1; left++) {
mid = left + 1;
right = nums.length - 1;
mid = left + 1;
right = nums.length - 1;

while(mid < right) {
currentSum = nums[left] + nums[mid] + nums[right];
while(mid < right) {
currentSum = nums[left] + nums[mid] + nums[right];

if(Math.abs(target - currentSum) < Math.abs(target - closest)) {
closest = currentSum;
}
if(Math.abs(target - currentSum) < Math.abs(target - closest)) {
closest = currentSum;
}

if(currentSum > target) {
right--;
} else {
mid++;
}
if(currentSum > target) {
right--;
} else {
mid++;
}
}
}

return closest;
Expand Down
16 changes: 8 additions & 8 deletions LeetcodeProblems/Algorithms/Container_With_Most_Water.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ Output: 1
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
var maxArea = function(height) {
let left = 0;
let right = height.length - 1;
let maxArea = calculateArea(left, right, height);

while(left < right) {
if(height[left] < height[right]) {
left++
} else {
right--;
}
maxArea = Math.max(maxArea, calculateArea(left, right, height))
if(height[left] < height[right]) {
left++;
} else {
right--;
}
maxArea = Math.max(maxArea, calculateArea(left, right, height));
}
return maxArea;
};
Expand All @@ -44,6 +44,6 @@ var calculateArea = function(x, y, height) {
let minHeight = height[x] > height[y] ? height[y] : height[x];
let width = y -x;
return (width * minHeight);
}
};

module.exports.maxArea = maxArea;
54 changes: 27 additions & 27 deletions LeetcodeProblems/Algorithms/Find_Anagrams.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,51 @@ The substring with start index = 2 is "ab", which is an anagram of "ab".
* @param {string} p
* @return {number[]}
*/
var findAnagrams = function(s, p) {
if(s.length < p.length) { return [] }
var findAnagrams = function(s, p) {
if(s.length < p.length) { return []; }

let start = 0;
let end = p.length - 1;
let hashBuild = {};
let countLeft = p.length;
let anagrams = []
let anagrams = [];

for(let e = 0; e < p.length; e++) {
hashBuild[p[e]] = hashBuild[p[e]] !== undefined ? hashBuild[p[e]] + 1 : 1;
hashBuild[p[e]] = hashBuild[p[e]] !== undefined ? hashBuild[p[e]] + 1 : 1;
}

for(let i = start; i < end; i++) {
if(hashBuild[s[i]] !== undefined) {
hashBuild[s[i]] = hashBuild[s[i]] - 1;
if(hashBuild[s[i]] >= 0) {
countLeft--;
}
if(hashBuild[s[i]] !== undefined) {
hashBuild[s[i]] = hashBuild[s[i]] - 1;
if(hashBuild[s[i]] >= 0) {
countLeft--;
}
}
}

while(end < s.length) {
// check left
if(hashBuild[s[end]] !== undefined) {
hashBuild[s[end]] = hashBuild[s[end]] - 1;
if(hashBuild[s[end]] >= 0) {
countLeft--;
}
if(countLeft == 0) {
anagrams.push(start);
}
// check left
if(hashBuild[s[end]] !== undefined) {
hashBuild[s[end]] = hashBuild[s[end]] - 1;
if(hashBuild[s[end]] >= 0) {
countLeft--;
}
if(countLeft == 0) {
anagrams.push(start);
}
}

// check right
if(hashBuild[s[start]] !== undefined) {
hashBuild[s[start]] = hashBuild[s[start]] + 1;
if(hashBuild[s[start]] >= 1) {
countLeft++;
}
// check right
if(hashBuild[s[start]] !== undefined) {
hashBuild[s[start]] = hashBuild[s[start]] + 1;
if(hashBuild[s[start]] >= 1) {
countLeft++;
}
}

// slide window
end++;
start++;
// slide window
end++;
start++;
}

return anagrams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Note that even though the subarrays have the same content, the two subarrays are
* @param {number[]} nums
* @return {boolean}
*/
var findSubarrays = function (nums) {
var findSubarrays = function (nums) {
const sumsSeen = new Set();

for (let i = 0; i < nums.length - 1; i++) {
Expand Down
16 changes: 8 additions & 8 deletions LeetcodeProblems/Algorithms/Happy_Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ Output: false
* @return {boolean}
*/
var isHappy = function(n) {
return checkHappyNumber(n);
return checkHappyNumber(n);
};

function checkHappyNumber(n){
strNumber = n.toString();
splitNumber = strNumber.split("");
if(splitNumber.length <= 1){
return (n <= 1)? true:false;
}
const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0);
return checkHappyNumber(digit)
let strNumber = n.toString();
let splitNumber = strNumber.split("");
if(splitNumber.length <= 1){
return (n <= 1)? true:false;
}
const digit = splitNumber.reduce((a,b)=> parseInt(a) + Math.pow(parseInt(b),2),0);
return checkHappyNumber(digit);
}

module.exports.isHappy = isHappy;
8 changes: 4 additions & 4 deletions LeetcodeProblems/Algorithms/Longest_Common_Prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ Explanation: There is no common prefix among the input strings.
* @param {string[]} strs
* @return {string}
*/
var longestCommonPrefix = function(strs) {
var longestCommonPrefix = function(strs) {
if(strs.length === 0) return "";

return strs.reduce((result, curr)=>{
let i = 0;
while(result[i] && curr[i] && result[i] === curr[i]) i++;
return result.slice(0, i);
let i = 0;
while(result[i] && curr[i] && result[i] === curr[i]) i++;
return result.slice(0, i);
});
};

Expand Down
12 changes: 6 additions & 6 deletions LeetcodeProblems/Algorithms/Longest_Substring.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ s consists of English letters, digits, symbols and spaces.
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
if(s.length == 0) { return 0 }
var lengthOfLongestSubstring = function(s) {
if(s.length == 0) { return 0; }

var repeatedChars = new Set();
var maxLength = 1;
Expand All @@ -45,15 +45,15 @@ s consists of English letters, digits, symbols and spaces.
while(end + 1 < s.length && start < s.length) {
if(repeatedChars.has(s.charAt(end + 1))) {
if(repeatedChars.has(s.charAt(start))) {
currentMaxLength--;
repeatedChars.delete(s.charAt(start))
currentMaxLength--;
repeatedChars.delete(s.charAt(start));
}
start++;
start++;
} else {
repeatedChars.add(s.charAt(end + 1));
currentMaxLength++;
if(currentMaxLength > maxLength) {
maxLength = currentMaxLength;
maxLength = currentMaxLength;
}
end++;
}
Expand Down
22 changes: 11 additions & 11 deletions LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ var longestOnes = function(nums, k) {
let end = 0;
let maxWindow = 0;
while(start < nums.length && end < nums.length) {
if(k > 0 || nums[end] == 1) {
if(nums[end] == 0) { k--; }
maxWindow = Math.max(maxWindow, end - start + 1);
end++;
} else { // k = 0 and nums[end] == 0
while(k == 0 && start < nums.length) {
if(nums[start] == 0) {
k++;
}
start++;
}
if(k > 0 || nums[end] == 1) {
if(nums[end] == 0) { k--; }
maxWindow = Math.max(maxWindow, end - start + 1);
end++;
} else { // k = 0 and nums[end] == 0
while(k == 0 && start < nums.length) {
if(nums[start] == 0) {
k++;
}
start++;
}
}
}

return maxWindow;
Expand Down
18 changes: 10 additions & 8 deletions LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ Explanation: There is only one hourglass in the matrix, with the sum: 1 + 2 + 3
* @param {number[][]} grid
* @return {number}
*/
var maxSum = function(grid) {
var maxSum = function(grid) {
const m = grid.length;
const n = grid[0].length;
if(m<3 || n < 3) {
return 0;
return 0;
}
let max = 0;
for(let i = 0; i<m-2; i++)
for(let j = 0; j<n-2;j++)
{
let cur = grid[i][j] + grid[i][j+1] + grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2];
max = Math.max(cur, max);
}
for(let j = 0; j<n-2;j++)
{
let cur = grid[i][j] + grid[i][j+1] + grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2];
max = Math.max(cur, max);
}
return max;
};
};

module.exports.maxSum = maxSum;
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
* @param {number[]} nums
* @return {number}
*/
var minPairSum = function(nums) {
var minPairSum = function(nums) {
nums.sort((a, b) => a-b);
let i = 0, j = nums.length - 1;
let max = -Infinity;
while (i < j) {
max = Math.max(max, nums[i++] + nums[j--]);
max = Math.max(max, nums[i++] + nums[j--]);
}
return max;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ var minAddToMakeValid = function(s) {
var extraParClosing = 0;

for(let i = 0; i < s.length; i++) {
if(s.charAt(i) == "(") {
opening++;
} else if(s.charAt(i) == ")") {
if(opening == 0) {
extraParClosing++;
} else {
opening--;;
}
}
if(s.charAt(i) == "(") {
opening++;
} else if(s.charAt(i) == ")") {
if(opening == 0) {
extraParClosing++;
} else {
opening--;
}
}
}
return extraParClosing + opening;
};
Expand All @@ -52,18 +52,19 @@ var minAddToMakeValidUsingQueue = function(s) {
var extraParClosing = 0;

for(let i = 0; i < s.length; i++) {
if(s.charAt(i) == "(") {
queue.push(s.charAt(i))
} else if(s.charAt(i) == ")") {
if(queue.length > 0) {
queue.pop();
} else {
extraParClosing++;
}
}
if(s.charAt(i) == "(") {
queue.push(s.charAt(i));
} else if(s.charAt(i) == ")") {
if(queue.length > 0) {
queue.pop();
} else {
extraParClosing++;
}
}
}

return extraParClosing + queue.length;
};

module.exports.minAddToMakeValid = minAddToMakeValid;
module.exports.minAddToMakeValid = minAddToMakeValid;
module.exports.minAddToMakeValidUsingQueue = minAddToMakeValidUsingQueue;
Loading