Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit acb679b

Browse files
Rename problems and fix identation
1 parent b19d63c commit acb679b

31 files changed

+189
-255
lines changed
File renamed without changes.

LeetcodeProblems/CoinChange.js renamed to LeetcodeProblems/Coin_Change.js

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,20 @@
11
/*
2+
Coin Change
3+
https://leetcode.com/problems/coin-change/
24
3-
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/description/
4-
5-
862. Shortest Subarray with Sum at Least K
6-
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
7-
8-
If there is no non-empty subarray with sum at least K, return -1.
9-
10-
5+
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
116
127
Example 1:
138
14-
Input: A = [1], K = 1
15-
Output: 1
9+
Input: coins = [1, 2, 5], amount = 11
10+
Output: 3
11+
Explanation: 11 = 5 + 5 + 1
1612
Example 2:
1713
18-
Input: A = [1,2], K = 4
14+
Input: coins = [2], amount = 3
1915
Output: -1
20-
Example 3:
21-
22-
Input: A = [2,-1,2], K = 3
23-
Output: 3
24-
25-
2616
Note:
27-
28-
1 <= A.length <= 50000
29-
-10 ^ 5 <= A[i] <= 10 ^ 5
30-
1 <= K <= 10 ^ 9
31-
*/
32-
33-
34-
/*
35-
Tree for solution
36-
37-
38-
[pos, countOfNumbers, K]
39-
[pos + 1, countOfNumbers, K] [pos + 1, countOfNumbers + 1, K - nums[pos]] [pos, countOfNumbers + 1, K - nums[pos]]
40-
...
41-
42-
[0, 0, 11] // [pos, countOfNumbers, K]
43-
/ | \
44-
[1, 0, 11] [1, 1, 10] [0, 1, 10]
45-
/ | \ / | \ / | \
46-
[2, 0, 11] [2, 1, 9] [1, 1, 9] [2, 0, 10] [2, 2, 8] [1, 2, 8] [1, 1, 10] [1, 2, 9] [0, 2, 9]
47-
...
17+
You may assume that you have an infinite number of each kind of coin.
4818
*/
4919

5020
// Solution 3
@@ -132,24 +102,7 @@ var min = function(a, b, c) {
132102
return (b < c) ? b : c;
133103
}
134104

