File tree 5 files changed +164
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence 5 files changed +164
-0
lines changed Original file line number Diff line number Diff line change
1
+ // TC:
2
+ // SC:
3
+ class Solution {
4
+ public int maxArea (int [] height ) {
5
+ int max = 0 ;
6
+
7
+ int start = 0 ;
8
+ int end = height .length -1 ;
9
+
10
+ while (start < end ) {
11
+ int heightLeft = height [start ];
12
+ int heightRight = height [end ];
13
+
14
+ int hei = Math .min (heightLeft , heightRight );
15
+ int wid = end - start ;
16
+
17
+ max = Math .max (max , hei *wid );
18
+
19
+ if (heightRight > heightLeft ) start += 1 ;
20
+ else end -= 1 ;
21
+ }
22
+ return max ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ // SC: O(n)
2
+ // -> n is the length of the given String
3
+ // TC: O(n * 26)
4
+ // -> n is the length of the given String * the number of alphabets
5
+ class TrieNode {
6
+ TrieNode [] childNode ;
7
+ boolean isEndOfWord ;
8
+
9
+ public TrieNode () {
10
+ childNode = new TrieNode [26 ];
11
+ isEndOfWord = false ;
12
+ }
13
+ }
14
+
15
+ class WordDictionary {
16
+
17
+ private TrieNode root ;
18
+
19
+ public WordDictionary () {
20
+ root = new TrieNode ();
21
+ }
22
+
23
+ public void addWord (String word ) {
24
+ TrieNode node = root ;
25
+
26
+ for (char c : word .toCharArray ()) {
27
+ int idx = c - 'a' ;
28
+ if (node .childNode [idx ] == null ) {
29
+ node .childNode [idx ] = new TrieNode ();
30
+ }
31
+ node = node .childNode [idx ];
32
+ }
33
+ node .isEndOfWord = true ;
34
+ }
35
+
36
+ public boolean search (String word ) {
37
+ return searchInNode (word .toCharArray (), 0 , root );
38
+ }
39
+
40
+ private boolean searchInNode (char [] word , int idx , TrieNode node ) {
41
+ if (idx == word .length ) return node .isEndOfWord ;
42
+
43
+ char c = word [idx ];
44
+
45
+ if (c == '.' ) {
46
+ for (TrieNode child : node .childNode ) {
47
+ if (child != null && searchInNode (word , idx +1 , child )) return true ;
48
+ }
49
+ return false ;
50
+ } else {
51
+ int childIdx = c - 'a' ;
52
+ if (node .childNode [childIdx ] == null ) return false ;
53
+ return searchInNode (word , idx +1 , node .childNode [childIdx ]);
54
+ }
55
+ }
56
+ }
Original file line number Diff line number Diff line change
1
+ // TC: O(n log n)
2
+ // -> nums for loop O(n) + binarySearch O(log n)
3
+ // SC: O(n)
4
+ // -> ArrayList could have nums all elements
5
+ class Solution {
6
+ public int lengthOfLIS (int [] nums ) {
7
+ List <Integer > output = new ArrayList <>();
8
+
9
+ for (int num : nums ) {
10
+ int start = 0 ;
11
+ int end = output .size ();
12
+ while (start < end ) {
13
+ int mid = start + (end - start ) / 2 ;
14
+ if (output .get (mid ) < num ) start = mid + 1 ;
15
+ else end = mid ;
16
+ }
17
+ if (start == output .size ()) output .add (num );
18
+ else output .set (start , num );
19
+ }
20
+ return output .size ();
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <Integer > spiralOrder (int [][] matrix ) {
3
+ List <Integer > output = new ArrayList <>();
4
+ int north = 0 ;
5
+ int south = matrix .length - 1 ;
6
+ int east = matrix [0 ].length - 1 ;
7
+ int west = 0 ;
8
+
9
+ while (north <= south && west <= east ) {
10
+ int j = west ;
11
+ while (j <= east ) {
12
+ output .add (matrix [north ][j ]);
13
+ j += 1 ;
14
+ }
15
+ north += 1 ;
16
+
17
+ int i = north ;
18
+ while (i <= south ) {
19
+ output .add (matrix [i ][east ]);
20
+ i += 1 ;
21
+ }
22
+ east -= 1 ;
23
+
24
+ if (north <= south ) {
25
+ j = east ;
26
+ while (j >= west ) {
27
+ output .add (matrix [south ][j ]);
28
+ j -= 1 ;
29
+ }
30
+ south -= 1 ;
31
+ }
32
+
33
+ if (west <= east ) {
34
+ i = south ;
35
+ while (i >= north ) {
36
+ output .add (matrix [i ][west ]);
37
+ i -= 1 ;
38
+ }
39
+ west += 1 ;
40
+ }
41
+ }
42
+ return output ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ // TC: O(n)
2
+ // -> n = s.length
3
+ // SC: O(n)
4
+ // -> n = s.length / 2
5
+ class Solution {
6
+ public boolean isValid (String s ) {
7
+ Stack <Character > stack = new Stack <>();
8
+
9
+ for (char c : s .toCharArray ()) {
10
+ if (c == '(' ) stack .add (')' );
11
+ else if (c == '{' ) stack .add ('}' );
12
+ else if (c == '[' ) stack .add (']' );
13
+ else if (stack .isEmpty () || stack .pop () != c ) return false ;
14
+ }
15
+
16
+ return stack .isEmpty ();
17
+ }
18
+ }
You can’t perform that action at this time.
0 commit comments