File tree Expand file tree Collapse file tree 5 files changed +136
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree 5 files changed +136
-0
lines changed Original file line number Diff line number Diff line change 1+ var maxProfit = function ( prices ) {
2+ let minPrice = Infinity ;
3+ let maxProfit = 0 ;
4+
5+ for ( let price of prices ) {
6+ if ( price < minPrice ) {
7+ minPrice = price ; // ๋ ์ผ ๊ฐ๊ฒฉ์ด ๋ํ๋๋ฉด ๊ฐฑ์
8+ } else {
9+ maxProfit = Math . max ( maxProfit , price - minPrice ) ; // ์ด์ต ๊ฐฑ์
10+ }
11+ }
12+
13+ return maxProfit ;
14+ } ;
Original file line number Diff line number Diff line change 1+ class Solution {
2+ /**
3+ * @param {string[] } strs
4+ * @returns {string }
5+ */
6+ encode ( strs ) {
7+ let result = "" ;
8+
9+ for ( const str of strs ) {
10+ result += `${ str . length } #${ str } ` ;
11+ }
12+
13+ return result ;
14+ }
15+
16+ /**
17+ * @param {string } str
18+ * @returns {string[] }
19+ */
20+ decode ( s ) {
21+ let result = [ ] ;
22+ let i = 0 ;
23+ // 5#hello5#world
24+ while ( i < s . length ) {
25+ const pos = s . indexOf ( "#" , i ) ;
26+ const len = parseInt ( s . slice ( i , pos ) ) ; // 5
27+ i = pos + 1 ;
28+ const str = s . slice ( i , i + len ) ;
29+ result . push ( str ) ;
30+ i += len ;
31+ }
32+ return result ;
33+ }
34+ }
35+
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string[] } strs
3+ * @return {string[][] }
4+ */
5+ var groupAnagrams = function ( strs ) {
6+ const dict = new Map ( ) ;
7+
8+ strs . forEach ( str => {
9+ const sorted = str . split ( '' ) . sort ( ) . join ( '' ) ;
10+ if ( ! dict . has ( sorted ) ) {
11+ dict . set ( sorted , [ str ] ) ;
12+ } else {
13+ dict . get ( sorted ) . push ( str ) ;
14+ }
15+ } ) ;
16+
17+ // value ๊ธธ์ด ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
18+ const result = [ ...dict . entries ( ) ]
19+ . sort ( ( a , b ) => b [ 1 ] . length - a [ 1 ] . length )
20+ . map ( ( [ _ , group ] ) => group ) ; // value (์ฆ, ์๋๊ทธ๋จ ๊ทธ๋ฃน)๋ง ๊บผ๋
21+
22+ return result ;
23+ } ;
Original file line number Diff line number Diff line change 1+ var Trie = function ( ) {
2+ this . root = { } ; // ๋ฃจํธ๋ ๋น ๊ฐ์ฒด๋ก ์์
3+ } ;
4+
5+ /**
6+ * @param {string } word
7+ * @return {void }
8+ */
9+ Trie . prototype . insert = function ( word ) {
10+ let node = this . root ;
11+ for ( let ch of word ) {
12+ if ( ! node [ ch ] ) node [ ch ] = { } ;
13+ node = node [ ch ] ;
14+ }
15+ node . isEnd = true ; // ๋จ์ด์ ๋์ ํ์
16+ } ;
17+
18+ /**
19+ * @param {string } word
20+ * @return {boolean }
21+ */
22+ Trie . prototype . search = function ( word ) {
23+ let node = this . root ;
24+ for ( let ch of word ) {
25+ if ( ! node [ ch ] ) return false ;
26+ node = node [ ch ] ;
27+ }
28+ return node . isEnd === true ; // ๋จ์ด์ ๋์ด์ด์ผ๋ง true
29+ } ;
30+
31+ /**
32+ * @param {string } prefix
33+ * @return {boolean }
34+ */
35+ Trie . prototype . startsWith = function ( prefix ) {
36+ let node = this . root ;
37+ for ( let ch of prefix ) {
38+ if ( ! node [ ch ] ) return false ;
39+ node = node [ ch ] ;
40+ }
41+ return true ; // ์ ๋์ฌ๋ง ๋งค์นญ๋๋ฉด true
42+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @param {string[] } wordDict
4+ * @return {boolean }
5+ */
6+ var wordBreak = function ( s , wordDict ) {
7+ const wordSet = new Set ( wordDict ) ; // ๋น ๋ฅธ ๊ฒ์์ ์ํ Set
8+ const dp = Array ( s . length + 1 ) . fill ( false ) ;
9+ dp [ 0 ] = true ; // ๋น ๋ฌธ์์ด์ ํญ์ ๊ฐ๋ฅ
10+
11+ for ( let i = 1 ; i <= s . length ; i ++ ) {
12+ for ( let j = 0 ; j < i ; j ++ ) {
13+ const word = s . slice ( j , i ) ;
14+ if ( dp [ j ] && wordSet . has ( word ) ) {
15+ dp [ i ] = true ;
16+ break ; // ๋ ์ด์ ๋ณผ ํ์ ์์
17+ }
18+ }
19+ }
20+
21+ return dp [ s . length ] ;
22+ } ;
You canโt perform that action at this time.
0 commit comments