File tree Expand file tree Collapse file tree 4 files changed +129
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings Expand file tree Collapse file tree 4 files changed +129
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ต๋ ์ด์ต์ ๊ณ์ฐํ๋ ํจ์
3
+ * @param {number[] } prices
4
+ * @returns {number }
5
+ *
6
+ * ์๊ฐ ๋ณต์ก๋ : O(n) (n: ์ฃผ์ ๊ฐ๊ฒฉ ๋ฐฐ์ด์ ๊ธธ์ด)
7
+ * ๊ณต๊ฐ ๋ณต์ก๋ : 0(1) (์ถ๊ฐ ์๋ฃ๊ตฌ์กฐ X)
8
+ */
9
+ function maxProfit ( prices : number [ ] ) : number {
10
+ let minPrice = 100001 ; // ์ง๊ธ๊น์ง์ ์ต์ ๊ฐ๊ฒฉ
11
+ let maxProfit = 0 ; // ์ต๋ ์ด์ต
12
+
13
+ for ( let price of prices ) {
14
+ // ์ต์ ๊ฐ๊ฒฉ ๊ฐฑ์
15
+ if ( price < minPrice ) {
16
+ minPrice = price ;
17
+ }
18
+
19
+ // ํ์ฌ ๊ฐ๊ฒฉ์์ ์ต์ ๊ฐ๊ฒฉ์ ๋บ ์ด์ต์ด ์ต๋ ์ด์ต๋ณด๋ค ํฌ๋ค๋ฉด ๊ฐฑ์
20
+ const potentialProfit = price - minPrice ;
21
+ if ( potentialProfit > maxProfit ) {
22
+ maxProfit = potentialProfit ;
23
+ }
24
+ }
25
+
26
+ return maxProfit ;
27
+ }
28
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @description ๋ฌธ์์ด ๋ฐฐ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํฉ๋๋ค.
3
+ * @param {string[] } strs - ๋ฌธ์์ด ๋ฐฐ์ด
4
+ * @returns {string } ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
5
+ *
6
+ * ์๊ฐ ๋ณต์ก๋: O(N)
7
+ * - N์ ์
๋ ฅ ๋ฐฐ์ด์ ๋ชจ๋ ๋ฌธ์์ด ๊ธธ์ด์ ํฉ
8
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(1)
9
+ * - ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์
10
+ */
11
+ function encode ( strs : string [ ] ) : string {
12
+ return strs . join ( ':' ) ;
13
+ }
14
+
15
+ /**
16
+ * @description ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด์ ๋ค์ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋์ฝ๋ฉํฉ๋๋ค.
17
+ * @param {string } s - ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด
18
+ * @returns {string[] } ๋์ฝ๋ฉ๋ ๋ฌธ์์ด ๋ฐฐ์ด
19
+ *
20
+ * ์๊ฐ ๋ณต์ก๋: O(N)
21
+ * - N์ ์
๋ ฅ ๋ฌธ์์ด์ ๊ธธ์ด
22
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(1)
23
+ * - ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์
24
+ */
25
+ function decode ( s : string ) : string [ ] {
26
+ return s . split ( ':' ) ;
27
+ }
28
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ฃผ์ด์ง ๋ฌธ์์ด ๋ฐฐ์ด์์ Anagram์ ๊ทธ๋ฃนํ ํด์ ๋ฐฐ์ด ๋ง๋ค๊ธฐ
3
+ * @param {string[] } strs - ๋ฌธ์์ด ๋ฐฐ์ด
4
+ * @returns {string[][] } Anagram์ ๊ทธ๋ฃน ๋ฐฐ์ด
5
+ *
6
+ * ๋ฌธ์์ด๋ค์ ์ ๋ ฌํด์ Map์ ๋ด์์ ์กด์ฌํ๋ฉด ๊ทธ ๋์ ๋ฌธ์๋ฅผ value๋ก ์ถ๊ฐ
7
+ * ์กด์ฌํ์ง ์์ผ๋ฉด ์๋ก์ด ๋ฐฐ์ด์ value๋ก ์ถ๊ฐ
8
+ *
9
+ * ์๊ฐ ๋ณต์ก๋: O(N * M * log(M))
10
+ * - N์ ๋ฌธ์์ด ๋ฐฐ์ด์ ๊ธธ์ด
11
+ * - M์ ๊ฐ ๋ฌธ์์ด์ ํ๊ท ๊ธธ์ด (์ ๋ ฌ ์๊ฐ ๋๋ฌธ)
12
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(N * M)
13
+ * - ํด์๋งต์ ์ ์ฅ๋๋ ๋ฌธ์์ด ๊ทธ๋ฃน ๋๋ฌธ
14
+ */
15
+ function groupAnagrams ( strs : string [ ] ) : string [ ] [ ] {
16
+ const anagramMap : Map < string , string [ ] > = new Map ( ) ;
17
+ for ( const str of strs ) {
18
+
19
+ // ์ ๋ ฌ๋ ๋ฌธ์์ด
20
+ const sortedStr = str . split ( '' ) . sort ( ) . join ( '' ) ;
21
+
22
+ // ์ ๋ ฌ๋ ๋ฌธ์์ด์ด ์กด์ฌํ๋ ์๋ ๊ฒฝ์ฐ
23
+ if ( ! anagramMap . has ( sortedStr ) ) {
24
+ anagramMap . set ( sortedStr , [ ] )
25
+ }
26
+
27
+ // ์ ๋ ฌ๋ ๋ฌธ์์ด์ key๋ก ํ๋ value str ์ถ๊ฐ
28
+ anagramMap . get ( sortedStr ) ?. push ( str ) ;
29
+ }
30
+
31
+ // anagramMap์์ values๋ง ๋ฐฐ์ด๋กํด์ ์ถ๋ ฅ
32
+ return Array . from ( anagramMap . values ( ) )
33
+ }
34
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ฃผ์ด์ง ๋ฌธ์์ด์ด ๋จ์ด ์ฌ์ ์ ์๋ ๋จ์ด๋ค๋ก ๋๋์ด์ง ์ ์๋์ง ํ์ธํฉ๋๋ค.
3
+ *
4
+ *
5
+ * @param {string } s - ํ์ธํ ๋ฌธ์์ด
6
+ * @param {string[] } wordDict - ๋จ์ด ์ฌ์
7
+ * @returns {boolean } ๋ฌธ์์ด์ด ๋จ์ด๋ก ์๋ฒฝํ ๋๋์ด์ง ์ ์์ผ๋ฉด `true`, ์๋๋ฉด `false`
8
+ *
9
+ * ์๊ฐ ๋ณต์ก์ฑ O(n * m * k)
10
+ * - n: ๋ฌธ์์ด s ๊ธธ์ด
11
+ * - m: ๋จ์ด ์ฌ์ ๊ธธ์ด
12
+ * - k: ๋จ์ด ์ฌ์ ๋ด ๋จ์ด ๊ธธ์ด
13
+ *
14
+ * ๊ณต๊ฐ ๋ณต์ก์ฑ O(n)
15
+ * - ๋ฉ๋ชจ์ด์ ์ด์
(memo) ๋ฐ ์ฌ๊ท ํธ์ถ ์คํ ํฌ๊ธฐ๊ฐ ๋ฌธ์์ด ๊ธธ์ด n์ ๋น๋ก.
16
+ */
17
+ function wordBreak ( s : string , wordDict : string [ ] ) : boolean {
18
+ const memo : Record < number , boolean > = { } ;
19
+
20
+ /**
21
+ * @param {number } start - ํ์ฌ ์์ idx
22
+ * @returns {boolean } ์ฃผ์ด์ง idx ๋ถํฐ ๋ฌธ์์ด์ ๋๋ ์ ์์ผ๋ฉด true
23
+ */
24
+ const dfs = ( start : number ) : boolean => {
25
+ if ( start in memo ) return memo [ start ] ;
26
+ if ( start === s . length ) return ( memo [ start ] = true ) ;
27
+
28
+ for ( const word of wordDict ) {
29
+ if ( s . startsWith ( word , start ) && dfs ( start + word . length ) ) {
30
+ return ( memo [ start ] = true ) ;
31
+ }
32
+ }
33
+
34
+ return ( memo [ start ] = false ) ;
35
+ } ;
36
+
37
+ return dfs ( 0 ) ;
38
+ }
39
+
You canโt perform that action at this time.
0 commit comments