File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/climbing-stairs/
2
+ /**
3
+ * @SC `O(1)`
4
+ * @TC `O(n)`
5
+ */
6
+ function climbStairs ( n : number ) : number {
7
+ if ( n <= 2 ) {
8
+ return n ;
9
+ }
10
+
11
+ let prev1 = 1 ;
12
+ let prev2 = 2 ;
13
+
14
+ for ( let i = 2 ; i < n ; i ++ ) {
15
+ let temp = prev1 ;
16
+ prev1 = prev2 ;
17
+ prev2 = temp + prev2 ;
18
+ }
19
+ return prev2 ;
20
+ }
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/valid-anagram/
2
+
3
+ /**
4
+ * @SC `O(N)`
5
+ * @TC `O(N)`
6
+ */
7
+ namespace use_hashmap {
8
+ function isAnagram ( s : string , t : string ) : boolean {
9
+ if ( s . length !== t . length ) {
10
+ return false ;
11
+ }
12
+ const counter = { } ;
13
+ for ( const char of s ) {
14
+ counter [ char ] = ( counter [ char ] || 0 ) + 1 ;
15
+ }
16
+ for ( const char of t ) {
17
+ if ( ! counter [ char ] ) {
18
+ return false ;
19
+ }
20
+ counter [ char ] = counter [ char ] - 1 ;
21
+ }
22
+ return true ;
23
+ }
24
+ }
25
+
26
+ /**
27
+ * @SC `O(N)`
28
+ * @TC `O(Nlog(N))`
29
+ */
30
+ namespace naive_approach {
31
+ function isAnagram ( s : string , t : string ) : boolean {
32
+ return [ ...s ] . sort ( ) . join ( '' ) === [ ...t ] . sort ( ) . join ( '' ) ;
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments