File tree 5 files changed +161
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree
5 files changed +161
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 주어진 prices에서 가장 큰 prices[j] - prices[i] (i < j) 를 반환하는 함수
3
+ * @param {number[] } prices
4
+ * @return {number }
5
+ */
6
+ const maxProfit = function ( prices ) {
7
+ let min = prices [ 0 ] ;
8
+ let profit = 0 ;
9
+
10
+ for ( const price of prices ) {
11
+ min = Math . min ( min , price ) ;
12
+ profit = Math . max ( profit , price - min ) ;
13
+ }
14
+
15
+ return profit ;
16
+ } ;
17
+ // 시간복잡도: O(n)
18
+ // 공간복잡도: O(1)
Original file line number Diff line number Diff line change
1
+ const encode = function ( strs ) {
2
+ const separator = '\\' ;
3
+ return strs . join ( separator ) ;
4
+ }
5
+
6
+
7
+ const decode = function ( str ) {
8
+ const separator = '\\' ;
9
+ return str . split ( separator ) ;
10
+ }
11
+
12
+ // 문제가 너무 별로다
13
+ // 문제에서 제시하고 있는 해답도 별로다
14
+ // 이모지같은 걸 구분자로 쓰거나, length를 앞에 넣어서 구별하거나 해도 사실 제대로 암호화했다고 말할 수 없음
15
+ // 그런 걸 정답으로 제공할 거면 이런 문제를 왜 내는 거여
16
+ // 이 문제가 Blind 75에 속해 있는 이유가 뭘까...?!
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 애너그램끼리 묶어서 반환하는 함수
3
+ * @param {string[] } strs
4
+ * @return {string[][] }
5
+ */
6
+ const groupAnagrams = function ( strs ) {
7
+ // 풀이 1
8
+ // 시간복잡도: O(n*s) (n: strs.length, s: str.length)
9
+ // 공간복잡도: O(n)
10
+ function groupManually ( ) {
11
+ const groups = { } ;
12
+
13
+ strs . forEach ( ( str ) => {
14
+ const key = [ ...str ] . sort ( ) . join ( '' ) ;
15
+ if ( ! groups [ key ] ) groups [ key ] = [ ] ;
16
+ groups [ key ] . push ( str ) ;
17
+ } ) ;
18
+
19
+ return Object . values ( groups ) ;
20
+ }
21
+
22
+ // 풀이 2
23
+ // 시간복잡도: O(n*s) (n: strs.length, s: str.length)
24
+ // 공간복잡도: O(n)
25
+ function groupByAnagram ( ) {
26
+ const result = Object . groupBy ( strs , ( str ) => [ ...str ] . sort ( ) . join ( '' ) ) ;
27
+ return Object . values ( result ) ;
28
+ }
29
+
30
+ return groupByAnagram ( ) ;
31
+ } ;
32
+
Original file line number Diff line number Diff line change
1
+ const Node = function ( value ) {
2
+ this . value = value ;
3
+ this . children = { } ;
4
+ this . data = null ;
5
+ }
6
+
7
+ const Trie = function ( ) {
8
+ this . root = new Node ( null ) ;
9
+ } ;
10
+
11
+ /**
12
+ * @param {string } word
13
+ * @return {void }
14
+ */
15
+ Trie . prototype . insert = function ( word ) {
16
+ let parent = this . root ;
17
+
18
+ for ( let i = 0 ; i < word . length ; i ++ ) {
19
+ if ( ! parent . children [ word [ i ] ] ) {
20
+ parent . children [ word [ i ] ] = new Node ( word [ i ] ) ;
21
+ }
22
+ parent = parent . children [ word [ i ] ] ;
23
+ }
24
+
25
+ parent . data = word ;
26
+ } ;
27
+
28
+ /**
29
+ * @param {string } word
30
+ * @return {boolean }
31
+ */
32
+ Trie . prototype . search = function ( word ) {
33
+ let parent = this . root ;
34
+ let i = 0 ;
35
+
36
+ while ( i < word . length && parent . children [ word [ i ] ] ) {
37
+ parent = parent . children [ word [ i ] ] ;
38
+ i += 1 ;
39
+ }
40
+
41
+ return parent . data === word ;
42
+ } ;
43
+
44
+ /**
45
+ * @param {string } prefix
46
+ * @return {boolean }
47
+ */
48
+ Trie . prototype . startsWith = function ( prefix ) {
49
+ let parent = this . root ;
50
+
51
+ for ( let char of prefix ) {
52
+ if ( ! parent . children [ char ] ) return false ;
53
+ parent = parent . children [ char ] ;
54
+ }
55
+
56
+ return true ;
57
+ } ;
58
+
59
+ /**
60
+ * Your Trie object will be instantiated and called as such:
61
+ * var obj = new Trie()
62
+ * obj.insert(word)
63
+ * var param_2 = obj.search(word)
64
+ * var param_3 = obj.startsWith(prefix)
65
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @param {string[] } wordDict
4
+ * @return {boolean }
5
+ */
6
+ const wordBreak = function ( s , wordDict ) {
7
+ const dp = Array ( s . length ) ;
8
+ const dict = new Set ( wordDict ) ;
9
+
10
+ function recurse ( start ) {
11
+ if ( start === s . length ) return true ;
12
+ if ( dp [ start ] !== undefined ) return dp [ start ] ;
13
+
14
+ for ( let end = start + 1 ; end <= s . length ; end ++ ) {
15
+ const substr = s . slice ( start , end ) ;
16
+ if ( dict . has ( substr ) && recurse ( end ) ) {
17
+ dp [ start ] = true ;
18
+ return true ;
19
+ }
20
+ }
21
+
22
+ dp [ start ] = false ;
23
+ return false ;
24
+ }
25
+
26
+ return recurse ( 0 ) ;
27
+ } ;
28
+
29
+ // 시간복잡도: O(n^2) (n: s.length. n번 재귀 & 최대 n번 슬라이싱)
30
+ // 공간복잡도: O(n + m) (m: wordDict.length)
You can’t perform that action at this time.
0 commit comments