File tree Expand file tree Collapse file tree 3 files changed +103
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure Expand file tree Collapse file tree 3 files changed +103
-0
lines changed Original file line number Diff line number Diff line change 1+ // tc: O(n);
2+ // sc: O(1);
3+ const maxArea = function ( height ) {
4+ let max = 0 ;
5+ let leftIdx = 0 ;
6+ let rightIdx = height . length - 1 ;
7+
8+ while ( leftIdx < rightIdx ) {
9+ const width = rightIdx - leftIdx ;
10+ const minHeight = Math . min ( height [ leftIdx ] , height [ rightIdx ] ) ;
11+ const area = width * minHeight ;
12+ max = Math . max ( max , area ) ;
13+
14+ if ( height [ leftIdx ] > height [ rightIdx ] ) {
15+ rightIdx -= 1 ;
16+ } else {
17+ leftIdx += 1 ;
18+ }
19+ }
20+
21+ return max ;
22+ } ;
Original file line number Diff line number Diff line change 1+ var WordDictionary = function ( ) {
2+ this . dictionary = new Set ( ) ;
3+ } ;
4+
5+ /**
6+ * @param {string } word
7+ * @return {void }
8+ */
9+ WordDictionary . prototype . addWord = function ( word ) {
10+ this . dictionary . add ( word ) ;
11+ } ;
12+
13+ /**
14+ * @param {string } word
15+ * @return {boolean }
16+ */
17+
18+ // tc: O(n * k), n: dictionary size, k: word length => O(n) in worst case
19+ // sc: O(1)
20+ WordDictionary . prototype . search = function ( word ) {
21+ if ( ! word . includes ( '.' ) ) {
22+ return this . dictionary . has ( word ) ;
23+ }
24+
25+ for ( const stored of this . dictionary ) {
26+ if ( stored . length !== word . length ) continue ;
27+
28+ let isMatch = true ;
29+ for ( let i = 0 ; i < word . length ; i ++ ) {
30+ if ( word [ i ] === '.' ) continue ;
31+ if ( word [ i ] !== stored [ i ] ) {
32+ isMatch = false ;
33+ break ;
34+ }
35+ }
36+ if ( isMatch ) return true ;
37+ }
38+
39+ return false ;
40+ } ;
41+
42+ /**
43+ * Your WordDictionary object will be instantiated and called as such:
44+ * var obj = new WordDictionary()
45+ * obj.addWord(word)
46+ * var param_2 = obj.search(word)
47+ */
Original file line number Diff line number Diff line change 1+ // tc: O(n)
2+ // sc: O(n)
3+ const isValid = function ( s ) {
4+ const bracketMap = {
5+ '(' : ')' ,
6+ '{' : '}' ,
7+ '[' : ']' ,
8+ } ;
9+
10+ if ( s . length % 2 !== 0 || isCloseBracket ( s [ 0 ] ) ) return false ;
11+
12+ const stack = [ ] ;
13+
14+ for ( let i = 0 ; i < s . length ; i ++ ) {
15+ if ( stack . length === 0 ) {
16+ stack . push ( s [ i ] ) ;
17+ continue ;
18+ }
19+
20+ let topBracket = stack . pop ( ) ;
21+ if ( bracketMap [ topBracket ] !== s [ i ] ) {
22+ stack . push ( topBracket ) ;
23+ stack . push ( s [ i ] ) ;
24+ }
25+ }
26+
27+ return stack . length === 0 ;
28+ } ;
29+
30+ function isCloseBracket ( char ) {
31+ const closeBrackets = [ ')' , '}' , ']' ] ;
32+
33+ return closeBrackets . includes ( char ) ;
34+ }
You can’t perform that action at this time.
0 commit comments