File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n
3+ * @return {number }
4+ */
5+ let climbStairs = function ( n ) {
6+ if ( n <= 1 ) return 1 ;
7+
8+ let ways = new Array ( n + 1 ) ;
9+ ways [ 0 ] = 1 ;
10+ ways [ 1 ] = 1 ;
11+
12+ for ( let i = 2 ; i <= n ; i ++ ) {
13+ ways [ i ] = ways [ i - 1 ] + ways [ i - 2 ] ; // 점화식 사용
14+ }
15+
16+ return ways [ n ] ;
17+ } ;
18+
19+ /*
20+ 1. 시간 복잡도: O(n)
21+ - for 루프의 시간 복잡도
22+ 2. 공간 복잡도: O(n)
23+ - 배열 ways의 공간 복잡도
24+ */
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @param {number } target
4+ * @return {number[] }
5+ */
6+ let twoSum = function ( nums , target ) {
7+ let indices = { } ;
8+
9+ nums . forEach ( ( item , index ) => {
10+ indices [ item ] = index ;
11+ } ) ;
12+
13+ for ( let i = 0 ; i < nums . length ; i ++ ) {
14+ let findNum = target - nums [ i ] ;
15+
16+ if ( indices [ findNum ] !== i && findNum . toString ( ) in indices ) {
17+ return [ indices [ findNum ] , i ] ;
18+ }
19+ }
20+ } ;
21+
22+ /*
23+ 1. 시간복잡도: O(n)
24+ - forEach와 for루프의 시간복잡도가 각 O(n)
25+ 2. 공간복잡도: O(n)
26+ - indices 객체의 공간복잡도가 O(n), 나머지는 O(1)
27+ */
You can’t perform that action at this time.
0 commit comments