File tree Expand file tree Collapse file tree 5 files changed +181
-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 +181
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int maxArea (vector<int >& height) {
4+ int result = 0 ;
5+ int left = 0 ;
6+ int right = height.size () - 1 ;
7+
8+ while (left < right){
9+ int len = right - left;
10+
11+ if (height[left] < height[right]){
12+ result = max (result, height[left] * len);
13+ left++;
14+ }else {
15+ result = max (result, height[right] * len);
16+ right--;
17+ }
18+ }
19+
20+ return result;
21+ }
22+ };
Original file line number Diff line number Diff line change 1+ class Trie {
2+ public:
3+ Trie* children[26 ];
4+ bool isEnd;
5+
6+ Trie (){
7+ for (int i = 0 ; i < 26 ; i++)
8+ children[i] = nullptr ;
9+
10+ isEnd = false ;
11+ }
12+ };
13+
14+ class WordDictionary {
15+ private:
16+ Trie* trie;
17+ public:
18+ WordDictionary () {
19+ trie = new Trie ();
20+ }
21+
22+ void addWord (string word) {
23+ Trie* node = trie;
24+
25+ for (char ch : word){
26+ int index = ch - ' a' ;
27+
28+ if (node->children [index] == nullptr )
29+ node->children [index] = new Trie ();
30+
31+ node = node->children [index];
32+ }
33+
34+ node->isEnd = true ;
35+ }
36+
37+ bool dfs (Trie* node, int index, string word){
38+ if (node == nullptr )
39+ return false ;
40+
41+ if (index == word.length ())
42+ return node->isEnd ;
43+
44+ char ch = word[index];
45+
46+ if (ch == ' .' ){
47+ for (int i = 0 ; i < 26 ; i++){
48+ if (dfs (node->children [i], index + 1 , word) == true )
49+ return true ;
50+ }
51+ }else
52+ return dfs (node->children [ch-' a' ], index + 1 , word);
53+
54+ return false ;
55+ }
56+
57+ bool search (string word) {
58+ return dfs (trie, 0 , word);
59+ }
60+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int lengthOfLIS (vector<int >& nums) {
4+ vector<int > sub;
5+
6+ for (int num : nums){
7+ auto it = lower_bound (sub.begin (), sub.end (), num);
8+
9+ if (it == sub.end ())
10+ sub.push_back (num);
11+ else
12+ *it = num;
13+ }
14+
15+ return sub.size ();
16+ }
17+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ vector<int > spiralOrder (vector<vector<int >>& matrix) {
4+ enum direction { R, D, L, U };
5+
6+ enum direction dir = R;
7+ vector<int > result;
8+ int min_R = 0 ;
9+ int max_R = matrix.size () - 1 ;
10+ int min_C = 0 ;
11+ int max_C = matrix[0 ].size () - 1 ;
12+ int r = 0 ;
13+ int c = 0 ;
14+ int size = matrix.size () * matrix[0 ].size ();
15+
16+ while (result.size () < size){
17+ result.push_back (matrix[r][c]);
18+
19+ switch (dir){
20+ case R:
21+ if (c == max_C){
22+ dir = D;
23+ min_R++;
24+ r++;
25+ }else
26+ c++;
27+ break ;
28+ case D:
29+ if (r == max_R){
30+ dir = L;
31+ max_C--;
32+ c--;
33+ }else
34+ r++;
35+ break ;
36+ case L:
37+ if (c == min_C){
38+ dir = U;
39+ max_R--;
40+ r--;
41+ }else
42+ c--;
43+ break ;
44+ case U:
45+ if (r == min_R){
46+ dir = R;
47+ min_C++;
48+ c++;
49+ }else
50+ r--;
51+ break ;
52+ }
53+ }
54+
55+ return result;
56+ }
57+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ bool isValid (string s) {
4+ stack<char > st;
5+
6+ for (char ch : s){
7+ if (ch == ' )' ){
8+ if (st.empty () || st.top () != ' (' )
9+ return false ;
10+ st.pop ();
11+ }else if (ch == ' }' ){
12+ if (st.empty () || st.top () != ' {' )
13+ return false ;
14+ st.pop ();
15+ }else if (ch == ' ]' ){
16+ if (st.empty () || st.top () != ' [' )
17+ return false ;
18+ st.pop ();
19+ }else
20+ st.push (ch);
21+ }
22+
23+ return st.empty ();
24+ }
25+ };
You can’t perform that action at this time.
0 commit comments