File tree Expand file tree Collapse file tree 5 files changed +171
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence Expand file tree Collapse file tree 5 files changed +171
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int maxArea (int [] height ) {
3+ int maxArea = 0 ;
4+ int s = 0 , e = height .length - 1 ;
5+
6+ while (s < e ) {
7+ int h = Math .min (height [s ], height [e ]);
8+ int area = (e - s ) * h ;
9+ maxArea = Math .max (maxArea , area );
10+
11+ if (height [s ] < height [e ]) {
12+ s ++;
13+ } else {
14+ e --;
15+ }
16+ }
17+
18+ return maxArea ;
19+ }
20+ }
21+
Original file line number Diff line number Diff line change 1+ class WordDictionary {
2+ private static class TrieNode {
3+ boolean isEnd ;
4+ Map <Character , TrieNode > children ;
5+
6+ TrieNode () {
7+ this .isEnd = false ;
8+ this .children = new HashMap <>();
9+ }
10+ }
11+
12+ private final TrieNode root ;
13+
14+ public WordDictionary () {
15+ root = new TrieNode ();
16+ }
17+
18+ public void addWord (String word ) {
19+ TrieNode node = root ;
20+ for (char ch : word .toCharArray ()) {
21+ node .children .putIfAbsent (ch , new TrieNode ());
22+ node = node .children .get (ch );
23+ }
24+ node .isEnd = true ;
25+ }
26+
27+ public boolean search (String word ) {
28+ return dfs (root , word , 0 );
29+ }
30+
31+ private boolean dfs (TrieNode node , String word , int index ) {
32+ if (index == word .length ()) {
33+ return node .isEnd ;
34+ }
35+
36+ char ch = word .charAt (index );
37+ if (ch == '.' ) {
38+ for (TrieNode child : node .children .values ()) {
39+ if (dfs (child , word , index + 1 )) {
40+ return true ;
41+ }
42+ }
43+ return false ;
44+ }
45+
46+ TrieNode next = node .children .get (ch );
47+ if (next == null ) {
48+ return false ;
49+ }
50+
51+ return dfs (next , word , index + 1 );
52+ }
53+ }
54+
Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+
3+ class SolutionLIS {
4+ public int lengthOfLIS (int [] nums ) {
5+ int n = nums .length ;
6+ int [] dp = new int [n ];
7+ Arrays .fill (dp , 1 );
8+
9+ for (int current = 1 ; current < n ; current ++) {
10+ for (int prev = 0 ; prev < current ; prev ++) {
11+ if (nums [prev ] < nums [current ]) {
12+ dp [current ] = Math .max (dp [current ], dp [prev ] + 1 );
13+ }
14+ }
15+ }
16+
17+ int maxLength = 0 ;
18+ for (int len : dp ) {
19+ maxLength = Math .max (maxLength , len );
20+ }
21+ return maxLength ;
22+ }
23+ }
24+
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class SolutionSpiral {
4+ public List <Integer > spiralOrder (int [][] matrix ) {
5+ List <Integer > result = new ArrayList <>();
6+
7+ if (matrix == null || matrix .length == 0 ) return result ;
8+
9+ int top = 0 , bottom = matrix .length - 1 ;
10+ int left = 0 , right = matrix [0 ].length - 1 ;
11+
12+ while (top <= bottom && left <= right ) {
13+ // Traverse from left to right
14+ for (int col = left ; col <= right ; col ++) {
15+ result .add (matrix [top ][col ]);
16+ }
17+ top ++;
18+
19+ if (top > bottom ) break ;
20+
21+ for (int row = top ; row <= bottom ; row ++) {
22+ result .add (matrix [row ][right ]);
23+ }
24+ right --;
25+
26+ if (left > right ) break ;
27+
28+ for (int col = right ; col >= left ; col --) {
29+ result .add (matrix [bottom ][col ]);
30+ }
31+ bottom --;
32+
33+ for (int row = bottom ; row >= top ; row --) {
34+ result .add (matrix [row ][left ]);
35+ }
36+ left ++;
37+ }
38+
39+ return result ;
40+ }
41+ }
42+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isValid (String s ) {
3+ Stack <Character > checking = new Stack <>();
4+
5+ for (char c : s .toCharArray ()){
6+ if (c == '(' || c == '{' || c == '[' ){
7+ checking .push (c );
8+ continue ;
9+ }
10+ if (checking .empty ()) return false ;
11+
12+ if (c == ')' && checking .peek () == '(' ){
13+ checking .pop ();
14+ continue ;
15+ }
16+ if (c == '}' && checking .peek () == '{' ){
17+ checking .pop ();
18+ continue ;
19+ }
20+ if (c == ']' && checking .peek () == '[' ){
21+ checking .pop ();
22+ continue ;
23+ }
24+ return false ;
25+ }
26+ if (!checking .empty ()) return false ;
27+ return true ;
28+ }
29+ }
30+
You can’t perform that action at this time.
0 commit comments