File tree Expand file tree Collapse file tree 5 files changed +144
-0
lines changed
best-time-to-buy-and-sell-stock
implement-trie-prefix-tree Expand file tree Collapse file tree 5 files changed +144
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/3sum/description
3+ * T.C: O(n^2)
4+ * S.C: O(1)
5+ */
6+ function threeSum ( nums : number [ ] ) : number [ ] [ ] {
7+ nums . sort ( ( a , b ) => a - b ) ;
8+
9+ const result : number [ ] [ ] = [ ] ;
10+
11+ for ( let i = 0 ; i < nums . length - 2 ; i ++ ) { // O(n)
12+ if ( i > 0 && nums [ i ] == nums [ i - 1 ] ) continue ;
13+
14+ let left = i + 1 ;
15+ let right = nums . length - 1 ;
16+
17+ while ( left < right ) { // O(n)
18+ const sum = nums [ i ] + nums [ left ] + nums [ right ] ;
19+ if ( sum == 0 ) {
20+ result . push ( [ nums [ i ] , nums [ left ] , nums [ right ] ] ) ;
21+
22+ while ( left < right && nums [ left ] == nums [ left + 1 ] ) left ++ ;
23+ while ( left < right && nums [ right ] == nums [ right - 1 ] ) right -- ;
24+
25+ left ++ ;
26+ right -- ;
27+ } else if ( sum < 0 ) {
28+ left ++ ;
29+ } else {
30+ right -- ;
31+ }
32+ }
33+ }
34+
35+ return result ;
36+ }
Original file line number Diff line number Diff line change 1+ // https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+ // T.C: O(N)
3+ // S.C: O(1)
4+ function maxProfit ( prices : number [ ] ) : number {
5+ let min = prices [ 0 ] ;
6+ let max = prices [ 0 ] ;
7+ let candidate = prices [ 0 ] ;
8+
9+ for ( let i = 1 ; i < prices . length ; i ++ ) {
10+ if ( prices [ i ] < candidate ) {
11+ candidate = prices [ i ] ;
12+ } else if ( prices [ i ] - candidate > max - min ) {
13+ min = candidate ;
14+ max = prices [ i ] ;
15+ }
16+ }
17+
18+ return max - min ;
19+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/group-anagrams/description
3+ * T.C: O(n * m * log(m)), n = strs.length, m = max(strs[i].length)
4+ * S.C: O(n * m)
5+ */
6+ function groupAnagrams ( strs : string [ ] ) : string [ ] [ ] {
7+ const map = new Map < string , string [ ] > ( ) ;
8+
9+ for ( const str of strs ) { // O(n)
10+ const sorted = str . split ( '' ) . sort ( ) . join ( '' ) ; // O(m * log(m))
11+
12+ if ( map . has ( sorted ) )
13+ map . get ( sorted ) ! . push ( str ) ;
14+ else
15+ map . set ( sorted , [ str ] ) ;
16+ }
17+
18+ return Array . from ( map . values ( ) ) ;
19+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/implement-trie-prefix-tree
3+ */
4+ class Trie {
5+ constructor ( private root : Record < string , any > = { } ) { }
6+
7+ insert ( word : string ) : void {
8+ let node = this . root ;
9+ for ( const char of word ) {
10+ if ( ! node [ char ] ) {
11+ node [ char ] = { } ;
12+ }
13+ node = node [ char ] ;
14+ }
15+ node . isEnd = true ;
16+ }
17+
18+ search ( word : string ) : boolean {
19+ let node = this . root ;
20+ for ( const char of word ) {
21+ if ( ! node [ char ] ) {
22+ return false ;
23+ }
24+ node = node [ char ] ;
25+ }
26+ return ! ! node . isEnd ;
27+ }
28+
29+ startsWith ( prefix : string ) : boolean {
30+ let node = this . root ;
31+ for ( const char of prefix ) {
32+ if ( ! node [ char ] ) {
33+ return false ;
34+ }
35+ node = node [ char ] ;
36+ }
37+ return true ;
38+ }
39+ }
40+
41+ /**
42+ * Your Trie object will be instantiated and called as such:
43+ * var obj = new Trie()
44+ * obj.insert(word)
45+ * var param_2 = obj.search(word)
46+ * var param_3 = obj.startsWith(prefix)
47+ */
Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/word-break
3+ * T.C. O(s^2)
4+ * S.C. O(s + w)
5+ */
6+ function wordBreak ( s : string , wordDict : string [ ] ) : boolean {
7+ const wordSet = new Set ( wordDict ) ;
8+
9+ const dp = new Array ( s . length + 1 ) . fill ( false ) ;
10+ dp [ 0 ] = true ;
11+
12+ for ( let i = 1 ; i <= s . length ; i ++ ) {
13+ for ( let j = 0 ; j < i ; j ++ ) {
14+ if ( ! dp [ j ] ) continue ;
15+ if ( j - i > 20 ) break ;
16+ if ( wordSet . has ( s . slice ( j , i ) ) ) {
17+ dp [ i ] = true ;
18+ break ;
19+ }
20+ }
21+ }
22+ return dp [ s . length ] ;
23+ }
You can’t perform that action at this time.
0 commit comments