Skip to content

Commit 16f4e07

Browse files
2 parents 669afba + 13d4b8e commit 16f4e07

File tree

6 files changed

+83
-6
lines changed

6 files changed

+83
-6
lines changed

concepts/Currying.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,4 @@ const sum = a => {
131131

132132
// Simplified to one line
133133
const sum = a => b => b ? sum(a + b) : a;
134-
```
134+
```

exercises/leetcode/groupAnagrams.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ const groupAnagrams = strs => {
3232
}
3333

3434
return Array.from(anagramMap.values());
35-
}
35+
}
36+
37+
/**
38+
* Time Complexity: O(NKlogK) - Outer Loop : O(N) and string sort O(KlogK) where K is max length
39+
* of a string in strs
40+
* Space Complexity: O(NK) (N elements with string of size K stored ?)
41+
*/

exercises/leetcode/rotateImage.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* You are given an n x n 2D matrix representing an image.
3+
4+
Rotate the image by 90 degrees (clockwise).
5+
6+
Note:
7+
8+
You have to rotate the image in-place, which means you have to modify the
9+
input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
10+
11+
Example 1:
12+
13+
Given input matrix =
14+
[
15+
[1,2,3],
16+
[4,5,6],
17+
[7,8,9]
18+
],
19+
20+
rotate the input matrix in-place such that it becomes:
21+
[
22+
[7,4,1],
23+
[8,5,2],
24+
[9,6,3]
25+
]
26+
Example 2:
27+
28+
Given input matrix =
29+
[
30+
[ 5, 1, 9,11],
31+
[ 2, 4, 8,10],
32+
[13, 3, 6, 7],
33+
[15,14,12,16]
34+
],
35+
36+
rotate the input matrix in-place such that it becomes:
37+
[
38+
[15,13, 2, 5],
39+
[14, 3, 4, 1],
40+
[12, 6, 8, 9],
41+
[16, 7,10,11]
42+
]
43+
*/
44+
45+
const rotate = matrix => {
46+
const step = matrix.length - 1;
47+
48+
for (let i = 0; i < (matrix.length / 2); i++) {
49+
for (let j = 0; j < (matrix.length - 1) / 2; j++) {
50+
let temp = matrix[i][j];
51+
matrix[i][j] = matrix[step - j][i];
52+
matrix[step - j][i] = matrix[step - i][step - j];
53+
matrix[step - i][step - j] = matrix[j][step - i];
54+
matrix[j][step - i] = temp;
55+
}
56+
}
57+
}

exercises/leetcode/validParenthesis.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ const validParenthesis = str => {
3434
}
3535

3636
return strStack.length === 0;
37-
}
37+
}
38+
/**
39+
* Time Complexity: O(n)
40+
* Space Complexity: O(n)
41+
*/

exercises/strings/reverseEachWord.js

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ var reverseWords = function(s) {
6565
return s.split(/\s+/).reverse().join(' ').trim();
6666
};
6767

68+
/**
69+
* Time Complexity: O(N)
70+
* Space Complexity: O(N)
71+
*/
72+
6873
/**
6974
* Without using built in functions
7075
* @param {string} s

exercises/strings/reverseString2.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,20 @@ const reverseEachWord = str => {
3030
while(end < n && str[end] !== ' ') {
3131
end++;
3232
}
33-
reverse(str, start, end);
33+
reverse(str, start, end - 1);
3434
start = end + 1;
3535
end++;
3636
}
3737
}
3838

3939
const reverseWords = s => {
4040
// reverse the string
41-
s = reverse(s, 0, s.length - 1);
41+
reverse(s, 0, s.length - 1);
4242
// reverse each word
4343
reverseEachWord(s);
44-
}
44+
}
45+
46+
/**
47+
* Time Complexity: O(N);
48+
* Space Complexity: O(1);
49+
*/

0 commit comments

Comments
 (0)