File tree 3 files changed +72
-0
lines changed
3 files changed +72
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 시간복잡도: O(n)
2
+ // 공간복잡도: O(1)
3
+
4
+ /**
5
+ * @param {number } n
6
+ * @return {number }
7
+ */
8
+ var climbStairs = function ( n ) {
9
+ if ( n <= 2 ) return n ;
10
+ let prev1 = 1 , prev2 = 2 ;
11
+ for ( let i = 3 ; i <= n ; i ++ ) {
12
+ let curr = prev1 + prev2 ;
13
+ prev1 = prev2 ;
14
+ prev2 = curr ;
15
+ }
16
+ return prev2 ;
17
+ } ;
18
+
Original file line number Diff line number Diff line change
1
+ // 시간복잡도: O(n)
2
+ // 공간복잡도: O(n)
3
+
4
+ /**
5
+ * @param {string } s
6
+ * @return {number }
7
+ */
8
+ var numDecodings = function ( s ) {
9
+ const memo = { } ;
10
+
11
+ const helper = ( index ) => {
12
+ if ( index === s . length ) return 1 ;
13
+ if ( s [ index ] === '0' ) return 0 ;
14
+ if ( memo [ index ] !== undefined ) return memo [ index ] ;
15
+
16
+ let ways = helper ( index + 1 ) ;
17
+ if ( index < s . length - 1 && parseInt ( s . slice ( index , index + 2 ) ) <= 26 ) {
18
+ ways += helper ( index + 2 ) ;
19
+ }
20
+
21
+ memo [ index ] = ways ;
22
+ return ways ;
23
+ } ;
24
+
25
+ return helper ( 0 ) ;
26
+ } ;
27
+
Original file line number Diff line number Diff line change
1
+ // 시간복잡도: O(n)
2
+ // 공간복잡도: O(1)
3
+
4
+ /**
5
+ * @param {string } s
6
+ * @param {string } t
7
+ * @return {boolean }
8
+ */
9
+ var isAnagram = function ( s , t ) {
10
+ if ( s . length !== t . length ) return false ;
11
+
12
+ const charCount = { } ;
13
+
14
+ for ( const char of s ) {
15
+ charCount [ char ] = ( charCount [ char ] || 0 ) + 1 ;
16
+ }
17
+
18
+ for ( const char of t ) {
19
+ if ( ! charCount [ char ] ) {
20
+ return false ;
21
+ }
22
+ charCount [ char ] -- ;
23
+ }
24
+
25
+ return true ;
26
+ } ;
27
+
You can’t perform that action at this time.
0 commit comments