135-
136105
function main() {
137-
// console.log("-------------");
138-
// console.log("Approach 1")
139-
// console.log(coinChange1([], 3));
140-
// console.log(coinChange1([2], 3));
141-
// console.log(coinChange1([1, 2, 5], 11));
142-
// console.log(coinChange1([3,7,405,436], 8839));
143-
// // console.log(coinChange1([370,417,408,156,143,434,168,83,177,280,117], 9953)); takes forever
144-
145-
// console.log("-------------");
146-
// console.log("Approach 2")
147-
// console.log(coinChange2([], 3));
148-
// console.log(coinChange2([2], 3));
149-
// console.log(coinChange2([1, 2, 5], 11));
150-
// console.log(coinChange2([3,7,405,436], 8839));
151-
// console.log(coinChange2([370,417,408,156,143,434,168,83,177,280,117], 9953));
152-
153106
console.log("-------------");
154107
console.log("Approach 3")
155108
console.log(coinChange3([], 3));

LeetcodeProblems/design-circular-deque.js renamed to LeetcodeProblems/Design_Circular_Deque.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
Design Circular Deque
23
https://leetcode.com/problems/design-circular-deque/description/
34
45
Design your implementation of the circular double-ended queue (deque).
@@ -145,5 +146,5 @@ var main = function(){
145146
console.log(obj.insertFront(4));
146147
console.log(obj.getFront());
147148
}
148-
main();
149+
149150
module.exports.main = main;

LeetcodeProblems/Escape-The-Ghosts.js renamed to LeetcodeProblems/Escape_The_Ghosts.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/*
2+
Escape The Ghosts
23
https://leetcode.com/problems/escape-the-ghosts/description/
34
4-
789. Escape The Ghosts
5-
65
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]).
76
87
Each turn, you and all ghosts simultaneously *may* move in one of 4 cardinal directions: north, east, west, or south, going from the previous point to a new point 1 unit of distance away.

LeetcodeProblems/FloodFill.js renamed to LeetcodeProblems/Flood_Fill.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
Flood Fill
23
https://leetcode.com/problems/flood-fill/description/
34
45
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

LeetcodeProblems/GenerateParentheses.js renamed to LeetcodeProblems/Generate_Parentheses.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
Generate Parentheses
23
https://leetcode.com/problems/generate-parentheses
34
45
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
@@ -31,7 +32,6 @@ var generateParenthesesApproach1 = function(n) {
3132
@param {string} leftParenthesis Amount for parenthesis left to be added.
3233
@param {[string]} sol array that contains the solution found so far.
3334
*/
34-
3535
var genParAux = function(str, position, leftParentheses, sol) {
3636
if(position === str.length) {
3737
var ret = str + ")".repeat(leftParentheses);

LeetcodeProblems/GroupAnagrams.js renamed to LeetcodeProblems/Group_Anagrams.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
Group Anagrams
23
https://leetcode.com/problems/group-anagrams/description/
34
45
Given an array of strings, group anagrams together.

LeetcodeProblems/linked-list-cycle-ii.js renamed to LeetcodeProblems/Linked_List_Cycle_II.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
Linked List Cycle
23
https://leetcode.com/problems/linked-list-cycle-ii/description/
34
45
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
@@ -9,6 +10,7 @@ Follow up:
910
Can you solve it without using extra space?
1011
*/
1112

13+
var ListNode = require('../utilsClasses/ListNode').ListNode;
1214

1315
// Optimal solution
1416
/**
@@ -59,15 +61,9 @@ var main = function() {
5961
const head = buildCycle();
6062
console.log(detectCycle(head));
6163
}
62-
main();
63-
64-
function ListNode(val) {
65-
this.val = val;
66-
this.next = null;
67-
}
6864

6965
function buildCycle() {
70-
var node1 = new ListNode(1);
66+
var node1 = ListNode.linkenList([1,2,3,4,5]);
7167
var node2 = new ListNode(2);
7268
var node3 = new ListNode(3);
7369
var node4 = new ListNode(4);
@@ -78,13 +74,7 @@ function buildCycle() {
7874
node3.next = node4;
7975
node4.next = node5;
8076
node5.next = node2;
81-
82-
/* 1 -> 2 -> 3 -> 4 -> 5
83-
\ /
84-
- - - - -
85-
*/
8677
return node1;
8778
}
8879

89-
main();
9080
module.exports.main = main;

LeetcodeProblems/Longest_Consecutive_Sequence.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Longest Consecutive Sequence
3-
43
https://leetcode.com/problems/longest-consecutive-sequence/
54
65
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
@@ -53,7 +52,6 @@ var longestConsecutive = function(nums) {
5352
return cons;
5453
};
5554

56-
5755
var main = function() {
5856
console.log(longestConsecutive([100, 1, 200, 3, 2, 400, 201]));
5957
console.log(longestConsecutive([1,2,3,4, 100, 1, 200, 3, 2, 400, 201]));

LeetcodeProblems/Longest_Palindromic_Substring.js

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
2-
https://leetcode.com/problems/longest-palindromic-substring/description/
3-
42
Longest Palindromic Substring
3+
https://leetcode.com/problems/longest-palindromic-substring/description/
54
65
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
76
@@ -16,7 +15,6 @@ Input: "cbbd"
1615
Output: "bb"
1716
*/
1817

19-
2018
/**
2119
* @param {string} s
2220
* @return {string}
@@ -25,46 +23,45 @@ var longestPalindrome = function(str) {
2523
if(str.length == 0)
2624
return "";
2725

28-
var maxPal = 1;
26+
var maxPal = 1;
2927
var posPalStart = 0;
30-
var currentPalStart = 0;
28+
var currentPalStart = 0;
3129

3230
for(var i = 1; i < str.length; i++) {
33-
if(str.charAt(i - 1) == str.charAt(i)) {
34-
currentPalStart = i - 1;
35-
var currentPal = 2;
36-
var iter = 1;
37-
while(i - iter - 1 >= 0 && i + iter < str.length &&
38-
str.charAt(i - iter - 1) == str.charAt(i + iter)) {
39-
currentPalStart = i - iter - 1;
40-
iter++;
41-
currentPal += 2;
42-
}
43-
}
44-
if(currentPal > maxPal) {
45-
maxPal = currentPal;
46-
posPalStart = currentPalStart;
47-
}
31+
if(str.charAt(i - 1) == str.charAt(i)) {
32+
currentPalStart = i - 1;
33+
var currentPal = 2;
34+
var iter = 1;
35+
while(i - iter - 1 >= 0 && i + iter < str.length &&
36+
str.charAt(i - iter - 1) == str.charAt(i + iter)) {
37+
currentPalStart = i - iter - 1;
38+
iter++;
39+
currentPal += 2;
40+
}
41+
}
42+
if(currentPal > maxPal) {
43+
maxPal = currentPal;
44+
posPalStart = currentPalStart;
45+
}
46+
}
47+
48+
for(var i = 1; i < str.length - 1; i++) {
49+
if(str.charAt(i - 1) == str.charAt(i + 1)) {
50+
currentPal = 1;
51+
var iter = 1;
52+
while(i - iter >= 0 && i + iter < str.length && str.charAt(i - iter) == str.charAt(i + iter)) {
53+
currentPalStart = i - iter;
54+
iter++;
55+
currentPal += 2;
56+
}
4857
}
49-
50-
for(var i = 1; i < str.length - 1; i++) {
51-
if(str.charAt(i - 1) == str.charAt(i + 1)) {
52-
currentPal = 1;
53-
var iter = 1;
54-
while(i - iter >= 0 && i + iter < str.length &&
55-
str.charAt(i - iter) == str.charAt(i + iter)) {
56-
currentPalStart = i - iter;
57-
iter++;
58-
currentPal += 2;
59-
}
60-
}
61-
if(currentPal > maxPal) {
62-
maxPal = currentPal;
63-
posPalStart = currentPalStart;
64-
}
58+
if(currentPal > maxPal) {
59+
maxPal = currentPal;
60+
posPalStart = currentPalStart;
6561
}
66-
67-
return str.slice(posPalStart, posPalStart + maxPal);
62+
}
63+
64+
return str.slice(posPalStart, posPalStart + maxPal);
6865
}
6966

7067
var main = function() {
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Lowest Common Ancestor of a Binary Tree
3-
43
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
54
65
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
@@ -33,53 +32,56 @@ All of the nodes' values will be unique.
3332
p and q are different and both values will exist in the binary tree.
3433
*/
3534

35+
// Solution 1
3636
var lowestCommonAncestor = function(root, p, q) {
37-
if(root === null) {
37+
if(root === null)
3838
return root;
39-
}
40-
if(p.val === root.val) {
39+
if(p.val === root.val)
4140
return p;
42-
}
43-
if(q.val === root.val) {
41+
if(q.val === root.val)
4442
return q;
45-
}
46-
if(lowestCommonAncestor(root.left, p, q));
43+
44+
const left = lowestCommonAncestor(root.left, p, q);
4745
const right = lowestCommonAncestor(root.right, p, q);
48-
if(left !== null && right !== null) {
46+
if(left !== null && right !== null)
4947
return root;
50-
}
5148
return left !== null ? left : right;
5249
};
5350

51+
52+
// Solution 2
5453
var lowestCommonAncestor1 = function(root, p, q) {
5554
const pathToP = pathTo(root, p.val);
56-
const pathToQ = pathTo(root, q.val);
57-
var elem = null;
58-
for(var i = 0; i < Math.min(pathToP.length, pathToQ.length); i++) {
59-
if(pathToP[i] !== pathToQ[i]) {
60-
return pathToP[i - 1];
61-
}
62-
}
55+
const pathToQ = pathTo(root, q.val);
56+
57+
if(pathToP === null || pathToQ === null)
58+
return null;
59+
60+
if(pathToP.length === 1)
61+
return pathToP[0];
62+
if(pathToQ.length === 1)
63+
return pathToQ[0];
64+
65+
var iter = 0;
66+
while(pathToP[iter + 1] === pathToQ[iter + 1])
67+
iter++
6368

64-
return elem;
69+
return pathToP[iter];
6570
};
6671

6772
var pathTo = function(root, a) {
68-
if(root === null) {
69-
return null;
70-
}
71-
if(root.val === a) {
72-
return [root.val];
73-
}
74-
const left = pathTo(root.left, a);
75-
if (left !== null) {
76-
return [root.val] + left;
77-
}
78-
const right = pathTo(root.right, a);
79-
if(right !== null) {
80-
return [root.val] + right;
81-
}
73+
if(root === null)
8274
return null;
83-
}
75+
if(root.val === a)
76+
return [root.val];
8477

78+
const left = pathTo(root.left, a);
79+
if (left !== null)
80+
return [root.val].concat(left);
8581

82+
const right = pathTo(root.right, a);
83+
if(right !== null)
84+
return [root.val].concat(right);
85+
86+
return null;
87+
}

0 commit comments

Comments
 (0)