File tree 2 files changed +97
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
2 files changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } height
3
+ * @return {number }
4
+ */
5
+
6
+ var maxArea = function ( height ) {
7
+ let max = 0 ; // 최대값
8
+ let left = 0 ; // 왼쪽 값
9
+ let right = height . length - 1 ; // 오른쪽 값
10
+
11
+ // 왼쪽과 오른쪽을 하나씩 줄여가며 가운데서 만날 때 까지 반복
12
+ while ( left < right ) {
13
+ // 최대값 = 가로길이(오른쪽 값 - 왼쪽 값) * 세로길이(왼쪽 값, 오른쪽 값 중 더 작은 값)
14
+ max = Math . max ( max , ( right - left ) * Math . min ( height [ left ] , height [ right ] ) ) ;
15
+ // 오른쪽 세로가 더 높다면 왼쪽 값 증가
16
+ if ( height [ left ] < height [ right ] ) {
17
+ left ++ ;
18
+ }
19
+ else {
20
+ right -- ;
21
+ }
22
+ }
23
+ return max ;
24
+ } ;
Original file line number Diff line number Diff line change
1
+ // node 함수선언
2
+ function Node ( ) {
3
+ this . child = { } ;
4
+ this . end = false ;
5
+ }
6
+
7
+ // 최상단의 루트를 노드로 초기화
8
+ var WordDictionary = function ( ) {
9
+ this . root = new Node ( ) ;
10
+ } ;
11
+
12
+ /**
13
+ * @param {string } word
14
+ * @return {void }
15
+ */
16
+ WordDictionary . prototype . addWord = function ( word ) {
17
+ // 현재위치를 최상단으로 초기화
18
+ let current = this . root ;
19
+
20
+ // 문자를 받고 단어하나씩 노드에 저장
21
+ for ( const char of word ) {
22
+ if ( ! current . child [ char ] ) {
23
+ current . child [ char ] = new Node ( ) ;
24
+ }
25
+ current = current . child [ char ] ;
26
+ }
27
+
28
+ // 반복이 끝났으면 true;
29
+ current . end = true ;
30
+
31
+ } ;
32
+
33
+ /**
34
+ * @param {string } word
35
+ * @return {boolean }
36
+ */
37
+ WordDictionary . prototype . search = function ( word ) {
38
+
39
+ // i 를 받아 단어 만큼 재귀하는 함수
40
+ const searchHelper = ( current , i ) => {
41
+ // i와 단어의 길이가 같으면 종료
42
+ if ( i === word . length ) return current . end ;
43
+
44
+ // 단어 = 찾을 문자의 i번째 단어
45
+ const char = word [ i ] ;
46
+
47
+ // 만약 문자가 . 라면
48
+ if ( char === '.' ) {
49
+ // 해당 현재 것들의 키를 가지고 반복
50
+ for ( const char of Object . keys ( current . child ) ) {
51
+ const children = current . child [ char ] ;
52
+ // end를 true로 하고 i+1로 재귀
53
+ if ( searchHelper ( children , i + 1 ) ) return true ;
54
+ }
55
+ return false ;
56
+ }
57
+ else {
58
+ // 현재 자식에 해당 문자가 없으면 false
59
+ if ( ! ( char in current . child ) ) return false ;
60
+ // 아니면 한번 더 재귀
61
+ return searchHelper ( current . child [ char ] , i + 1 ) ;
62
+ }
63
+ }
64
+ // 결과 리턴
65
+ return searchHelper ( this . root , 0 ) ;
66
+ } ;
67
+
68
+ /**
69
+ * Your WordDictionary object will be instantiated and called as such:
70
+ * var obj = new WordDictionary()
71
+ * obj.addWord(word)
72
+ * var param_2 = obj.search(word)
73
+ */
You can’t perform that action at this time.
0 commit comments