File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed
best-time-to-buy-and-sell-stock 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+ prices๋ฅผ ์ํํ๋ฉฐ ํ์ฌ ์ต์ ๊ฐ๊ณผ์ ์ฐจ์ด๋ฅผ ๊ตฌํด์ ์ต๋ profit์ ๊ฐฑ์ ํ ๋ค ์ต์ ๊ฐ(๊ตฌ๋งค๊ฐ)๋ฅผ ๊ฐฑ์ , ์ต์ข
๊ฐ์ ๋ฆฌํดํ๋ค
3+
4+ ์๊ฐ๋ณต์ก๋ O(N) - N์ prices์ length
5+ */
6+
7+ function maxProfit ( prices : number [ ] ) : number {
8+ let buy
9+ let result = 0
10+
11+ for ( let price of prices ) {
12+ if ( buy === undefined ) {
13+ buy = price
14+ continue
15+ }
16+ result = Math . max ( result , price - buy )
17+ buy = Math . min ( price , buy )
18+ }
19+
20+ return result
21+ }
Original file line number Diff line number Diff line change 1+ /*
2+ strs์ ๋ฌธ์์ด๋ค์ ๋ฑ์ฅ๋น๋๋ฅผ ๋ฐํ์ผ๋ก ๊ณ์ฐํ ํน์ ๋ฌธ์์ด๋ก ์ ํํ๊ณ ๊ฐ์ ๊ฒ๋ผ๋ฆฌ ๋ฌถ๋๋ค.
3+
4+ ์๊ฐ๋ณต์ก๋ : O(N * K) - N์ strs์ ๊ฐ์, K๋ strs์ ๊ธธ์ด. N ์ํ ์์ K ์ํ
5+ */
6+
7+ function groupAnagrams ( strs : string [ ] ) : string [ ] [ ] {
8+ const anagramMap = { }
9+ const result = strs . reduce ( ( acc , cur ) => {
10+ const charArray = new Array ( 26 ) . fill ( 0 )
11+
12+ for ( let char of cur ) {
13+ const charIdx = char . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 )
14+ charArray [ charIdx ] += 1
15+ }
16+
17+ const sortedValue = charArray . join ( '#' )
18+
19+ const targetIdx = anagramMap [ sortedValue ]
20+ if ( targetIdx !== undefined ) {
21+ acc [ targetIdx ] . push ( cur )
22+ } else {
23+ anagramMap [ sortedValue ] = acc . length
24+ acc . push ( [ cur ] )
25+ }
26+ return acc
27+ } , [ ] )
28+
29+ return result
30+ }
You canโt perform that action at this time.
0 commit comments