File tree 6 files changed +83
-6
lines changed
6 files changed +83
-6
lines changed Original file line number Diff line number Diff line change @@ -131,4 +131,4 @@ const sum = a => {
131
131
132
132
// Simplified to one line
133
133
const sum = a => b => b ? sum (a + b) : a;
134
- ```
134
+ ```
Original file line number Diff line number Diff line change @@ -32,4 +32,10 @@ const groupAnagrams = strs => {
32
32
}
33
33
34
34
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
+ */
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -34,4 +34,8 @@ const validParenthesis = str => {
34
34
}
35
35
36
36
return strStack . length === 0 ;
37
- }
37
+ }
38
+ /**
39
+ * Time Complexity: O(n)
40
+ * Space Complexity: O(n)
41
+ */
Original file line number Diff line number Diff line change @@ -65,6 +65,11 @@ var reverseWords = function(s) {
65
65
return s . split ( / \s + / ) . reverse ( ) . join ( ' ' ) . trim ( ) ;
66
66
} ;
67
67
68
+ /**
69
+ * Time Complexity: O(N)
70
+ * Space Complexity: O(N)
71
+ */
72
+
68
73
/**
69
74
* Without using built in functions
70
75
* @param {string } s
Original file line number Diff line number Diff line change @@ -30,15 +30,20 @@ const reverseEachWord = str => {
30
30
while ( end < n && str [ end ] !== ' ' ) {
31
31
end ++ ;
32
32
}
33
- reverse ( str , start , end ) ;
33
+ reverse ( str , start , end - 1 ) ;
34
34
start = end + 1 ;
35
35
end ++ ;
36
36
}
37
37
}
38
38
39
39
const reverseWords = s => {
40
40
// reverse the string
41
- s = reverse ( s , 0 , s . length - 1 ) ;
41
+ reverse ( s , 0 , s . length - 1 ) ;
42
42
// reverse each word
43
43
reverseEachWord ( s ) ;
44
- }
44
+ }
45
+
46
+ /**
47
+ * Time Complexity: O(N);
48
+ * Space Complexity: O(1);
49
+ */
You can’t perform that action at this time.
0 commit comments