From 72e95a8114ebcbeed25b140880a8798830dc390d Mon Sep 17 00:00:00 2001 From: hot9cups Date: Sat, 8 Oct 2022 14:22:54 +0530 Subject: [PATCH 01/22] Added Minimize Maximum Pair Sum in Array - Added Minimize Maximum Pair Sum in Array and corresponding tests. --- .../Minimize_Maximum_Pair_Sum_In_Array.js | 46 +++++++++++++++++++ ...Minimize_Maximum_Pair_Sum_In_Array_Test.js | 10 ++++ README.md | 1 + 3 files changed, 57 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js create mode 100644 LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js diff --git a/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js b/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js new file mode 100644 index 0000000..75671a3 --- /dev/null +++ b/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js @@ -0,0 +1,46 @@ +/* +Minimize Maximum Pair Sum in Array +https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/description/ + +The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs. + +For example, if we have pairs (1,5), (2,3), and (4,4), the maximum pair sum would be max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8. +Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that: + +Each element of nums is in exactly one pair, and +The maximum pair sum is minimized. +Return the minimized maximum pair sum after optimally pairing up the elements. + + + +Example 1: + +Input: nums = [3,5,2,3] +Output: 7 +Explanation: The elements can be paired up into pairs (3,3) and (5,2). +The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7. + + +Example 2: + +Input: nums = [3,5,4,2,4,6] +Output: 8 +Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2). +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) { + 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--]); + } + return max; +}; + +module.exports.minPairSum = minPairSum; diff --git a/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js b/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js new file mode 100644 index 0000000..240edd6 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js @@ -0,0 +1,10 @@ +const assert = require('assert'); +const minPairSum = require('../../LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array').minPairSum; + +var test = function () { + assert.equal(minPairSum([3,5,2,3]), 7); + assert.equal(minPairSum([3,5,4,2,4,6]), 8); + assert.equal(minPairSum([1,1,1,1]), 2); +} + +module.exports.test = test; diff --git a/README.md b/README.md index ac9c66a..91d495b 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ To run a specific problem in your console run `node ` (e.g. | [Time Needed to Rearrange a Binary String](/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js)| Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ | | [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ | | [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ | +| [Minimize Maximum Pair Sum in Array ](/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js) | Medium | https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ | | [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ | | [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ | | [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ | From 24d0ef3471736173239a19678a9a0a40eaea2315 Mon Sep 17 00:00:00 2001 From: Temitope Agbaje Date: Sat, 8 Oct 2022 08:42:13 +0100 Subject: [PATCH 02/22] Add another method to 2sum Problem --- LeetcodeProblems/Algorithms/2Sum.js | 28 +++++++++++------ LeetcodeProblemsTests/Algorithms/2Sum_Test.js | 6 ++-- .../Algorithms/3Sum_Closest_Test.js | 10 +++--- LeetcodeProblemsTests/Algorithms/3Sum_Test.js | 21 ++++++------- .../Algorithms/Add_Two_Numbers_Test.js | 7 ++--- .../Algorithms/Award_Budget_Cuts_Test.js | 4 +-- Test.js | 31 ++++++++++++------- 7 files changed, 61 insertions(+), 46 deletions(-) diff --git a/LeetcodeProblems/Algorithms/2Sum.js b/LeetcodeProblems/Algorithms/2Sum.js index 44df5cd..a4b6373 100644 --- a/LeetcodeProblems/Algorithms/2Sum.js +++ b/LeetcodeProblems/Algorithms/2Sum.js @@ -28,16 +28,26 @@ Output: [0,1] * @param {number} target * @return {number[]} */ - var twoSum = function(nums, target) { - let map ={}; - for(let i=0;i { return new Promise(function (resolve, reject) { @@ -28,11 +35,13 @@ var loadProblems = () => { if (error) { reject(error); } else { - problems = files.filter(item => !(REGEX_PATTERN_HIDDEN_FILES).test(item)); + problems = files.filter( + (item) => !REGEX_PATTERN_HIDDEN_FILES.test(item) + ); resolve(problems); } - }) + }); }); -} +}; test_all(); From 8f5e2d126d737a12af596d48f868f3fa45789b2f Mon Sep 17 00:00:00 2001 From: Temitope Agbaje Date: Sat, 8 Oct 2022 20:21:53 +0100 Subject: [PATCH 03/22] updated 2sum --- LeetcodeProblems/Algorithms/2Sum.js | 6 ++++-- LeetcodeProblemsTests/Algorithms/2Sum_Test.js | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/LeetcodeProblems/Algorithms/2Sum.js b/LeetcodeProblems/Algorithms/2Sum.js index a4b6373..11e7606 100644 --- a/LeetcodeProblems/Algorithms/2Sum.js +++ b/LeetcodeProblems/Algorithms/2Sum.js @@ -38,9 +38,10 @@ var twoSum = function (nums, target) { map[nums[i]] = i; } } +}; - //Another method - +//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) { @@ -51,3 +52,4 @@ var twoSum = function (nums, target) { }; module.exports.twoSum = twoSum; +module.exports.twoSum2 = twoSum2; diff --git a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js index ced9e42..1cf3897 100644 --- a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js +++ b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js @@ -1,5 +1,6 @@ const assert = require("assert"); const twoSum = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum; +const twoSum2 = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum2; var test = function () { assert.deepEqual([0,1], twoSum([2,7,11,15], 9)); @@ -7,5 +8,12 @@ var test = function () { assert.deepEqual([0,1], twoSum([3,3], 6)); }; +var test2 = function () { + assert.deepEqual([0,1], twoSum2([2,7,11,15], 9)); + assert.deepEqual([1,2], twoSum2([3,2,4], 6)); + assert.deepEqual([0,1], twoSum2([3,3], 6)); +}; + module.exports.test = test; +module.exports.test2 = test2; From 71ad30223f2d5e8dea2a99ae79de2a3eb301df52 Mon Sep 17 00:00:00 2001 From: hot9cups Date: Sat, 8 Oct 2022 21:26:06 +0530 Subject: [PATCH 04/22] Add Top K Frequent Elements - Added Top K Frequent Elements and the corresponding tests. - Updated Readme to reflect the same. --- .../Algorithms/Top_K_Frequent_Elements.js | 54 +++++++++++++++++++ .../Top_K_Frequent_Elements_Test.js | 10 ++++ README.md | 1 + 3 files changed, 65 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js create mode 100644 LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js diff --git a/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js b/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js new file mode 100644 index 0000000..a67a82d --- /dev/null +++ b/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js @@ -0,0 +1,54 @@ +/* +Top K Frequent Elements +https://leetcode.com/problems/top-k-frequent-elements/description/ + +Given an integer array nums and an integer k, return the k most frequent elements. +You may return the answer in any order. + + +Example 1: + +Input: nums = [1,1,1,2,2,3], k = 2 +Output: [1,2] + + +Example 2: + +Input: nums = [1], k = 1 +Output: [1] + + +Constraints: + +1) 1 <= nums.length <= 10^5 +2) -10^4 <= nums[i] <= 10^4 +3) k is in the range [1, the number of unique elements in the array]. +4) It is guaranteed that the answer is unique. +*/ + +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ + var topKFrequent = function(nums, k) { + const freqMap = new Map(); + const bucket = []; + const result = []; + + for(let num of nums) { + freqMap.set(num, (freqMap.get(num) || 0) + 1); + } + + for(let [num, freq] of freqMap) { + bucket[freq] = (bucket[freq] || new Set()).add(num); + } + + for(let i = bucket.length-1; i >= 0; i--) { + if(bucket[i]) result.push(...bucket[i]); + if(result.length === k) break; + } + return result; +}; + +module.exports.topKFrequent = topKFrequent; diff --git a/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js b/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js new file mode 100644 index 0000000..5900f68 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js @@ -0,0 +1,10 @@ +const assert = require('assert'); +const topKFrequent = require('../../LeetcodeProblems/Algorithms/Top_K_Frequent_Elements').topKFrequent; + +var test = function () { + assert.deepEqual(topKFrequent([1,1,1,2,2,3], 2).sort(), [1,2]); + assert.deepEqual(topKFrequent([7,8,9,8,9,8], 2).sort(), [8,9]); + assert.deepEqual(topKFrequent([1,1,1,1], 1).sort(), [1]); +} + +module.exports.test = test; diff --git a/README.md b/README.md index 91d495b..6bf105e 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ To run a specific problem in your console run `node ` (e.g. | [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ | | [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ | | [Minimize Maximum Pair Sum in Array ](/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js) | Medium | https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ | +| [Top K Frequent Elements ](/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js) | Medium | https://leetcode.com/problems/top-k-frequent-elements/ | | [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ | | [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ | | [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ | From c02cf2cc19b7eebf598e37ddf295b81a01e68c0b Mon Sep 17 00:00:00 2001 From: hot9cups Date: Sun, 9 Oct 2022 06:58:02 +0530 Subject: [PATCH 05/22] Update LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js Co-authored-by: Ignacio Chiazzo Cardarello --- LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js b/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js index a67a82d..5180f8c 100644 --- a/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js +++ b/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js @@ -45,8 +45,10 @@ Constraints: } for(let i = bucket.length-1; i >= 0; i--) { - if(bucket[i]) result.push(...bucket[i]); - if(result.length === k) break; + if(bucket[i]) { + result.push(...bucket[i]); + if(result.length === k) break; + } } return result; }; From 04a3e81896b36d3ff63d8d0012b1ac6193fc045e Mon Sep 17 00:00:00 2001 From: hot9cups Date: Sun, 9 Oct 2022 12:52:39 +0530 Subject: [PATCH 06/22] EsLint and Tests fix - A lot of files had Linting issues. Used EsLint to fix all the troublesome files. - Fixed the tests that were failing. Node Tests.js should run just fine now! --- LeetcodeProblems/Algorithms/2Sum.js | 6 +- LeetcodeProblems/Algorithms/3SumClosest.js | 28 +++--- .../Algorithms/Container_With_Most_Water.js | 16 ++-- LeetcodeProblems/Algorithms/Find_Anagrams.js | 54 ++++++------ .../Find_Subarrays_With_Equal_Sum.js | 2 +- LeetcodeProblems/Algorithms/Happy_Number.js | 16 ++-- .../Algorithms/Longest_Common_Prefix.js | 8 +- .../Algorithms/Longest_Substring.js | 12 +-- .../Algorithms/Max_Consecutive_Ones_III.js | 22 ++--- .../Algorithms/Maximise_Hour_Glass_Sum.js | 18 ++-- .../Minimize_Maximum_Pair_Sum_In_Array.js | 4 +- .../Minimum_Add_To_Make_Parentheses_Valid.js | 39 ++++---- .../Algorithms/Minimum_Size_Subarray.js | 30 +++---- .../Algorithms/Minimum_Window_Substring.js | 88 +++++++++---------- .../Algorithms/Next_Permutation.js | 28 +++--- .../Algorithms/Permutations_In_String.js | 42 ++++----- LeetcodeProblems/Algorithms/Shuffle_String.js | 6 +- .../Time_Needed_Rearrange_Binary_String.js | 10 ++- .../Algorithms/Top_K_Frequent_Elements.js | 14 +-- LeetcodeProblemsTests/Algorithms/2Sum_Test.js | 8 +- .../Backspace_String_Compare_Test.js | 6 +- ...Best_Time_To_Buy_And_Sell_Stock_II_Test.js | 4 +- .../Algorithms/Binary_Gap_Test.js | 4 +- .../Algorithms/Clone_Graph_Test.js | 2 +- .../Algorithms/Coin_Change_Test.js | 4 +- ...rom_Preorder_and_Inorder_Traversal_Test.js | 4 +- .../Container_With_Most_Water_Test.js | 4 +- .../Algorithms/Deletion_Distance_Test.js | 8 +- .../Algorithms/Design_Circular_Deque_Test.js | 6 +- .../Algorithms/Edit_Distance_Test.js | 6 +- .../Algorithms/Escape_The_Ghosts_Test.js | 4 +- .../Algorithms/Find_Anagrams_Test.js | 4 +- .../Find_Subarrays_With_Equal_Sum_Test.js | 6 +- .../Algorithms/Flood_Fill_Test.js | 4 +- .../Algorithms/Generate_Parenthesis_Test.js | 2 +- .../Algorithms/Group_Anagrams_Test.js | 8 +- .../Algorithms/Happy_Number_Test.js | 6 +- .../Implement_stack_using_queues_Test.js | 6 +- .../Kth_Largest_Element_in_an_Array_Test.js | 4 +- .../Algorithms/Linked_List_Cycle_II_Test.js | 8 +- .../Algorithms/Longest_Common_Prefix_Test.js | 4 +- .../Longest_Consecutive_Sequence_Test.js | 4 +- .../Longest_Palindromic_Substring_Test.js | 4 +- .../Algorithms/Longest_Substring_Test.js | 4 +- ...t_Common_Ancestor_of_a_Binary_Tree_Test.js | 10 +-- .../Algorithms/Majority_Element_Test.js | 6 +- .../Algorithms/Max_Area_Of_Island_Test.js | 12 +-- .../Max_Consecutive_Ones_III_Test.js | 4 +- .../Algorithms/Maximal_Square_Test.js | 4 +- .../Algorithms/Maximise_Hour_Glass_Sum.js | 6 +- .../Algorithms/Maximun_Subarray_Test.js | 4 +- .../Algorithms/Min_Stack_Test.js | 4 +- ...Minimize_Maximum_Pair_Sum_In_Array_Test.js | 6 +- ...imum_Add_To_Make_Parentheses_Valid_Test.js | 4 +- .../Algorithms/Minimum_Size_Subarray_Test.js | 4 +- .../Minimum_Window_Substring_Test.js | 4 +- .../Algorithms/NQueens_Test.js | 10 +-- .../Algorithms/Next_Permutation_Test.js | 4 +- .../Algorithms/Number_of_Islands_Test.js | 4 +- .../Number_of_Segments_in_a_String_Test.js | 4 +- .../Algorithms/Permutations_II_Test.js | 32 +++---- .../Algorithms/Permutations_In_String_Test.js | 4 +- .../Algorithms/Permutations_Test.js | 4 +- .../Permutations_With_Duplicates_Test.js | 6 +- .../Permutations_Without_Duplicates_Test.js | 6 +- .../Regular_Expression_Matching_Test.js | 6 +- .../Remove_Invalid_Parentheses_Test.js | 6 +- .../Algorithms/Restore_IP_Addresses_Test.js | 10 +-- .../Algorithms/Reverse_Integer_Test.js | 6 +- .../Algorithms/Reverse_String_II_Test.js | 6 +- .../Algorithms/Same_Tree_Test.js | 2 +- .../SearchIng_Rotated_Sorted_Array_Test.js | 6 +- .../Algorithms/Search_a_2D_Matrix_II_Test.js | 6 +- .../Algorithms/Search_a_2D_Matrix_Test.js | 6 +- .../Algorithms/Set_Matrix_Zeroes_Test.js | 6 +- .../Algorithms/Shuffle_String_Test.js | 12 +-- .../Algorithms/Simplify_Path_Test.js | 8 +- .../Algorithms/Spiral_Matrix_Test.js | 16 ++-- .../Algorithms/Subarray_Sum_Equals_K_Test.js | 6 +- .../Algorithms/Subsets_Test.js | 4 +- .../Algorithms/Sum_Of_Square_Numbers_Test.js | 6 +- .../Algorithms/Swap_Nodes_In_Pairs_Test.js | 10 +-- .../Algorithms/Symmetric_Tree_Test.js | 2 +- .../Algorithms/Tic_Tac_Toe_Test.js | 8 +- ...ime_Needed_Rearrange_Binary_String_Test.js | 10 +-- .../Top_K_Frequent_Elements_Test.js | 6 +- .../Unique_Binary_Search_Trees_Test.js | 10 +-- .../Algorithms/Unique_Paths_Test.js | 10 +-- .../Algorithms/Valid_Parentheses_Test.js | 6 +- ...der_Serialization_of_a_Binary_Tree_Test.js | 4 +- .../Algorithms/merge_k_sorted_lists_Test.js | 8 +- 91 files changed, 449 insertions(+), 446 deletions(-) diff --git a/LeetcodeProblems/Algorithms/2Sum.js b/LeetcodeProblems/Algorithms/2Sum.js index 11e7606..3cf3682 100644 --- a/LeetcodeProblems/Algorithms/2Sum.js +++ b/LeetcodeProblems/Algorithms/2Sum.js @@ -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; @@ -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]; } } diff --git a/LeetcodeProblems/Algorithms/3SumClosest.js b/LeetcodeProblems/Algorithms/3SumClosest.js index 271821a..22f0734 100644 --- a/LeetcodeProblems/Algorithms/3SumClosest.js +++ b/LeetcodeProblems/Algorithms/3SumClosest.js @@ -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; diff --git a/LeetcodeProblems/Algorithms/Container_With_Most_Water.js b/LeetcodeProblems/Algorithms/Container_With_Most_Water.js index 4b6098f..90b8373 100644 --- a/LeetcodeProblems/Algorithms/Container_With_Most_Water.js +++ b/LeetcodeProblems/Algorithms/Container_With_Most_Water.js @@ -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; }; @@ -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; \ No newline at end of file diff --git a/LeetcodeProblems/Algorithms/Find_Anagrams.js b/LeetcodeProblems/Algorithms/Find_Anagrams.js index 1d201b0..5624329 100644 --- a/LeetcodeProblems/Algorithms/Find_Anagrams.js +++ b/LeetcodeProblems/Algorithms/Find_Anagrams.js @@ -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; diff --git a/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js b/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js index 3981e04..fa28d07 100644 --- a/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js +++ b/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum.js @@ -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++) { diff --git a/LeetcodeProblems/Algorithms/Happy_Number.js b/LeetcodeProblems/Algorithms/Happy_Number.js index 4d6bf3b..df3e996 100644 --- a/LeetcodeProblems/Algorithms/Happy_Number.js +++ b/LeetcodeProblems/Algorithms/Happy_Number.js @@ -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; diff --git a/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js b/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js index a992179..2468131 100644 --- a/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js +++ b/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js @@ -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); }); }; diff --git a/LeetcodeProblems/Algorithms/Longest_Substring.js b/LeetcodeProblems/Algorithms/Longest_Substring.js index a6430f1..cc6726c 100644 --- a/LeetcodeProblems/Algorithms/Longest_Substring.js +++ b/LeetcodeProblems/Algorithms/Longest_Substring.js @@ -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; @@ -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++; } diff --git a/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js b/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js index 7766668..5344c66 100644 --- a/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js +++ b/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js @@ -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; diff --git a/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js b/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js index 124d4a2..55bfd3f 100644 --- a/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js +++ b/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js @@ -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 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; }; diff --git a/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js b/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js index 9181d20..3f88dc1 100644 --- a/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js +++ b/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js @@ -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; }; @@ -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; \ No newline at end of file +module.exports.minAddToMakeValid = minAddToMakeValid; +module.exports.minAddToMakeValidUsingQueue = minAddToMakeValidUsingQueue; \ No newline at end of file diff --git a/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js b/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js index dcaa489..481531e 100644 --- a/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js +++ b/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js @@ -24,8 +24,8 @@ Output: 0 * @param {number[]} nums * @return {number} */ - var minSubArrayLength = function(target, nums) { - if(nums.length == 0) { return 0 } +var minSubArrayLength = function(target, nums) { + if(nums.length == 0) { return 0; } let start = 0; let end = 0; @@ -34,20 +34,20 @@ Output: 0 let currentWindow = 1; while(start < nums.length && end < nums.length) { - currentWindow = (end + 1 - start) - if(currentSum >= target || (minWindow != 0 && currentWindow > minWindow) ) { - if(minWindow == 0 || minWindow > currentWindow ) { - minWindow = currentWindow; - if(minWindow == 1) { return 1 }; - } - currentSum -= nums[start]; - start++; - } else { - end++; - if(end < nums.length) { - currentSum += nums[end]; - } + currentWindow = (end + 1 - start); + if(currentSum >= target || (minWindow != 0 && currentWindow > minWindow) ) { + if(minWindow == 0 || minWindow > currentWindow ) { + minWindow = currentWindow; + if(minWindow == 1) { return 1; } } + currentSum -= nums[start]; + start++; + } else { + end++; + if(end < nums.length) { + currentSum += nums[end]; + } + } } return minWindow; diff --git a/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js b/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js index d3b5bd5..b58892f 100644 --- a/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js +++ b/LeetcodeProblems/Algorithms/Minimum_Window_Substring.js @@ -73,59 +73,59 @@ var getHash = function (t) { // Solution 2 // Similar idea code slightly different; var buildHash = function(t) { - let hash = {}; - let occ = 0; - for(var i = 0; i < t.length; i++) { - occ = hash[t[i]] == undefined ? 0 : hash[t[i]]; - hash[t[i]] = occ + 1; - } - return hash; -} + let hash = {}; + let occ = 0; + for(var i = 0; i < t.length; i++) { + occ = hash[t[i]] == undefined ? 0 : hash[t[i]]; + hash[t[i]] = occ + 1; + } + return hash; +}; var minWindow2 = function(s, t) { - var hashT = buildHash(t); - var start = 0; - var end = 0; - var countLeft = t.length; - var minWindow = ""; - var minWindowLeft = -1; - var maxWindowRight = -1; - - while(start < s.length && end < s.length) { - if(countLeft > 0) { // window does not contain all elements - if(hashT[s[end]] !== undefined) { - hashT[s[end]] = hashT[s[end]] - 1; - if(hashT[s[end]] >= 0) { - countLeft--; - } - } + var hashT = buildHash(t); + var start = 0; + var end = 0; + var countLeft = t.length; + var minWindowLeft = -1; + var maxWindowRight = -1; + + while(start < s.length && end < s.length) { + if(countLeft > 0) { // window does not contain all elements + if(hashT[s[end]] !== undefined) { + hashT[s[end]] = hashT[s[end]] - 1; + if(hashT[s[end]] >= 0) { + countLeft--; + } + } - if(countLeft > 0) { - end++; - } - } else { // found window - if(minWindowLeft == -1 || ((maxWindowRight - minWindowLeft + 1) > (end - start + 1)) ) { - minWindowLeft = start; - maxWindowRight = end; - } - if(hashT[s[start]] !== undefined) { - hashT[s[start]] = hashT[s[start]] + 1; - if(hashT[s[start]] > 0) { - countLeft++; - end++; - } - } - start++; + if(countLeft > 0) { + end++; + } + } else { // found window + if(minWindowLeft == -1 || ((maxWindowRight - minWindowLeft + 1) > (end - start + 1)) ) { + minWindowLeft = start; + maxWindowRight = end; + } + if(hashT[s[start]] !== undefined) { + hashT[s[start]] = hashT[s[start]] + 1; + if(hashT[s[start]] > 0) { + countLeft++; + end++; } + } + start++; } + } - if(minWindowLeft > -1) { - return s.substring(minWindowLeft, maxWindowRight + 1); - } + if(minWindowLeft > -1) { + return s.substring(minWindowLeft, maxWindowRight + 1); + } - return ""; + return ""; }; module.exports.minWindow = minWindow; +module.exports.minWindow2 = minWindow2; \ No newline at end of file diff --git a/LeetcodeProblems/Algorithms/Next_Permutation.js b/LeetcodeProblems/Algorithms/Next_Permutation.js index e125d7f..30e496a 100644 --- a/LeetcodeProblems/Algorithms/Next_Permutation.js +++ b/LeetcodeProblems/Algorithms/Next_Permutation.js @@ -33,37 +33,37 @@ Output: [1,5,1] * @return {void} Do not return anything, modify nums in-place instead. */ - var nextPermutation = function(nums) { +var nextPermutation = function(nums) { let k = nums.length - 2; while ( k >= 0 && nums[k] >= nums[k + 1]) { - --k; + --k; } if (k === -1) { - reverse(nums, 0, nums.length-1); - return; + reverse(nums, 0, nums.length-1); + return; } for (let l = nums.length - 1; l > k; l--) { - if (nums[l] > nums[k]) { - swap(nums, k, l); - break; - } + if (nums[l] > nums[k]) { + swap(nums, k, l); + break; + } } reverse(nums, k + 1, nums.length -1); }; function swap(arr, a, b) { - let temp = arr[a]; - arr[a] = arr[b]; - arr[b] = temp; + let temp = arr[a]; + arr[a] = arr[b]; + arr[b] = temp; } function reverse(nums, start ,end) { while(start < end) { - swap(nums, start, end); - start++; - end--; + swap(nums, start, end); + start++; + end--; } } diff --git a/LeetcodeProblems/Algorithms/Permutations_In_String.js b/LeetcodeProblems/Algorithms/Permutations_In_String.js index 17ea56c..8ddfb40 100644 --- a/LeetcodeProblems/Algorithms/Permutations_In_String.js +++ b/LeetcodeProblems/Algorithms/Permutations_In_String.js @@ -26,9 +26,9 @@ s1 and s2 consist of lowercase English letters. * @param {string} s2 * @return {boolean} */ - var checkInclusion = function(s1, s2) { +var checkInclusion = function(s1, s2) { if(s1.length > s2.length) { - return false + return false; } let start = 0; @@ -37,37 +37,37 @@ s1 and s2 consist of lowercase English letters. let hashBuild = {}; for(var i = 0; i < s1.length; i++) { - hashBuild[s1[i]] = (hashBuild[s1[i]] || 0) + 1; + hashBuild[s1[i]] = (hashBuild[s1[i]] || 0) + 1; } for(var j = start; j < end; j++) { // TODO: didn't count upper bound - if(hashBuild[s2[j]] !== undefined) { - hashBuild[s2[j]] = hashBuild[s2[j]] - 1; - if(hashBuild[s2[j]] >= 0) { - countLeft--; - } + if(hashBuild[s2[j]] !== undefined) { + hashBuild[s2[j]] = hashBuild[s2[j]] - 1; + if(hashBuild[s2[j]] >= 0) { + countLeft--; } + } } while(end < s2.length) { - if(hashBuild[s2[end]] !== undefined) { - hashBuild[s2[end]] = hashBuild[s2[end]] - 1; - if(hashBuild[s2[end]] >= 0) { - countLeft--; - } + if(hashBuild[s2[end]] !== undefined) { + hashBuild[s2[end]] = hashBuild[s2[end]] - 1; + if(hashBuild[s2[end]] >= 0) { + countLeft--; } + } - if(countLeft == 0) { return true } + if(countLeft == 0) { return true; } - if(hashBuild[s2[start]] !== undefined) { - hashBuild[s2[start]] = hashBuild[s2[start]] + 1; - if(hashBuild[s2[start]] >= 1) { - countLeft++; - } + if(hashBuild[s2[start]] !== undefined) { + hashBuild[s2[start]] = hashBuild[s2[start]] + 1; + if(hashBuild[s2[start]] >= 1) { + countLeft++; } + } - start++; - end++; + start++; + end++; } return false; diff --git a/LeetcodeProblems/Algorithms/Shuffle_String.js b/LeetcodeProblems/Algorithms/Shuffle_String.js index e0ccd21..bf3395f 100644 --- a/LeetcodeProblems/Algorithms/Shuffle_String.js +++ b/LeetcodeProblems/Algorithms/Shuffle_String.js @@ -25,9 +25,9 @@ Explanation: After shuffling, each character remains in its position. * @return {string} */ var restoreString = function(s, indices) { - let arrshuffle = []; - indices.forEach((sindex, index) => arrshuffle[sindex] = s.charAt(index)) - return arrshuffle.join('') + let arrshuffle = []; + indices.forEach((sindex, index) => arrshuffle[sindex] = s.charAt(index)); + return arrshuffle.join(""); }; module.exports.restoreString = restoreString; \ No newline at end of file diff --git a/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js b/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js index 87e5b89..603a914 100644 --- a/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js +++ b/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js @@ -36,12 +36,14 @@ s[i] is either '0' or '1'. * @param {string} s * @return {number} */ - var secondsToRemoveOccurrences = function(s) { +var secondsToRemoveOccurrences = function(s) { let result = 0; - while (true) { - const replaced = s.replaceAll('01', '10'); + for(;;) { + const replaced = s.replaceAll("01", "10"); if (s === replaced) return result; s = replaced; result += 1; } -}; \ No newline at end of file +}; + +module.exports.secondsToRemoveOccurrences = secondsToRemoveOccurrences; diff --git a/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js b/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js index 5180f8c..80770cb 100644 --- a/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js +++ b/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js @@ -31,24 +31,24 @@ Constraints: * @param {number} k * @return {number[]} */ - var topKFrequent = function(nums, k) { +var topKFrequent = function(nums, k) { const freqMap = new Map(); const bucket = []; const result = []; for(let num of nums) { - freqMap.set(num, (freqMap.get(num) || 0) + 1); + freqMap.set(num, (freqMap.get(num) || 0) + 1); } for(let [num, freq] of freqMap) { - bucket[freq] = (bucket[freq] || new Set()).add(num); + bucket[freq] = (bucket[freq] || new Set()).add(num); } for(let i = bucket.length-1; i >= 0; i--) { - if(bucket[i]) { - result.push(...bucket[i]); - if(result.length === k) break; - } + if(bucket[i]) { + result.push(...bucket[i]); + if(result.length === k) break; + } } return result; }; diff --git a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js index 1cf3897..132b3db 100644 --- a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js +++ b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js @@ -2,18 +2,16 @@ const assert = require("assert"); const twoSum = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum; const twoSum2 = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum2; + var test = function () { assert.deepEqual([0,1], twoSum([2,7,11,15], 9)); assert.deepEqual([1,2], twoSum([3,2,4], 6)); assert.deepEqual([0,1], twoSum([3,3], 6)); -}; - -var test2 = function () { + assert.deepEqual([0,1], twoSum2([2,7,11,15], 9)); assert.deepEqual([1,2], twoSum2([3,2,4], 6)); assert.deepEqual([0,1], twoSum2([3,3], 6)); }; -module.exports.test = test; -module.exports.test2 = test2; +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js b/LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js index 29d0757..22fbc39 100644 --- a/LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Backspace_String_Compare_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -const backspaceCompare = require('../../LeetcodeProblems/Algorithms/Backspace_String_Compare').backspaceCompare; -const backspaceCompare2 = require('../../LeetcodeProblems/Algorithms/Backspace_String_Compare').backspaceCompare2; +const assert = require("assert"); +const backspaceCompare = require("../../LeetcodeProblems/Algorithms/Backspace_String_Compare").backspaceCompare; +const backspaceCompare2 = require("../../LeetcodeProblems/Algorithms/Backspace_String_Compare").backspaceCompare2; function test() { assert.equal(backspaceCompare("ab#c", "ad#c"), true); // true diff --git a/LeetcodeProblemsTests/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II_Test.js b/LeetcodeProblemsTests/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II_Test.js index 5af376c..c3207fb 100644 --- a/LeetcodeProblemsTests/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const maxProfit = require('../../LeetcodeProblems/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II').maxProfit; +const assert = require("assert"); +const maxProfit = require("../../LeetcodeProblems/Algorithms/Best_Time_To_Buy_And_Sell_Stock_II").maxProfit; function test() { assert.equal(maxProfit([7,1,5,3,6,4]), 7); diff --git a/LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js b/LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js index 2754af7..6025822 100644 --- a/LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Binary_Gap_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const binaryGap = require('../../LeetcodeProblems/Algorithms/Binary_Gap').binaryGap; +const assert = require("assert"); +const binaryGap = require("../../LeetcodeProblems/Algorithms/Binary_Gap").binaryGap; function test() { assert.equal(binaryGap(22), 2); // 10110 diff --git a/LeetcodeProblemsTests/Algorithms/Clone_Graph_Test.js b/LeetcodeProblemsTests/Algorithms/Clone_Graph_Test.js index ea8b26b..928d068 100644 --- a/LeetcodeProblemsTests/Algorithms/Clone_Graph_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Clone_Graph_Test.js @@ -1,5 +1,5 @@ var test = function() { // TODO -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Coin_Change_Test.js b/LeetcodeProblemsTests/Algorithms/Coin_Change_Test.js index 4629e79..df5ae45 100644 --- a/LeetcodeProblemsTests/Algorithms/Coin_Change_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Coin_Change_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const coinChange = require('../../LeetcodeProblems/Algorithms/Coin_Change').coinChange; +const assert = require("assert"); +const coinChange = require("../../LeetcodeProblems/Algorithms/Coin_Change").coinChange; function test() { assert.equal(coinChange([], 3), -1); diff --git a/LeetcodeProblemsTests/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Test.js b/LeetcodeProblemsTests/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Test.js index 93b7aed..1ddea51 100644 --- a/LeetcodeProblemsTests/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -var buildTree = require('../../LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal').buildTree; +// const assert = require("assert"); +var buildTree = require("../../LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal").buildTree; function test() { // TODO diff --git a/LeetcodeProblemsTests/Algorithms/Container_With_Most_Water_Test.js b/LeetcodeProblemsTests/Algorithms/Container_With_Most_Water_Test.js index 4006962..cb08735 100644 --- a/LeetcodeProblemsTests/Algorithms/Container_With_Most_Water_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Container_With_Most_Water_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const maxArea = require('../../LeetcodeProblems/Algorithms/Container_With_Most_Water_Test').maxArea; +const assert = require("assert"); +const maxArea = require("../../LeetcodeProblems/Algorithms/Container_With_Most_Water").maxArea; function test() { assert.equal(49, maxArea([1,8,6,2,5,4,8,3,7])); diff --git a/LeetcodeProblemsTests/Algorithms/Deletion_Distance_Test.js b/LeetcodeProblemsTests/Algorithms/Deletion_Distance_Test.js index 0cfdb27..d1ae378 100644 --- a/LeetcodeProblemsTests/Algorithms/Deletion_Distance_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Deletion_Distance_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -var deletionDistance = require('../../LeetcodeProblems/Algorithms/Deletion_Distance').deletionDistance; -var deletionDistance2 = require('../../LeetcodeProblems/Algorithms/Deletion_Distance').deletionDistance2; -var deletionDistanceDP = require('../../LeetcodeProblems/Algorithms/Deletion_Distance').deletionDistanceDP; +const assert = require("assert"); +var deletionDistance = require("../../LeetcodeProblems/Algorithms/Deletion_Distance").deletionDistance; +var deletionDistance2 = require("../../LeetcodeProblems/Algorithms/Deletion_Distance").deletionDistance2; +var deletionDistanceDP = require("../../LeetcodeProblems/Algorithms/Deletion_Distance").deletionDistanceDP; function test() { assert.equal(deletionDistance("dog", "frog"), 3); diff --git a/LeetcodeProblemsTests/Algorithms/Design_Circular_Deque_Test.js b/LeetcodeProblemsTests/Algorithms/Design_Circular_Deque_Test.js index 40f0b0e..cf8bc81 100644 --- a/LeetcodeProblemsTests/Algorithms/Design_Circular_Deque_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Design_Circular_Deque_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -var MyCircularDeque = require('../../LeetcodeProblems/Algorithms/Design_Circular_Deque').MyCircularDeque; +const assert = require("assert"); +var MyCircularDeque = require("../../LeetcodeProblems/Algorithms/Design_Circular_Deque").MyCircularDeque; var test = function() { const obj = new MyCircularDeque(3); @@ -12,6 +12,6 @@ var test = function() { assert.equal(obj.deleteLast(), true); assert.equal(obj.insertFront(4), true); assert.equal(obj.getFront(), 4); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Edit_Distance_Test.js b/LeetcodeProblemsTests/Algorithms/Edit_Distance_Test.js index f190880..2810420 100644 --- a/LeetcodeProblemsTests/Algorithms/Edit_Distance_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Edit_Distance_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -var minDistance = require('../../LeetcodeProblems/Algorithms/Edit_Distance').minDistance; -var minDistance2 = require('../../LeetcodeProblems/Algorithms/Edit_Distance').minDistance2; +const assert = require("assert"); +var minDistance = require("../../LeetcodeProblems/Algorithms/Edit_Distance").minDistance; +var minDistance2 = require("../../LeetcodeProblems/Algorithms/Edit_Distance").minDistance2; function test() { assert.equal(minDistance("ros", "horse"), 3); diff --git a/LeetcodeProblemsTests/Algorithms/Escape_The_Ghosts_Test.js b/LeetcodeProblemsTests/Algorithms/Escape_The_Ghosts_Test.js index ec0f67d..4225fbf 100644 --- a/LeetcodeProblemsTests/Algorithms/Escape_The_Ghosts_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Escape_The_Ghosts_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -var escapeGhosts = require('../../LeetcodeProblems/Algorithms/Escape_The_Ghosts').escapeGhosts; +const assert = require("assert"); +var escapeGhosts = require("../../LeetcodeProblems/Algorithms/Escape_The_Ghosts").escapeGhosts; function test() { assert.equal(escapeGhosts([[1, 0], [0, 3]], [0, 1]), true); diff --git a/LeetcodeProblemsTests/Algorithms/Find_Anagrams_Test.js b/LeetcodeProblemsTests/Algorithms/Find_Anagrams_Test.js index 504e136..3b90a57 100644 --- a/LeetcodeProblemsTests/Algorithms/Find_Anagrams_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Find_Anagrams_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -var findAnagrams = require('../../LeetcodeProblems/Algorithms/Find_Anagrams').findAnagrams; +const assert = require("assert"); +var findAnagrams = require("../../LeetcodeProblems/Algorithms/Find_Anagrams").findAnagrams; function test() { assert.deepEqual([], findAnagrams("AA", "BB")); diff --git a/LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js b/LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js index 09f692e..956a327 100644 --- a/LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Find_Subarrays_With_Equal_Sum_Test.js @@ -1,10 +1,10 @@ -const assert = require('assert'); -const findSubarrays = require('../../LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum').findSubarrays; +const assert = require("assert"); +const findSubarrays = require("../../LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum").findSubarrays; var test = function () { assert.equal(findSubarrays([4,2,4]), true); assert.equal(findSubarrays([1,2,3,4,5]), false); assert.equal(findSubarrays([0,0,0]), true); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Flood_Fill_Test.js b/LeetcodeProblemsTests/Algorithms/Flood_Fill_Test.js index 36a29bb..447795f 100644 --- a/LeetcodeProblemsTests/Algorithms/Flood_Fill_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Flood_Fill_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const floodFill = require('../../LeetcodeProblems/Algorithms/Flood_Fill').floodFill; +const assert = require("assert"); +const floodFill = require("../../LeetcodeProblems/Algorithms/Flood_Fill").floodFill; function test() { assert.deepEqual( diff --git a/LeetcodeProblemsTests/Algorithms/Generate_Parenthesis_Test.js b/LeetcodeProblemsTests/Algorithms/Generate_Parenthesis_Test.js index ea8b26b..928d068 100644 --- a/LeetcodeProblemsTests/Algorithms/Generate_Parenthesis_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Generate_Parenthesis_Test.js @@ -1,5 +1,5 @@ var test = function() { // TODO -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Group_Anagrams_Test.js b/LeetcodeProblemsTests/Algorithms/Group_Anagrams_Test.js index be15de9..c871e7e 100644 --- a/LeetcodeProblemsTests/Algorithms/Group_Anagrams_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Group_Anagrams_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const groupAnagrams = require('../../LeetcodeProblems/Algorithms/Group_Anagrams').groupAnagrams; +const assert = require("assert"); +const groupAnagrams = require("../../LeetcodeProblems/Algorithms/Group_Anagrams").groupAnagrams; function test() { assert.deepEqual( groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]), - [ [ 'eat', 'tea', 'ate' ], [ 'tan', 'nat' ], [ 'bat' ] ] - ) + [ [ "eat", "tea", "ate" ], [ "tan", "nat" ], [ "bat" ] ] + ); } module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js b/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js index 13d2013..fa4544d 100644 --- a/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Happy_Number_Test.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const isHappy = require('../../LeetcodeProblems/Algorithms/Happy_Number').isHappy; +const assert = require("assert"); +const isHappy = require("../../LeetcodeProblems/Algorithms/Happy_Number").isHappy; var test = function () { assert.equal(isHappy(19),true); assert.equal(isHappy(2),false); -} +}; module.exports.test = test; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/Implement_stack_using_queues_Test.js b/LeetcodeProblemsTests/Algorithms/Implement_stack_using_queues_Test.js index cdc8e7d..8e3fb3a 100644 --- a/LeetcodeProblemsTests/Algorithms/Implement_stack_using_queues_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Implement_stack_using_queues_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const MyStack = require('../../LeetcodeProblems/Algorithms/Implement_stack_using_queues').MyStack; +const assert = require("assert"); +const MyStack = require("../../LeetcodeProblems/Algorithms/Implement_stack_using_queues").MyStack; var test = function () { var myStack = new MyStack(); @@ -14,6 +14,6 @@ var test = function () { assert.equal(myStack.pop(), 1); assert.equal(myStack.pop(), 2); assert.equal(myStack.pop(), 3); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Kth_Largest_Element_in_an_Array_Test.js b/LeetcodeProblemsTests/Algorithms/Kth_Largest_Element_in_an_Array_Test.js index a21900d..156d2e1 100644 --- a/LeetcodeProblemsTests/Algorithms/Kth_Largest_Element_in_an_Array_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Kth_Largest_Element_in_an_Array_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const findKthLargest = require('../../LeetcodeProblems/Algorithms/Kth_Largest_Element_in_an_Array').findKthLargest; +const assert = require("assert"); +const findKthLargest = require("../../LeetcodeProblems/Algorithms/Kth_Largest_Element_in_an_Array").findKthLargest; function test() { assert.equal(findKthLargest([3,2,1,5,6,4], 2), 5); diff --git a/LeetcodeProblemsTests/Algorithms/Linked_List_Cycle_II_Test.js b/LeetcodeProblemsTests/Algorithms/Linked_List_Cycle_II_Test.js index 0206e8b..5014106 100644 --- a/LeetcodeProblemsTests/Algorithms/Linked_List_Cycle_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Linked_List_Cycle_II_Test.js @@ -1,13 +1,13 @@ -const assert = require('assert'); -var ListNode = require('../../UtilsClasses/ListNode').ListNode; -const detectCycle = require('../../LeetcodeProblems/Algorithms/Linked_List_Cycle_II').detectCycle; +const assert = require("assert"); +var ListNode = require("../../UtilsClasses/ListNode").ListNode; +const detectCycle = require("../../LeetcodeProblems/Algorithms/Linked_List_Cycle_II").detectCycle; var test = function() { const cycle = buildCycle(); var list = cycle.list; var nodeCycle = cycle.nodeCycle; assert.equal(detectCycle(list), nodeCycle); -} +}; function buildCycle() { var node1 = ListNode.linkenList([1,2,3,4,5]); diff --git a/LeetcodeProblemsTests/Algorithms/Longest_Common_Prefix_Test.js b/LeetcodeProblemsTests/Algorithms/Longest_Common_Prefix_Test.js index d1d5e46..92c6c7a 100644 --- a/LeetcodeProblemsTests/Algorithms/Longest_Common_Prefix_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Longest_Common_Prefix_Test.js @@ -1,6 +1,6 @@ -const assert = require('assert'); -const longestCommonPrefix = require('../../LeetcodeProblems/Algorithms/Longest_Common_Prefix').longestCommonPrefix; +const assert = require("assert"); +const longestCommonPrefix = require("../../LeetcodeProblems/Algorithms/Longest_Common_Prefix").longestCommonPrefix; function test() { assert.equal(longestCommonPrefix(["flower","flow","flight"]), "fl"); diff --git a/LeetcodeProblemsTests/Algorithms/Longest_Consecutive_Sequence_Test.js b/LeetcodeProblemsTests/Algorithms/Longest_Consecutive_Sequence_Test.js index 6ad441d..00cc6ed 100644 --- a/LeetcodeProblemsTests/Algorithms/Longest_Consecutive_Sequence_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Longest_Consecutive_Sequence_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const longestConsecutive = require('../../LeetcodeProblems/Algorithms/Longest_Consecutive_Sequence').longestConsecutive; +const assert = require("assert"); +const longestConsecutive = require("../../LeetcodeProblems/Algorithms/Longest_Consecutive_Sequence").longestConsecutive; function test() { assert.equal(longestConsecutive([100, 1, 200, 3, 2, 400, 201]), 3); diff --git a/LeetcodeProblemsTests/Algorithms/Longest_Palindromic_Substring_Test.js b/LeetcodeProblemsTests/Algorithms/Longest_Palindromic_Substring_Test.js index 1b61abc..cab1b1b 100644 --- a/LeetcodeProblemsTests/Algorithms/Longest_Palindromic_Substring_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Longest_Palindromic_Substring_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const longestPalindrome = require('../../LeetcodeProblems/Algorithms/Longest_Palindromic_Substring').longestPalindrome; +const assert = require("assert"); +const longestPalindrome = require("../../LeetcodeProblems/Algorithms/Longest_Palindromic_Substring").longestPalindrome; function test() { assert.equal(longestPalindrome("pabcdcbte"), "bcdcb"); diff --git a/LeetcodeProblemsTests/Algorithms/Longest_Substring_Test.js b/LeetcodeProblemsTests/Algorithms/Longest_Substring_Test.js index 09c073e..f4a873e 100644 --- a/LeetcodeProblemsTests/Algorithms/Longest_Substring_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Longest_Substring_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const lengthOfLongestSubstring = require('../../LeetcodeProblems/Algorithms/Longest_Substring').lengthOfLongestSubstring; +const assert = require("assert"); +const lengthOfLongestSubstring = require("../../LeetcodeProblems/Algorithms/Longest_Substring").lengthOfLongestSubstring; function test() { assert.equal(4, lengthOfLongestSubstring("abcdbcd")); diff --git a/LeetcodeProblemsTests/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree_Test.js b/LeetcodeProblemsTests/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree_Test.js index 0ec8727..045f9ba 100644 --- a/LeetcodeProblemsTests/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -var TreeNode = require('../../UtilsClasses/TreeNode').TreeNode; -const lowestCommonAncestor = require('../../LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree').lowestCommonAncestor; -const lowestCommonAncestor2 = require('../../LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree').lowestCommonAncestor2; +// const assert = require("assert"); +var TreeNode = require("../../UtilsClasses/TreeNode").TreeNode; +const lowestCommonAncestor = require("../../LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree").lowestCommonAncestor; +const lowestCommonAncestor2 = require("../../LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree").lowestCommonAncestor2; var test = function() { var root = new TreeNode(3); @@ -34,6 +34,6 @@ var test = function() { console.log(lowestCommonAncestor2(root, left, tempRight.right)); console.log(lowestCommonAncestor2(root, left, right)); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Majority_Element_Test.js b/LeetcodeProblemsTests/Algorithms/Majority_Element_Test.js index 494d641..b9420d2 100644 --- a/LeetcodeProblemsTests/Algorithms/Majority_Element_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Majority_Element_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const majorityElement = require('../../LeetcodeProblems/Algorithms/Majority_Element').majorityElement; +const assert = require("assert"); +const majorityElement = require("../../LeetcodeProblems/Algorithms/Majority_Element").majorityElement; function test() { assert.equal(majorityElement([2,2,3]), 2); @@ -7,4 +7,4 @@ function test() { assert.equal(majorityElement([1,1,1,2,3,45,1,2,4,1,1]), 1); } -module.exports.test = test +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Max_Area_Of_Island_Test.js b/LeetcodeProblemsTests/Algorithms/Max_Area_Of_Island_Test.js index 69bf4e1..b8269e6 100644 --- a/LeetcodeProblemsTests/Algorithms/Max_Area_Of_Island_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Max_Area_Of_Island_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const { maxAreaOfIsland } = require('../../LeetcodeProblems/Algorithms/Max_Area_Of_Island'); +const assert = require("assert"); +const { maxAreaOfIsland } = require("../../LeetcodeProblems/Algorithms/Max_Area_Of_Island"); function test1() { var matrix = [ @@ -11,7 +11,7 @@ function test1() { [0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,0,1,1,0,0,0,0] - ] + ]; assert.strictEqual(maxAreaOfIsland(matrix), 6); } @@ -24,7 +24,7 @@ function test2() { [0, 1, 0, 0, 1], [0, 1, 1, 0, 1], [0, 0, 0, 0, 0], - ] + ]; assert.strictEqual(maxAreaOfIsland(matrix), 5); } @@ -37,7 +37,7 @@ function test3() { [0, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], - ] + ]; assert.strictEqual(maxAreaOfIsland(matrix), 11); } @@ -48,4 +48,4 @@ function test() { test3(); } -module.exports.test = test +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Max_Consecutive_Ones_III_Test.js b/LeetcodeProblemsTests/Algorithms/Max_Consecutive_Ones_III_Test.js index 96b6986..ef4eb81 100644 --- a/LeetcodeProblemsTests/Algorithms/Max_Consecutive_Ones_III_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Max_Consecutive_Ones_III_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const longestOnes = require('../../LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III').longestOnes; +const assert = require("assert"); +const longestOnes = require("../../LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III").longestOnes; function test() { assert.equal(1, longestOnes([1], 1)); diff --git a/LeetcodeProblemsTests/Algorithms/Maximal_Square_Test.js b/LeetcodeProblemsTests/Algorithms/Maximal_Square_Test.js index 2aa2a6e..11a0764 100644 --- a/LeetcodeProblemsTests/Algorithms/Maximal_Square_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Maximal_Square_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const maximalSquare = require('../../LeetcodeProblems/Algorithms/Maximal_Square').maximalSquare; +const assert = require("assert"); +const maximalSquare = require("../../LeetcodeProblems/Algorithms/Maximal_Square").maximalSquare; function test() { assert.equal(maximalSquare([["1","0"]]), 1); diff --git a/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js b/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js index 066b941..f728cfd 100644 --- a/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js +++ b/LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const maxSum = require('../../LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum').maxSum; +const assert = require("assert"); +const maxSum = require("../../LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum").maxSum; function test() { assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]), 30); assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]]), 35); } -module.exports.test = test +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Maximun_Subarray_Test.js b/LeetcodeProblemsTests/Algorithms/Maximun_Subarray_Test.js index aeb6f0d..68fd57e 100644 --- a/LeetcodeProblemsTests/Algorithms/Maximun_Subarray_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Maximun_Subarray_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const maxSubArray = require('../../LeetcodeProblems/Algorithms/Maximun_Subarray').maxSubArray; +const assert = require("assert"); +const maxSubArray = require("../../LeetcodeProblems/Algorithms/Maximun_Subarray").maxSubArray; function test() { assert.equal(maxSubArray([]), 0); diff --git a/LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js b/LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js index 16fb7c6..5cbddda 100644 --- a/LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Min_Stack_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const MinStack = require('../../LeetcodeProblems/Algorithms/Min_Stack').MinStack; +const assert = require("assert"); +const MinStack = require("../../LeetcodeProblems/Algorithms/Min_Stack").MinStack; function test() { var minStack = new MinStack(); diff --git a/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js b/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js index 240edd6..3180536 100644 --- a/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Minimize_Maximum_Pair_Sum_In_Array_Test.js @@ -1,10 +1,10 @@ -const assert = require('assert'); -const minPairSum = require('../../LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array').minPairSum; +const assert = require("assert"); +const minPairSum = require("../../LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array").minPairSum; var test = function () { assert.equal(minPairSum([3,5,2,3]), 7); assert.equal(minPairSum([3,5,4,2,4,6]), 8); assert.equal(minPairSum([1,1,1,1]), 2); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Minimum_Add_To_Make_Parentheses_Valid_Test.js b/LeetcodeProblemsTests/Algorithms/Minimum_Add_To_Make_Parentheses_Valid_Test.js index b175736..69baf4d 100644 --- a/LeetcodeProblemsTests/Algorithms/Minimum_Add_To_Make_Parentheses_Valid_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Minimum_Add_To_Make_Parentheses_Valid_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const minAddToMakeValid = require('../../LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid').minAddToMakeValid; +const assert = require("assert"); +const minAddToMakeValid = require("../../LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid").minAddToMakeValid; var test = function() { assert.strictEqual(1, minAddToMakeValid("()(")); diff --git a/LeetcodeProblemsTests/Algorithms/Minimum_Size_Subarray_Test.js b/LeetcodeProblemsTests/Algorithms/Minimum_Size_Subarray_Test.js index 7d2c4d1..a09502e 100644 --- a/LeetcodeProblemsTests/Algorithms/Minimum_Size_Subarray_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Minimum_Size_Subarray_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const minSubArrayLength = require('../../LeetcodeProblems/Algorithms/Minimum_Size_Subarray').minSubArrayLength; +const assert = require("assert"); +const minSubArrayLength = require("../../LeetcodeProblems/Algorithms/Minimum_Size_Subarray").minSubArrayLength; function test() { assert.equal(0, minSubArrayLength(10, [])); diff --git a/LeetcodeProblemsTests/Algorithms/Minimum_Window_Substring_Test.js b/LeetcodeProblemsTests/Algorithms/Minimum_Window_Substring_Test.js index d18350e..e580710 100644 --- a/LeetcodeProblemsTests/Algorithms/Minimum_Window_Substring_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Minimum_Window_Substring_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const minWindow = require('../../LeetcodeProblems/Algorithms/Minimum_Window_Substring').minWindow; +const assert = require("assert"); +const minWindow = require("../../LeetcodeProblems/Algorithms/Minimum_Window_Substring").minWindow; function test() { assert.equal(minWindow("ADOBECODEBANC", "ABC"), "BANC"); diff --git a/LeetcodeProblemsTests/Algorithms/NQueens_Test.js b/LeetcodeProblemsTests/Algorithms/NQueens_Test.js index 9387762..42d95ff 100644 --- a/LeetcodeProblemsTests/Algorithms/NQueens_Test.js +++ b/LeetcodeProblemsTests/Algorithms/NQueens_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const solveNQueens = require('../../LeetcodeProblems/Algorithms/NQueens').solveNQueens; +// const assert = require("assert"); +const solveNQueens = require("../../LeetcodeProblems/Algorithms/NQueens").solveNQueens; // TODO: Add assertions @@ -7,7 +7,7 @@ var test = function() { printMatrixes(solveNQueens(4), 4); printMatrixes(solveNQueens(5), 5); printMatrixes(solveNQueens(6), 6); -} +}; var printMatrixes = function(matrixes, n) { console.log("Start solution of n: " + n); @@ -15,7 +15,7 @@ var printMatrixes = function(matrixes, n) { printMatrix(matrixes[i]); } console.log("End solution of n: " + n); -} +}; var printMatrix = function(matrix) { console.log("------------"); @@ -23,6 +23,6 @@ var printMatrix = function(matrix) { console.log(matrix[i]); } console.log("------------"); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js b/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js index e14a5e0..ec47f8c 100644 --- a/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const nextPermutation = require('../../LeetcodeProblems/Algorithms/Next_Permutation').nextPermutation; +const assert = require("assert"); +const nextPermutation = require("../../LeetcodeProblems/Algorithms/Next_Permutation").nextPermutation; function test() { let test1 = [1,2,3]; diff --git a/LeetcodeProblemsTests/Algorithms/Number_of_Islands_Test.js b/LeetcodeProblemsTests/Algorithms/Number_of_Islands_Test.js index cd07f85..1a46197 100644 --- a/LeetcodeProblemsTests/Algorithms/Number_of_Islands_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Number_of_Islands_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const numIslands = require('../../LeetcodeProblems/Algorithms/Number_of_Islands').numIslands; +const assert = require("assert"); +const numIslands = require("../../LeetcodeProblems/Algorithms/Number_of_Islands").numIslands; function test() { assert.equal(numIslands([[1]]), 1); diff --git a/LeetcodeProblemsTests/Algorithms/Number_of_Segments_in_a_String_Test.js b/LeetcodeProblemsTests/Algorithms/Number_of_Segments_in_a_String_Test.js index e78cd3e..13a982f 100644 --- a/LeetcodeProblemsTests/Algorithms/Number_of_Segments_in_a_String_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Number_of_Segments_in_a_String_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const countSegments = require('../../LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String').countSegments; +const assert = require("assert"); +const countSegments = require("../../LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String").countSegments; function test() { assert.equal(countSegments(" "), 0); diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_II_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_II_Test.js index 53f4449..e464f80 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_II_Test.js @@ -1,30 +1,30 @@ -const assert = require('assert'); -const permuteUnique = require('../../LeetcodeProblems/Algorithms/Permutations_II').permuteUnique; +const assert = require("assert"); +const permuteUnique = require("../../LeetcodeProblems/Algorithms/Permutations_II").permuteUnique; function test() { assert.deepEqual( permuteUnique([1,1,2]), - [ [ '1', '1', '2' ], [ '1', '2', '1' ], [ '2', '1', '1' ] ] + [ [ "1", "1", "2" ], [ "1", "2", "1" ], [ "2", "1", "1" ] ] ); assert.deepEqual( permuteUnique([1,3,2,1]), [ - [ '1', '1', '2', '3' ], - [ '1', '1', '3', '2' ], - [ '1', '2', '1', '3' ], - [ '1', '2', '3', '1' ], - [ '1', '3', '1', '2' ], - [ '1', '3', '2', '1' ], - [ '2', '1', '1', '3' ], - [ '2', '1', '3', '1' ], - [ '2', '3', '1', '1' ], - [ '3', '1', '1', '2' ], - [ '3', '1', '2', '1' ], - [ '3', '2', '1', '1' ] + [ "1", "1", "2", "3" ], + [ "1", "1", "3", "2" ], + [ "1", "2", "1", "3" ], + [ "1", "2", "3", "1" ], + [ "1", "3", "1", "2" ], + [ "1", "3", "2", "1" ], + [ "2", "1", "1", "3" ], + [ "2", "1", "3", "1" ], + [ "2", "3", "1", "1" ], + [ "3", "1", "1", "2" ], + [ "3", "1", "2", "1" ], + [ "3", "2", "1", "1" ] ] ); assert.deepEqual(permuteUnique([]), [ [] ]); - assert.deepEqual(permuteUnique([1,1]), [ [ '1', '1' ] ]); + assert.deepEqual(permuteUnique([1,1]), [ [ "1", "1" ] ]); } module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_In_String_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_In_String_Test.js index 33b4dbf..f8d4536 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_In_String_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_In_String_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const checkInclusion = require('../../LeetcodeProblems/Algorithms/Permutations_In_String').checkInclusion; +const assert = require("assert"); +const checkInclusion = require("../../LeetcodeProblems/Algorithms/Permutations_In_String").checkInclusion; function test() { assert.equal(false, checkInclusion("abc", "ab")); diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_Test.js index 8fe85e0..f3f338e 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const permute = require('../../LeetcodeProblems/Algorithms/Permutations').permute; +const assert = require("assert"); +const permute = require("../../LeetcodeProblems/Algorithms/Permutations").permute; function test() { assert.deepEqual(permute([]), [ [] ]); diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_With_Duplicates_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_With_Duplicates_Test.js index ecbdf12..984192b 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_With_Duplicates_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_With_Duplicates_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const subsetWithoutDuplicates = require('../../LeetcodeProblems/Algorithms/Permutations_With_Duplicates').subsetWithoutDuplicates; +const assert = require("assert"); +const subsetWithoutDuplicates = require("../../LeetcodeProblems/Algorithms/Permutations_With_Duplicates").subsetWithoutDuplicates; var test = function() { assert.deepEqual( @@ -19,6 +19,6 @@ var test = function() { [ 3, 2, 1, 1 ] ] ); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Permutations_Without_Duplicates_Test.js b/LeetcodeProblemsTests/Algorithms/Permutations_Without_Duplicates_Test.js index 60169e9..379b627 100644 --- a/LeetcodeProblemsTests/Algorithms/Permutations_Without_Duplicates_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Permutations_Without_Duplicates_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const subsetWithoutDuplicates = require('../../LeetcodeProblems/Algorithms/Permutations_Without_Duplicates').subsetWithoutDuplicates; +const assert = require("assert"); +const subsetWithoutDuplicates = require("../../LeetcodeProblems/Algorithms/Permutations_Without_Duplicates").subsetWithoutDuplicates; var test = function() { assert.deepEqual( @@ -13,6 +13,6 @@ var test = function() { [ 3, 2, 1 ] ] ); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Regular_Expression_Matching_Test.js b/LeetcodeProblemsTests/Algorithms/Regular_Expression_Matching_Test.js index ffcf28d..95bca74 100644 --- a/LeetcodeProblemsTests/Algorithms/Regular_Expression_Matching_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Regular_Expression_Matching_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const isMatch = require('../../LeetcodeProblems/Algorithms/Regular_Expression_Matching').isMatch; +const assert = require("assert"); +const isMatch = require("../../LeetcodeProblems/Algorithms/Regular_Expression_Matching").isMatch; var test = function() { assert.equal(isMatch("aa", "a"), false); @@ -8,6 +8,6 @@ var test = function() { assert.equal(isMatch("ab", ".*"), true); assert.equal(isMatch("aab", "c*a*b"), true); assert.equal(isMatch("mississippi", "mis*is*p*."), false); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Remove_Invalid_Parentheses_Test.js b/LeetcodeProblemsTests/Algorithms/Remove_Invalid_Parentheses_Test.js index a0a1a48..d345205 100644 --- a/LeetcodeProblemsTests/Algorithms/Remove_Invalid_Parentheses_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Remove_Invalid_Parentheses_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const removeInvalidParentheses = require('../../LeetcodeProblems/Algorithms/Remove_Invalid_Parentheses').removeInvalidParentheses; +const assert = require("assert"); +const removeInvalidParentheses = require("../../LeetcodeProblems/Algorithms/Remove_Invalid_Parentheses").removeInvalidParentheses; var test = function() { assert.equal(removeInvalidParentheses("))))(()"), "()"); assert.equal(removeInvalidParentheses("(()"), "()"); assert.equal(removeInvalidParentheses("(d))()"), "(d)()"); assert.equal(removeInvalidParentheses("(())"), "(())"); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Restore_IP_Addresses_Test.js b/LeetcodeProblemsTests/Algorithms/Restore_IP_Addresses_Test.js index f7d1c4a..9cf9ceb 100644 --- a/LeetcodeProblemsTests/Algorithms/Restore_IP_Addresses_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Restore_IP_Addresses_Test.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const restoreIpAddresses = require('../../LeetcodeProblems/Algorithms/Restore_IP_Addresses').restoreIpAddresses; +const assert = require("assert"); +const restoreIpAddresses = require("../../LeetcodeProblems/Algorithms/Restore_IP_Addresses").restoreIpAddresses; var test = function() { - assert.deepEqual(restoreIpAddresses("010010"), [ '0.10.0.10', '0.100.1.0']); - assert.deepEqual(restoreIpAddresses("25525511135"), [ '255.255.11.135', '255.255.111.35' ]); -} + assert.deepEqual(restoreIpAddresses("010010"), [ "0.10.0.10", "0.100.1.0"]); + assert.deepEqual(restoreIpAddresses("25525511135"), [ "255.255.11.135", "255.255.111.35" ]); +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Reverse_Integer_Test.js b/LeetcodeProblemsTests/Algorithms/Reverse_Integer_Test.js index a599cf3..95da676 100644 --- a/LeetcodeProblemsTests/Algorithms/Reverse_Integer_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Reverse_Integer_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const reverseInteger = require('../../LeetcodeProblems/Algorithms/Reverse_Integer').reverseInteger; +const assert = require("assert"); +const reverseInteger = require("../../LeetcodeProblems/Algorithms/Reverse_Integer").reverseInteger; var test = function() { assert.equal(reverseInteger(123), 321); assert.equal(reverseInteger(-123), -321); assert.equal(reverseInteger(120), 21); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Reverse_String_II_Test.js b/LeetcodeProblemsTests/Algorithms/Reverse_String_II_Test.js index e86446d..9fae9d4 100644 --- a/LeetcodeProblemsTests/Algorithms/Reverse_String_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Reverse_String_II_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const reverseStr = require('../../LeetcodeProblems/Algorithms/Reverse_String_II').reverseStr; +const assert = require("assert"); +const reverseStr = require("../../LeetcodeProblems/Algorithms/Reverse_String_II").reverseStr; var test = function() { assert.equal(reverseStr("abcdefg", 2), "bacdfeg"); assert.equal(reverseStr("abcdefg", 3), "cbadefg"); assert.equal(reverseStr("abcdefg", 1), "abcdefg"); assert.equal(reverseStr("abcdefg", 0), "abcdefg"); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Same_Tree_Test.js b/LeetcodeProblemsTests/Algorithms/Same_Tree_Test.js index ea8b26b..928d068 100644 --- a/LeetcodeProblemsTests/Algorithms/Same_Tree_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Same_Tree_Test.js @@ -1,5 +1,5 @@ var test = function() { // TODO -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/SearchIng_Rotated_Sorted_Array_Test.js b/LeetcodeProblemsTests/Algorithms/SearchIng_Rotated_Sorted_Array_Test.js index 1e45c57..866496c 100644 --- a/LeetcodeProblemsTests/Algorithms/SearchIng_Rotated_Sorted_Array_Test.js +++ b/LeetcodeProblemsTests/Algorithms/SearchIng_Rotated_Sorted_Array_Test.js @@ -1,8 +1,8 @@ -const assert = require('assert'); -const search = require('../../LeetcodeProblems/Algorithms/SearchIng_Rotated_Sorted_Array').search; +const assert = require("assert"); +const search = require("../../LeetcodeProblems/Algorithms/SearchIng_Rotated_Sorted_Array").search; var test = function() { assert.equal(search([4,5,6,7,0,1,2], 5), 1); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_II_Test.js b/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_II_Test.js index f7b27f8..933f5dd 100644 --- a/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_II_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_II_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const searchMatrix = require('../../LeetcodeProblems/Algorithms/Search_a_2D_Matrix_II').searchMatrix; +const assert = require("assert"); +const searchMatrix = require("../../LeetcodeProblems/Algorithms/Search_a_2D_Matrix_II").searchMatrix; const matrix1 = [ [1,4,7, 11,15], @@ -13,6 +13,6 @@ var test = function() { assert.equal(searchMatrix(matrix1, 5), true); assert.equal(searchMatrix(matrix1, 0), false); assert.equal(searchMatrix(matrix1, 15), true); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_Test.js b/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_Test.js index f1e2856..f5e7380 100644 --- a/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Search_a_2D_Matrix_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const searchMatrix = require('../../LeetcodeProblems/Algorithms/Search_a_2D_Matrix').searchMatrix; +const assert = require("assert"); +const searchMatrix = require("../../LeetcodeProblems/Algorithms/Search_a_2D_Matrix").searchMatrix; var test = function() { assert.equal(searchMatrix([], 0), false); @@ -7,6 +7,6 @@ var test = function() { assert.equal(searchMatrix([[1], [3]], 1), true); const matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]]; assert.equal(searchMatrix(matrix, 3), true); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Set_Matrix_Zeroes_Test.js b/LeetcodeProblemsTests/Algorithms/Set_Matrix_Zeroes_Test.js index 732c4f1..98709fe 100644 --- a/LeetcodeProblemsTests/Algorithms/Set_Matrix_Zeroes_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Set_Matrix_Zeroes_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const setZeroes = require('../../LeetcodeProblems/Algorithms/Set_Matrix_Zeroes').setZeroes; +const assert = require("assert"); +const setZeroes = require("../../LeetcodeProblems/Algorithms/Set_Matrix_Zeroes").setZeroes; var test = function() { assert.deepEqual( setZeroes([[1,1,1],[1,0,1],[1,1,1]]), [[1, 0, 1], [0, 0, 0], [1, 0, 1]] ); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js b/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js index b53eef9..cb37d91 100644 --- a/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Shuffle_String_Test.js @@ -1,10 +1,10 @@ -const assert = require('assert'); -const restoreString = require('../../LeetcodeProblems/Algorithms/Shuffle_String').restoreString; +const assert = require("assert"); +const restoreString = require("../../LeetcodeProblems/Algorithms/Shuffle_String").restoreString; var test = function () { - assert.equal(restoreString('codeleet',[4,5,6,7,0,2,1,3]),'leetcode'); - assert.equal(restoreString('abc',[0,1,2]),'abc'); - assert.equal(restoreString('hacktoberfest',[9,10,11,12,4,5,6,7,8,0,1,2,3]),'festtoberhack'); -} + assert.equal(restoreString("codeleet",[4,5,6,7,0,2,1,3]),"leetcode"); + assert.equal(restoreString("abc",[0,1,2]),"abc"); + assert.equal(restoreString("hacktoberfest",[9,10,11,12,4,5,6,7,8,0,1,2,3]),"festtoberhack"); +}; module.exports.test = test; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/Simplify_Path_Test.js b/LeetcodeProblemsTests/Algorithms/Simplify_Path_Test.js index d5d27cd..8815e3d 100644 --- a/LeetcodeProblemsTests/Algorithms/Simplify_Path_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Simplify_Path_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const simplifyPath = require('../../LeetcodeProblems/Algorithms/Simplify_Path').simplifyPath; +const assert = require("assert"); +const simplifyPath = require("../../LeetcodeProblems/Algorithms/Simplify_Path").simplifyPath; var test = function() { assert.equal(simplifyPath("/../c"), "/c"); @@ -7,7 +7,7 @@ var test = function() { assert.equal(simplifyPath("/home/"), "/home"); // => "/home" assert.equal(simplifyPath("/a/./b/../../c/"), "/c"); // => "/c" assert.equal(simplifyPath("/a/../../b/../c//.//"), "/c"); // => "/c" - assert.equal(simplifyPath("/a//b////c/d//././/.."), "/a/b/c") // => "/a/b/c" -} + assert.equal(simplifyPath("/a//b////c/d//././/.."), "/a/b/c"); // => "/a/b/c" +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Spiral_Matrix_Test.js b/LeetcodeProblemsTests/Algorithms/Spiral_Matrix_Test.js index 7ffe0f8..ff56856 100644 --- a/LeetcodeProblemsTests/Algorithms/Spiral_Matrix_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Spiral_Matrix_Test.js @@ -1,17 +1,17 @@ -const assert = require('assert'); -const spiralOrder = require('../../LeetcodeProblems/Algorithms/Spiral_Matrix').spiralOrder; +const assert = require("assert"); +const spiralOrder = require("../../LeetcodeProblems/Algorithms/Spiral_Matrix").spiralOrder; var test = function() { const matrix = [ - [ 1, 2, 3 ], - [ 4, 5, 6 ], - [ 7, 8, 9 ] - ] + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ] + ]; assert.deepEqual( spiralOrder(matrix), [1, 2, 3, 6, 9, 8, 7, 4, 5] - ) -} + ); +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Subarray_Sum_Equals_K_Test.js b/LeetcodeProblemsTests/Algorithms/Subarray_Sum_Equals_K_Test.js index b7697a6..1b3481b 100644 --- a/LeetcodeProblemsTests/Algorithms/Subarray_Sum_Equals_K_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Subarray_Sum_Equals_K_Test.js @@ -1,11 +1,11 @@ -const assert = require('assert'); -const subarraySum = require('../../LeetcodeProblems/Algorithms/Subarray_Sum_Equals_K').subarraySum; +const assert = require("assert"); +const subarraySum = require("../../LeetcodeProblems/Algorithms/Subarray_Sum_Equals_K").subarraySum; var test = function() { assert.strictEqual(subarraySum([1,1,1], 2), 2); assert.strictEqual(subarraySum([1], 0), 0); assert.strictEqual(subarraySum([0], 0), 1); assert.strictEqual(subarraySum([0,0,0,0,0], 0), 15); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Subsets_Test.js b/LeetcodeProblemsTests/Algorithms/Subsets_Test.js index 63bad19..c175b83 100644 --- a/LeetcodeProblemsTests/Algorithms/Subsets_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Subsets_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const subsets = require('../../LeetcodeProblems/Algorithms/Subsets').subsets; +const assert = require("assert"); +const subsets = require("../../LeetcodeProblems/Algorithms/Subsets").subsets; function test() { assert.deepEqual(subsets([]), [[]]); diff --git a/LeetcodeProblemsTests/Algorithms/Sum_Of_Square_Numbers_Test.js b/LeetcodeProblemsTests/Algorithms/Sum_Of_Square_Numbers_Test.js index c4f642b..26c033a 100644 --- a/LeetcodeProblemsTests/Algorithms/Sum_Of_Square_Numbers_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Sum_Of_Square_Numbers_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const judgeSquareSum = require('../../LeetcodeProblems/Algorithms/Sum_Of_Square_Numbers').judgeSquareSum; +const assert = require("assert"); +const judgeSquareSum = require("../../LeetcodeProblems/Algorithms/Sum_Of_Square_Numbers").judgeSquareSum; var test = function() { assert.strictEqual(judgeSquareSum(0), true); @@ -8,6 +8,6 @@ var test = function() { assert.strictEqual(judgeSquareSum(16), true); assert.strictEqual(judgeSquareSum(24), false); assert.strictEqual(judgeSquareSum(25), true); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Swap_Nodes_In_Pairs_Test.js b/LeetcodeProblemsTests/Algorithms/Swap_Nodes_In_Pairs_Test.js index 9a8e8f9..06df05e 100644 --- a/LeetcodeProblemsTests/Algorithms/Swap_Nodes_In_Pairs_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Swap_Nodes_In_Pairs_Test.js @@ -1,13 +1,13 @@ -const assert = require('assert'); -const ListNode = require('../../UtilsClasses/ListNode').ListNode; -const ListNodeTestHelper = require('../../UtilsClasses/ListNodeTestHelper'); -const swapPairs = require('../../LeetcodeProblems/Algorithms/Swap_Nodes_In_Pairs').swapPairs; +// const assert = require("assert"); +const ListNode = require("../../UtilsClasses/ListNode").ListNode; +const ListNodeTestHelper = require("../../UtilsClasses/ListNodeTestHelper"); +const swapPairs = require("../../LeetcodeProblems/Algorithms/Swap_Nodes_In_Pairs").swapPairs; var test = function () { ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2,3,4])), [2,1,4,3]); ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([])), []); ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1])), [1]); ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2])), [2, 1]); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Symmetric_Tree_Test.js b/LeetcodeProblemsTests/Algorithms/Symmetric_Tree_Test.js index ea8b26b..928d068 100644 --- a/LeetcodeProblemsTests/Algorithms/Symmetric_Tree_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Symmetric_Tree_Test.js @@ -1,5 +1,5 @@ var test = function() { // TODO -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Tic_Tac_Toe_Test.js b/LeetcodeProblemsTests/Algorithms/Tic_Tac_Toe_Test.js index b9656ab..d1d98ec 100644 --- a/LeetcodeProblemsTests/Algorithms/Tic_Tac_Toe_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Tic_Tac_Toe_Test.js @@ -1,14 +1,14 @@ -const TicTacToe = require('../../LeetcodeProblems/Algorithms/Tic_Tac_Toe').TicTacToe; +const TicTacToe = require("../../LeetcodeProblems/Algorithms/Tic_Tac_Toe").TicTacToe; var test = function() { - ticTacToe = new TicTacToe(); + let ticTacToe = new TicTacToe(); ticTacToe.isBoardFull(); ticTacToe.addToken(0, 1, "X"); ticTacToe.printBoard(); var iter = 0; while(iter < 8) { - const str = (iter % 2 == 0) ? "0" : "X" + const str = (iter % 2 == 0) ? "0" : "X"; ticTacToe.makeMove(str); iter++; } @@ -18,6 +18,6 @@ var test = function() { ticTacToe.addToken(0,0,"X"); ticTacToe.printBoard(); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js b/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js index c4e2f00..a51e1bf 100644 --- a/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Time_Needed_Rearrange_Binary_String_Test.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const secondsToRemoveOccurrences = require('../../LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String').secondsToRemoveOccurrences; +const assert = require("assert"); +const secondsToRemoveOccurrences = require("../../LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String").secondsToRemoveOccurrences; function test() { assert.equal(secondsToRemoveOccurrences("0110101"), 4); - assert.equal(secondsToRemoveOccurrences("11100"), 0) -}; + assert.equal(secondsToRemoveOccurrences("11100"), 0); +} -module.exports.test = test +module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js b/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js index 5900f68..5fc89a1 100644 --- a/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js @@ -1,10 +1,10 @@ -const assert = require('assert'); -const topKFrequent = require('../../LeetcodeProblems/Algorithms/Top_K_Frequent_Elements').topKFrequent; +const assert = require("assert"); +const topKFrequent = require("../../LeetcodeProblems/Algorithms/Top_K_Frequent_Elements").topKFrequent; var test = function () { assert.deepEqual(topKFrequent([1,1,1,2,2,3], 2).sort(), [1,2]); assert.deepEqual(topKFrequent([7,8,9,8,9,8], 2).sort(), [8,9]); assert.deepEqual(topKFrequent([1,1,1,1], 1).sort(), [1]); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Unique_Binary_Search_Trees_Test.js b/LeetcodeProblemsTests/Algorithms/Unique_Binary_Search_Trees_Test.js index 9e66ece..5a10c4a 100644 --- a/LeetcodeProblemsTests/Algorithms/Unique_Binary_Search_Trees_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Unique_Binary_Search_Trees_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -const numTrees1 = require('../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees').numTrees1; -const numTrees2 = require('../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees').numTrees2; -const numTrees3 = require('../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees').numTrees3; +const assert = require("assert"); +const numTrees1 = require("../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees").numTrees1; +const numTrees2 = require("../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees").numTrees2; +const numTrees3 = require("../../LeetcodeProblems/Algorithms/Unique_Binary_Search_Trees").numTrees3; var test = function () { assert.strictEqual(numTrees1(1), 1); @@ -18,6 +18,6 @@ var test = function () { assert.strictEqual(numTrees3(2), 2); assert.strictEqual(numTrees3(3), 5); assert.strictEqual(numTrees3(5), 42); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Unique_Paths_Test.js b/LeetcodeProblemsTests/Algorithms/Unique_Paths_Test.js index 05d106a..a67592a 100644 --- a/LeetcodeProblemsTests/Algorithms/Unique_Paths_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Unique_Paths_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -const uniquePaths1 = require('../../LeetcodeProblems/Algorithms/Unique_Paths').uniquePaths1; -const uniquePaths2 = require('../../LeetcodeProblems/Algorithms/Unique_Paths').uniquePaths2; -const uniquePaths3 = require('../../LeetcodeProblems/Algorithms/Unique_Paths').uniquePaths3; +const assert = require("assert"); +const uniquePaths1 = require("../../LeetcodeProblems/Algorithms/Unique_Paths").uniquePaths1; +const uniquePaths2 = require("../../LeetcodeProblems/Algorithms/Unique_Paths").uniquePaths2; +const uniquePaths3 = require("../../LeetcodeProblems/Algorithms/Unique_Paths").uniquePaths3; var test = function() { assert.strictEqual(uniquePaths1(10,4), 220); @@ -12,6 +12,6 @@ var test = function() { assert.strictEqual(uniquePaths3(10,4), 220); assert.strictEqual(uniquePaths3(3,2), 3); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Valid_Parentheses_Test.js b/LeetcodeProblemsTests/Algorithms/Valid_Parentheses_Test.js index 8e54762..a110c7b 100644 --- a/LeetcodeProblemsTests/Algorithms/Valid_Parentheses_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Valid_Parentheses_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const isValid = require('../../LeetcodeProblems/Algorithms/Valid_Parentheses').isValid; +const assert = require("assert"); +const isValid = require("../../LeetcodeProblems/Algorithms/Valid_Parentheses").isValid; var test = function () { assert.strictEqual(isValid(""), true); @@ -7,6 +7,6 @@ var test = function () { assert.strictEqual(isValid("([)]"), false); assert.strictEqual(isValid("{[()]}{[()]}"), true); assert.strictEqual(isValid("{[())()]}"), false); -} +}; module.exports.test = test; diff --git a/LeetcodeProblemsTests/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree_Test.js b/LeetcodeProblemsTests/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree_Test.js index 3dd59ef..ad60f96 100644 --- a/LeetcodeProblemsTests/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree_Test.js +++ b/LeetcodeProblemsTests/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree_Test.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const isValidSerialization = require('../../LeetcodeProblems/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree').isValidSerialization; +const assert = require("assert"); +const isValidSerialization = require("../../LeetcodeProblems/Algorithms/Verify_Preorder_Serialization_of_a_Binary_Tree").isValidSerialization; var test = function() { assert.strictEqual(isValidSerialization(""), true); diff --git a/LeetcodeProblemsTests/Algorithms/merge_k_sorted_lists_Test.js b/LeetcodeProblemsTests/Algorithms/merge_k_sorted_lists_Test.js index 4c563de..0db1c51 100644 --- a/LeetcodeProblemsTests/Algorithms/merge_k_sorted_lists_Test.js +++ b/LeetcodeProblemsTests/Algorithms/merge_k_sorted_lists_Test.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -const ListNodeTestHelper = require('../../UtilsClasses/ListNodeTestHelper'); -var ListNode = require('../../UtilsClasses/ListNode').ListNode; -const mergeKLists = require('../../LeetcodeProblems/Algorithms/merge_k_sorted_lists').mergeKLists; +const assert = require("assert"); +const ListNodeTestHelper = require("../../UtilsClasses/ListNodeTestHelper"); +var ListNode = require("../../UtilsClasses/ListNode").ListNode; +const mergeKLists = require("../../LeetcodeProblems/Algorithms/merge_k_sorted_lists").mergeKLists; function test() { assert.deepEqual(mergeKLists([]), null); From c7bf9755b55cccf322307c3689ad9a82fb10ce56 Mon Sep 17 00:00:00 2001 From: deepesh85b <115779998+deepesh85b@users.noreply.github.com> Date: Mon, 17 Oct 2022 12:24:06 +0530 Subject: [PATCH 07/22] Added Solution of Gas Station (JavaScript) --- .../Algorithms/GasStation/gasStation.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/GasStation/gasStation.js diff --git a/LeetcodeProblems/Algorithms/GasStation/gasStation.js b/LeetcodeProblems/Algorithms/GasStation/gasStation.js new file mode 100644 index 0000000..d418548 --- /dev/null +++ b/LeetcodeProblems/Algorithms/GasStation/gasStation.js @@ -0,0 +1,36 @@ +/** + * Problem: https://leetcode.com/problems/gas-station/description/ + */ +/** + * @param {number[]} gas + * @param {number[]} cost + * @return {number} + */ +var canCompleteCircuit = function(gas, cost) { + var gasSum =0, costSum = 0; + var len = gas.length; + var result = 0; + + for (var i = 0; i < len; ++i) { + gasSum += gas[i]; + costSum += cost[i]; + } + + if (costSum > gasSum) + return -1; + else { + gasSum = costSum = 0; + for (var i = 0; i < len; ++i) { + gasSum += gas[i]; + costSum += cost[i]; + if (costSum > gasSum) { + gasSum = 0; + costSum = 0; + result = i + 1; + } + } + } + + return result; +}; +module.exports = canCompleteCircuit; From 8614e0df416611fa4978cc90156bc2dce2401f02 Mon Sep 17 00:00:00 2001 From: deepesh85b <115779998+deepesh85b@users.noreply.github.com> Date: Mon, 17 Oct 2022 12:25:33 +0530 Subject: [PATCH 08/22] Create test-cases.js --- LeetcodeProblems/Algorithms/GasStation/test-cases.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/GasStation/test-cases.js diff --git a/LeetcodeProblems/Algorithms/GasStation/test-cases.js b/LeetcodeProblems/Algorithms/GasStation/test-cases.js new file mode 100644 index 0000000..a4062ba --- /dev/null +++ b/LeetcodeProblems/Algorithms/GasStation/test-cases.js @@ -0,0 +1,7 @@ +module.exports = [{ + input : [[1, 2, 3, 4], [2, 3, 4, 5]], + output : -1 +}, { + input : [[8, 2, 3, 4], [2, 3, 4, 5]], + output : 0 +}]; From 1c76f4b6106575923196a4f76f4f096cbc1281c6 Mon Sep 17 00:00:00 2001 From: deepesh85b <115779998+deepesh85b@users.noreply.github.com> Date: Mon, 17 Oct 2022 12:26:06 +0530 Subject: [PATCH 09/22] Rename gasStation.js to index.js --- .../Algorithms/GasStation/{gasStation.js => index.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename LeetcodeProblems/Algorithms/GasStation/{gasStation.js => index.js} (100%) diff --git a/LeetcodeProblems/Algorithms/GasStation/gasStation.js b/LeetcodeProblems/Algorithms/GasStation/index.js similarity index 100% rename from LeetcodeProblems/Algorithms/GasStation/gasStation.js rename to LeetcodeProblems/Algorithms/GasStation/index.js From 269678daefdc301265ffa75d28c239761795bea6 Mon Sep 17 00:00:00 2001 From: deepesh85b <115779998+deepesh85b@users.noreply.github.com> Date: Mon, 17 Oct 2022 18:00:17 +0530 Subject: [PATCH 10/22] Update index.js --- LeetcodeProblems/Algorithms/GasStation/index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/LeetcodeProblems/Algorithms/GasStation/index.js b/LeetcodeProblems/Algorithms/GasStation/index.js index d418548..b52be60 100644 --- a/LeetcodeProblems/Algorithms/GasStation/index.js +++ b/LeetcodeProblems/Algorithms/GasStation/index.js @@ -1,6 +1,20 @@ /** * Problem: https://leetcode.com/problems/gas-station/description/ */ +/* +There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i]. +You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. +You begin the journey with an empty tank at one of the gas stations. +Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, +otherwise return -1. +If there exists a solution, it is guaranteed to be unique + +Example 1: + +Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2] +Output: 3 + +*/ /** * @param {number[]} gas * @param {number[]} cost From 01f04abb9dd99295d9de10486df26f6fab3c35d2 Mon Sep 17 00:00:00 2001 From: deepesh85b <115779998+deepesh85b@users.noreply.github.com> Date: Mon, 17 Oct 2022 18:06:29 +0530 Subject: [PATCH 11/22] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6bf105e..a88abec 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ To run a specific problem in your console run `node ` (e.g. | [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ | | [Minimize Maximum Pair Sum in Array ](/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js) | Medium | https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ | | [Top K Frequent Elements ](/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js) | Medium | https://leetcode.com/problems/top-k-frequent-elements/ | +| [Gas Station](/LeetcodeProblems/Algorithms/Gas Station/index.js) | Medium | https://leetcode.com/problems/gas-station/description/| | [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ | | [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ | | [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ | From 89f0d89b0afc547a689cebe51068aa54e737e58b Mon Sep 17 00:00:00 2001 From: deepesh85b <115779998+deepesh85b@users.noreply.github.com> Date: Mon, 17 Oct 2022 18:08:21 +0530 Subject: [PATCH 12/22] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a88abec..262e475 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ To run a specific problem in your console run `node ` (e.g. | [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ | | [Minimize Maximum Pair Sum in Array ](/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js) | Medium | https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ | | [Top K Frequent Elements ](/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js) | Medium | https://leetcode.com/problems/top-k-frequent-elements/ | -| [Gas Station](/LeetcodeProblems/Algorithms/Gas Station/index.js) | Medium | https://leetcode.com/problems/gas-station/description/| +| [Gas Station](/LeetcodeProblems/Algorithms/GasStation/index.js) | Medium | https://leetcode.com/problems/gas-station/description/| | [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ | | [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ | | [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ | From 1aa5cfae7eb3e6096eb979e8406b7cbf0a9b0328 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Cardarello Date: Mon, 17 Oct 2022 12:08:02 -0700 Subject: [PATCH 13/22] Update LeetcodeProblems/Algorithms/GasStation/index.js --- LeetcodeProblems/Algorithms/GasStation/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LeetcodeProblems/Algorithms/GasStation/index.js b/LeetcodeProblems/Algorithms/GasStation/index.js index b52be60..56fd29c 100644 --- a/LeetcodeProblems/Algorithms/GasStation/index.js +++ b/LeetcodeProblems/Algorithms/GasStation/index.js @@ -34,7 +34,7 @@ var canCompleteCircuit = function(gas, cost) { return -1; else { gasSum = costSum = 0; - for (var i = 0; i < len; ++i) { + for (i = 0; i < len; ++i) { gasSum += gas[i]; costSum += cost[i]; if (costSum > gasSum) { From 90218049a8753681d9a0a8f41c7c9cc9df7c3cc4 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Cardarello Date: Mon, 17 Oct 2022 12:10:56 -0700 Subject: [PATCH 14/22] Add linter to the README --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 262e475..d82a457 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,11 @@ Solutions of algorithm problems using Javascript. https://ignacio-chiazzo.githu The solutions are located under `/LeetcodeProblems`. Each problem has a test file located under `/LeetcodeProblemsTest`. ### Run Tests -To run all the test run `node Test.js` in the console. -To run a specific problem in your console run `node ` (e.g. `node LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js`). +**Unit tests:** To run all the test run `node Test.js` in the console. To run a specific problem in your console run `node ` (e.g. `node LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js`). + +**Linter:** This repository uses [`es-lint`](https://eslint.org/docs/latest/user-guide/command-line-interface). To run all the tests you would need to install the packages by running `npm install` followed by `npx eslint LeetcodeProblems LeetcodeProblemsTests` which will run the eslint in all problems and tests. + ### Leetcode Problems From c9f1a87cf70fe9c155428dee55a7f3e8f6dbf210 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Cardarello Date: Tue, 18 Oct 2022 07:13:18 -0700 Subject: [PATCH 15/22] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index d82a457..2014395 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil **Unit tests:** To run all the test run `node Test.js` in the console. To run a specific problem in your console run `node ` (e.g. `node LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js`). -**Linter:** This repository uses [`es-lint`](https://eslint.org/docs/latest/user-guide/command-line-interface). To run all the tests you would need to install the packages by running `npm install` followed by `npx eslint LeetcodeProblems LeetcodeProblemsTests` which will run the eslint in all problems and tests. - +**Linter:** This repository uses [`es-lint`](https://eslint.org/docs/latest/user-guide/command-line-interface). To run all the tests you would need to install the packages by running `npm install` followed by `npx eslint LeetcodeProblems LeetcodeProblemsTests` which will run the eslint in all problems and tests. You can also use the [flag `--fix`](https://eslint.org/docs/latest/user-guide/command-line-interface#fixing-problems) which will automatically fix some of the errors. ### Leetcode Problems From a2a2a238012af1378151679c9b9becd9c7f975fa Mon Sep 17 00:00:00 2001 From: Kavya Sharma <86572886+kav1239@users.noreply.github.com> Date: Sat, 22 Oct 2022 15:37:36 +0530 Subject: [PATCH 16/22] added Maximal_square.js file --- Maximal_square.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Maximal_square.js diff --git a/Maximal_square.js b/Maximal_square.js new file mode 100644 index 0000000..ee9a897 --- /dev/null +++ b/Maximal_square.js @@ -0,0 +1,53 @@ +/* +Maximal Area of Square + +Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. + +Example 1 Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] +Output: 4 + + +Example 2 Input: matrix = [["0","1"],["1","0"]] +Output: 1 + + +Example 3 Input: matrix = [["0"]] +Output: 0 + + +*/ + +/** + * @param {character[][]} matrix + * @return {number} + */ +var maximalSquare = function(matrix) { + var m = matrix.length; + var n = (matrix[0] || []).length; + var dp = Array(m).fill(0).map(_ => Array(n)); + var max = 0; + + for (var k = 0; k < m; k++) { + dp[k][0] = matrix[k][0] === '1' ? 1 : 0; + max = Math.max(max, dp[k][0]); + } + + for (var p = 0; p < n; p++) { + dp[0][p] = matrix[0][p] === '1' ? 1 : 0; + max = Math.max(max, dp[0][p]); + } + + for (var i = 1; i < m; i++) { + for (var j = 1; j < n; j++) { + if (matrix[i][j] === '1') { + dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1; + max = Math.max(max, dp[i][j]); + } else { + dp[i][j] = 0; + } + } + } + + return max * max; + +}; From 9669bf80d6888b0c748dd6d6fbd62794b8c545a6 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Cardarello Date: Sat, 22 Oct 2022 11:17:14 -0400 Subject: [PATCH 17/22] Apply suggestions from code review --- Maximal_square.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Maximal_square.js b/Maximal_square.js index ee9a897..0cb14e8 100644 --- a/Maximal_square.js +++ b/Maximal_square.js @@ -1,4 +1,5 @@ /* +https://leetcode.com/problems/maximal-square Maximal Area of Square Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. @@ -22,7 +23,7 @@ Output: 0 * @return {number} */ var maximalSquare = function(matrix) { - var m = matrix.length; + var m = matrix.length; var n = (matrix[0] || []).length; var dp = Array(m).fill(0).map(_ => Array(n)); var max = 0; From 84d148f13f33edc63ef0de638a0fe4d7c9694862 Mon Sep 17 00:00:00 2001 From: Kavya Sharma <86572886+kav1239@users.noreply.github.com> Date: Sat, 22 Oct 2022 15:56:47 +0530 Subject: [PATCH 18/22] added Maximal_square_Test.js file --- Maximal_square_Test.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Maximal_square_Test.js diff --git a/Maximal_square_Test.js b/Maximal_square_Test.js new file mode 100644 index 0000000..edadb2b --- /dev/null +++ b/Maximal_square_Test.js @@ -0,0 +1,37 @@ +const assert = require('assert'); +const {Maximalsquare } = require('../LeetcodeProblems/Maximal_square'); + +function test1() { + var matrix = [ + [1, 0, 1, 0, 0], + [1, 0, 1, 1, 1], + [1, 1, 1, 1 1], + [1, 0, 0, 1, 0], + ] + + assert.strictEqual(Maximalsquare(matrix), 4); +} + +function test2() { + var matrix = [ + [0, 1], + [1,0] + ] + + assert.strictEqual(Maximalsquare(matrix), 1); +} + +function test3(){ + var matrix = [ + [0] + ] + assert.strictEqual(Maximalsquare(matrix), 0); +} + +function test() { + test1(); + test2(); + test3(); +} + +module.exports.test = test From d563119e3f943ace68e3b96880bf4332cf4a5f1e Mon Sep 17 00:00:00 2001 From: Kavya Sharma <86572886+kav1239@users.noreply.github.com> Date: Sat, 22 Oct 2022 16:13:55 +0530 Subject: [PATCH 19/22] readme.md created --- readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 readme.md diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..f2e8f80 --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ + [Maximal Square ](/LeetcodeProblems/Maximal_Square.js) | Medium | https://leetcode.com/problems/maximal-square/ From 47220c65a502c125944dfdca09a79dc3bb9b9db5 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Cardarello Date: Sun, 23 Oct 2022 08:06:17 -0400 Subject: [PATCH 20/22] Revert "readme.md created" This reverts commit d563119e3f943ace68e3b96880bf4332cf4a5f1e. --- readme.md | 1 - 1 file changed, 1 deletion(-) delete mode 100644 readme.md diff --git a/readme.md b/readme.md deleted file mode 100644 index f2e8f80..0000000 --- a/readme.md +++ /dev/null @@ -1 +0,0 @@ - [Maximal Square ](/LeetcodeProblems/Maximal_Square.js) | Medium | https://leetcode.com/problems/maximal-square/ From b4ac44358ee63ba6ab8530cdac7051852f82b4c0 Mon Sep 17 00:00:00 2001 From: ignacio-chiazzo Date: Sun, 23 Oct 2022 08:16:21 -0400 Subject: [PATCH 21/22] Move Gas station problem to algorithms problem --- LeetcodeProblems/Algorithms/GasStation/test-cases.js | 7 ------- .../{GasStation/index.js => Gas_Station.js} | 3 ++- LeetcodeProblemsTests/Algorithms/Gas_Station_Test.js | 11 +++++++++++ 3 files changed, 13 insertions(+), 8 deletions(-) delete mode 100644 LeetcodeProblems/Algorithms/GasStation/test-cases.js rename LeetcodeProblems/Algorithms/{GasStation/index.js => Gas_Station.js} (95%) create mode 100644 LeetcodeProblemsTests/Algorithms/Gas_Station_Test.js diff --git a/LeetcodeProblems/Algorithms/GasStation/test-cases.js b/LeetcodeProblems/Algorithms/GasStation/test-cases.js deleted file mode 100644 index a4062ba..0000000 --- a/LeetcodeProblems/Algorithms/GasStation/test-cases.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = [{ - input : [[1, 2, 3, 4], [2, 3, 4, 5]], - output : -1 -}, { - input : [[8, 2, 3, 4], [2, 3, 4, 5]], - output : 0 -}]; diff --git a/LeetcodeProblems/Algorithms/GasStation/index.js b/LeetcodeProblems/Algorithms/Gas_Station.js similarity index 95% rename from LeetcodeProblems/Algorithms/GasStation/index.js rename to LeetcodeProblems/Algorithms/Gas_Station.js index 56fd29c..f7fe18c 100644 --- a/LeetcodeProblems/Algorithms/GasStation/index.js +++ b/LeetcodeProblems/Algorithms/Gas_Station.js @@ -47,4 +47,5 @@ var canCompleteCircuit = function(gas, cost) { return result; }; -module.exports = canCompleteCircuit; + +module.exports.canCompleteCircuit = canCompleteCircuit; \ No newline at end of file diff --git a/LeetcodeProblemsTests/Algorithms/Gas_Station_Test.js b/LeetcodeProblemsTests/Algorithms/Gas_Station_Test.js new file mode 100644 index 0000000..ef35fe6 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/Gas_Station_Test.js @@ -0,0 +1,11 @@ +const assert = require("assert"); +const canCompleteCircuit = require("../../LeetcodeProblems/Algorithms/Gas_Station").canCompleteCircuit; + +function test() { + assert.equal(3, canCompleteCircuit([1,2,3,4,5], [3,4,5,1,2])); + assert.equal(-1, canCompleteCircuit([1,2,3,4], [2,3,4,5])); + assert.equal(0, canCompleteCircuit([8,2,3,4], [2,3,4,5])); +} + +module.exports.test = test; + From a3d646a81413da25d417ab89c5436694c83854e5 Mon Sep 17 00:00:00 2001 From: hot9cups Date: Sun, 23 Oct 2022 13:37:56 +0530 Subject: [PATCH 22/22] Enhancing Tests.js logic - Modified Tests.js to get rid of hardcoding and work for files having variable number of test methods. Now any number of tests can be exported and can also be named anything(As opposed to being 'test' from earlier) --- LeetcodeProblemsTests/Algorithms/2Sum_Test.js | 9 ++++++--- Test.js | 20 ++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js index 132b3db..19f8ce0 100644 --- a/LeetcodeProblemsTests/Algorithms/2Sum_Test.js +++ b/LeetcodeProblemsTests/Algorithms/2Sum_Test.js @@ -3,15 +3,18 @@ const twoSum = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum; const twoSum2 = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum2; -var test = function () { +var testFirstSolution = function () { assert.deepEqual([0,1], twoSum([2,7,11,15], 9)); assert.deepEqual([1,2], twoSum([3,2,4], 6)); assert.deepEqual([0,1], twoSum([3,3], 6)); - +}; + +var testSecondSolution = function() { assert.deepEqual([0,1], twoSum2([2,7,11,15], 9)); assert.deepEqual([1,2], twoSum2([3,2,4], 6)); assert.deepEqual([0,1], twoSum2([3,3], 6)); }; -module.exports.test = test; +module.exports.testFirstSolution = testFirstSolution; +module.exports.testSecondSolution = testSecondSolution; diff --git a/Test.js b/Test.js index 185ebe3..27850e4 100644 --- a/Test.js +++ b/Test.js @@ -10,19 +10,15 @@ var test_all = async function () { const problems = await loadProblems(); for (i in problems) { console.log("Solving: " + problems[i]); - const problem = require(TESTS_FOLDER + problems[i]); - - if (typeof problem.test !== "undefined") { - problem.test(); - console.log("✅ Tests for " + problems[i] + " run successfully \n"); - } else { - console.warn( - problem, - "🔴 The problem " + - problems[i] + - " doesn't have a test method implemented.\n" - ); + const tests = require(TESTS_FOLDER + problems[i]); + if (Object.keys(tests).length == 0) { + console.warn("🔴 The problem " + problems[i] + " doesn't have a test method implemented.\n"); + continue; + } + for(testIdx in tests) { + tests[testIdx](); } + console.log("✅ Tests for " + problems[i] + " run successfully \n"); } } catch (error) { throw new Error(error);