File tree Expand file tree Collapse file tree 4 files changed +139
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree 4 files changed +139
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 시간 복잡도: prices.length만큼 순회하므로 O(n)
3+ * 공간 복잡도: 상수 크기의 변수만 사용하므로 O(1)
4+ */
5+ /**
6+ * @param {number[] } prices
7+ * @return {number }
8+ */
9+ var maxProfit = function ( prices ) {
10+ let maxProfit = 0 ;
11+ let min = prices [ 0 ] ;
12+
13+ for ( let i = 0 ; i < prices . length ; i ++ ) {
14+ maxProfit = Math . max ( maxProfit , prices [ i ] - min ) ;
15+ min = Math . min ( min , prices [ i ] ) ;
16+ }
17+ return maxProfit ;
18+ } ;
Original file line number Diff line number Diff line change 1+ class Solution {
2+ /**
3+ * 시간 복잡도: strs의 길이만큼 순회하므로, O(n)
4+ * 공간 복잡도: 결괏값 제외 추가 변수 사용 없으므로 O(1)
5+ */
6+ /**
7+ * @param {string[] } strs
8+ * @returns {string }
9+ */
10+ encode ( strs ) {
11+ return strs . reduce ( ( acc , cur ) => acc + `${ cur . length } ` + '!' + cur , '' ) ;
12+ }
13+
14+ /**
15+ * 시간 복잡도: str의 길이만큼 순회하므로 str의 길이가 m이면, O(m)
16+ * 공간 복잡도: 결괏값 제외 상수 크기 변수만 사용하므로, O(1)
17+ */
18+ /**
19+ * @param {string } str
20+ * @returns {string[] }
21+ */
22+ decode ( str ) {
23+ const res = [ ] ;
24+ let i = 0 ;
25+ while ( i < str . length ) {
26+ let j = i ;
27+ while ( str [ j ] !== '!' ) {
28+ j ++ ;
29+ }
30+ const len = Number ( str . slice ( i , j ) ) ;
31+ const s = str . slice ( j + 1 , j + 1 + len ) ;
32+ res . push ( s ) ;
33+ i = j + 1 + len ;
34+ }
35+ return res ;
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * 시간 복잡도:
3+ * 정렬 작업은 각 문자열의 길이가 m일 때 O(m logm)이고, 총 strs의 길이만큼 수행되므로
4+ * 시간 복잡도는 O(n * mlogm)
5+ * 공간 복잡도:
6+ * Map 키는 최대 길이 m인 문자열 strs.length개이다.
7+ * 따라서 공간 복잡도는 O(n * m)
8+ */
9+ /**
10+ * @param {string[] } strs
11+ * @return {string[][] }
12+ */
13+ var groupAnagrams = function ( strs ) {
14+ const map = new Map ( ) ;
15+ for ( const s of strs ) {
16+ const key = s . split ( '' ) . sort ( ) . join ( '' ) ;
17+ if ( ! map . has ( key ) ) {
18+ map . set ( key , [ ] )
19+ }
20+ map . get ( key ) . push ( s ) ;
21+ }
22+ return Array . from ( map . values ( ) ) ;
23+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 시간 복잡도: 주어진 문자열의 길이만큼 순회하므로, O(n)
3+ * 공간 복잡도: 삽입된 모든 문자열의 길이만큼 노드가 만들어지므로, 이를 m이라고 하면 O(m)
4+ */
5+ var Trie = function ( ) {
6+ this . isEnd = false ;
7+ this . children = { } ;
8+ } ;
9+
10+ /**
11+ * @param {string } word
12+ * @return {void }
13+ */
14+ Trie . prototype . insert = function ( word ) {
15+ let cur = this . children ;
16+ for ( const c of word ) {
17+ if ( ! cur [ c ] ) {
18+ cur [ c ] = { isEnd : false , children : { } } ;
19+ }
20+ cur = cur [ c ] ;
21+ }
22+ cur . isEnd = true ;
23+ } ;
24+
25+ /**
26+ * @param {string } word
27+ * @return {boolean }
28+ */
29+ Trie . prototype . search = function ( word ) {
30+ let cur = this . children ;
31+ for ( const c of word ) {
32+ if ( ! cur [ c ] ) {
33+ return false ;
34+ }
35+ cur = cur [ c ]
36+ }
37+ return cur . isEnd ;
38+ } ;
39+
40+ /**
41+ * @param {string } prefix
42+ * @return {boolean }
43+ */
44+ Trie . prototype . startsWith = function ( prefix ) {
45+ let cur = this . children ;
46+ for ( const c of prefix ) {
47+ if ( ! cur [ c ] ) {
48+ return false ;
49+ }
50+ cur = cur [ c ] ;
51+ }
52+ return true ;
53+ } ;
54+
55+ /**
56+ * Your Trie object will be instantiated and called as such:
57+ * var obj = new Trie()
58+ * obj.insert(word)
59+ * var param_2 = obj.search(word)
60+ * var param_3 = obj.startsWith(prefix)
61+ */
You can’t perform that action at this time.
0 commit comments