File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ function climbStairs ( n ) {
2+ // if there's only 1 step, return 1
3+ if ( n === 1 ) return 1 ;
4+ // initialize the first prev values (when n=1 and n=0 each)
5+ let prev1 = 1 ;
6+ let prev2 = 1 ;
7+ // iterate through step 2 to n
8+ for ( let i = 2 ; i <= n ; i ++ ) {
9+ let current = prev1 + prev2 ;
10+ prev2 = prev1 ;
11+ prev1 = current ;
12+ }
13+ return prev1 ;
14+ }
15+
16+ // Time complexity: O(n) - single for loop
17+ // Space Complexity: O(1)
Original file line number Diff line number Diff line change 1+ var isAnagram = function ( s , t ) {
2+ const sArray = [ ] ;
3+ const tArray = [ ] ;
4+
5+ for ( i = 0 ; i < s . length ; i ++ ) {
6+ sArray . push ( s . charAt ( i ) ) ;
7+ }
8+ for ( i = 0 ; i < t . length ; i ++ ) {
9+ tArray . push ( t . charAt ( i ) ) ;
10+ }
11+
12+ return JSON . stringify ( sArray . sort ( ) ) === JSON . stringify ( tArray . sort ( ) ) ;
13+ } ;
14+
15+ // Time complexity : O(nlogn)
16+ // Space complexity : O(n)
You can’t perform that action at this time.
0 commit comments