File tree Expand file tree Collapse file tree 4 files changed +146
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence Expand file tree Collapse file tree 4 files changed +146
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < vector>
2+ #include < iostream>
3+ #include < algorithm>
4+
5+ using namespace std ;
6+
7+ class Solution {
8+ public:
9+ int maxArea (vector<int >& height) {
10+ int left = 0 ;
11+ int right = height.size () - 1 ;
12+ int maxArea = 0 ;
13+
14+ while (left < right) {
15+
16+ int area = (right - left) * std::min (height[left], height[right]);
17+ if (area > maxArea)
18+ maxArea = area;
19+
20+ if (height[left] < height[right])
21+ left++;
22+ else
23+ right--;
24+
25+ }
26+ return (maxArea);
27+ }
28+ };
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < string>
3+
4+ using namespace std ;
5+
6+ struct TrieNode {
7+ bool isEnd;
8+ TrieNode* children[26 ];
9+ TrieNode () : isEnd(false ) {
10+ for (int i = 0 ; i < 26 ; i++)
11+ children[i] = nullptr ;
12+ }
13+ };
14+
15+ class WordDictionary {
16+ private:
17+ TrieNode* root;
18+ bool dfs (string& word, int idx, TrieNode* node) {
19+ if (!node)
20+ return (false );
21+ if (idx == word.size ()) // 단어가 끝났을 때만 true
22+ return (node->isEnd );
23+
24+ char c = word[idx];
25+ if (c == ' .' ) {
26+ for (int i = 0 ; i < 26 ; i++) {
27+ if (dfs (word, idx + 1 , node->children [i]))
28+ return (true );
29+ }
30+ return (false );
31+ }
32+ else {
33+ return dfs (word, idx + 1 , node->children [c - ' a' ]);
34+ }
35+ }
36+
37+ public:
38+ WordDictionary () {
39+ root = new TrieNode ();
40+ }
41+
42+ void addWord (string word) {
43+ TrieNode* cur = root;
44+ for (char c : word) {
45+ int index = c - ' a' ;
46+ if (!cur->children [index])
47+ cur->children [index] = new TrieNode ();
48+ cur = cur->children [index];
49+ }
50+ cur->isEnd = true ;
51+ }
52+
53+ bool search (string word) {
54+ return dfs (word, 0 , root);
55+ }
56+ };
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < vector>
3+ #include < cmath>
4+
5+ using namespace std ;
6+
7+ class Solution {
8+ public:
9+ int lengthOfLIS (vector<int >& nums) {
10+ vector<int > dp (nums.size (), 1 );
11+ int ans = 1 ;
12+
13+ for (int i = 1 ; i < nums.size (); i++) {
14+ for (int j = 0 ; j < i; j++) {
15+ if (nums[i] > nums[j])
16+ dp[i] = max (dp[i], dp[j] + 1 );
17+ ans = max (ans, dp[i]);
18+ }
19+ }
20+ return (ans);
21+ }
22+ };
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < stack>
3+ #include < string>
4+
5+ using namespace std ;
6+
7+ class Solution {
8+ public:
9+ bool isValid (string s) {
10+ stack<char > st;
11+
12+ for (char c : s) {
13+ // if (!st.empty())
14+ // cout << st.top();
15+ if (c == ' }' ) {
16+ if (st.empty () || st.top () != ' {' ) {
17+ return (false );
18+ }
19+ st.pop ();
20+ continue ;
21+ }
22+ else if (c == ' )' ) {
23+ if (st.empty () || st.top () != ' (' ) {
24+ return (false );
25+ }
26+ st.pop ();
27+ continue ;
28+ }
29+ else if (c == ' ]' ) {
30+ if (st.empty () || st.top () != ' [' ) {
31+ return (false );
32+ }
33+ st.pop ();
34+ continue ;
35+ }
36+ st.push (c);
37+ }
38+ return (st.empty ());
39+ }
40+ };
You can’t perform that action at this time.
0 commit comments