File tree Expand file tree Collapse file tree 5 files changed +253
-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 +253
-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 l = 0 ;
5+ int r = height.size () - 1 ;
6+
7+ int area = 0 ;
8+
9+ // ํฌ ํฌ์ธํฐ๋ก ์ ๊ทผ
10+ while (l < r)
11+ {
12+ // ๋๋น(๋ฌผ ์ ์ฅ๋)๋ฅผ ๊ตฌํ๊ณ ์ต๋๊ฐ ์
๋ฐ์ดํธ
13+ area = max (area, (r - l) * min (height[r], height[l]));
14+
15+ // ๋์ด๊ฐ ๋ฎ์ ์ชฝ์ ์ปค์๋ฅผ ์ด๋
16+ // -> ๋์ ์ชฝ์ ์ปค์๋ฅผ ์์ง์ด๋ฉด ๋ฌด์กฐ๊ฑด ์ํด
17+ // -> ๋๋น๋ ์ค์ด๋ค๊ณ ๋์ด๊น์ง ์ค์ด๋๋๊น
18+ if (height[l] < height[r])
19+ {
20+ l++;
21+ }
22+ else
23+ {
24+ r--;
25+ }
26+ }
27+
28+ return area;
29+ }
30+ };
Original file line number Diff line number Diff line change 1+ // ํ์ด ์ ๊ทผ: Trie๋ก ํด๊ฒฐ
2+
3+ class WordDictionary {
4+ public:
5+ WordDictionary () {
6+ mRoot = make_unique<Node>(); // RAII๋ฅผ ์ํด ์ค๋งํธ ํฌ์ธํฐ ์ฌ์ฉ
7+ }
8+
9+ void addWord (string word) {
10+ Node* curNode = mRoot .get ();
11+ for (char c : word)
12+ {
13+ auto & child = curNode->children [c - ' a' ];
14+ if (child == nullptr )
15+ {
16+ child = make_unique<Node>();
17+ }
18+
19+ curNode = child.get ();
20+ }
21+
22+ curNode->isWord = true ;
23+ }
24+
25+ bool search (string word) {
26+ return search_impl (word, mRoot .get ());
27+ }
28+
29+ private:
30+ struct Node
31+ {
32+ bool isWord = false ;
33+ unique_ptr<Node> children[26 ];
34+ };
35+
36+ unique_ptr<Node> mRoot ;
37+
38+ // ๋ณต์ฌ ์๋ substr์ ์ํด std::string_view ํ์ฉ
39+ bool search_impl (string_view word, Node* root)
40+ {
41+ Node* curNode = root;
42+ for (int i = 0 ; i < word.size (); ++i)
43+ {
44+ char c = word[i];
45+ if (c == ' .' ) // '.'์ ์์ผ๋ ์นด๋์ด๋ฏ๋ก ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ์ํํ๋ฉฐ ์๋
46+ {
47+ for (auto & p : curNode->children )
48+ {
49+ if (p != nullptr && search_impl (word.substr (i + 1 ), p.get ()))
50+ {
51+ return true ;
52+ }
53+ }
54+
55+ return false ;
56+ }
57+ else
58+ {
59+ auto & child = curNode->children [c - ' a' ];
60+ if (child == nullptr )
61+ {
62+ return false ;
63+ }
64+
65+ curNode = child.get ();
66+ }
67+ }
68+
69+ return curNode->isWord ;
70+ }
71+ };
Original file line number Diff line number Diff line change 1+ // O(n^2) ํ์ด
2+ class Solution {
3+ public:
4+ int lengthOfLIS (vector<int >& nums) {
5+ int n = nums.size ();
6+ vector<int > d (n); // ์ ํ์: d[i] = a[i]๋ฅผ ๋ง์ง๋ง์ผ๋ก ํ๋ LIS ๊ธธ์ด
7+
8+ for (int i = 0 ; i < n; ++i)
9+ {
10+ d[i] = 1 ; // ์ต์๊ฐ์ 1
11+ for (int j = 0 ; j < i; ++j)
12+ {
13+ // ์ฆ๊ฐํ๋ ๋ถ๋ถ์์ด์ด๊ณ , ๊ธฐ์กด์ ๊ตฌํ ๊ธธ์ด๋ณด๋ค ๊ธธ๋ฉด ์
๋ฐ์ดํธ
14+ if (nums[j] < nums[i] && d[j] + 1 > d[i])
15+ {
16+ d[i] = d[j] + 1 ;
17+ }
18+ }
19+ }
20+
21+ // d์ ์ต๋๊ฐ ๋ฐํ
22+ return *max_element (d.begin (), d.end ());
23+ }
24+ };
25+
26+ // O(n log n) ํ์ด
27+ class Solution {
28+ public:
29+ int lengthOfLIS (vector<int >& nums) {
30+ int n = nums.size ();
31+ vector<int > d; // d[i] = ๊ธธ์ด๊ฐ i + 1์ธ LIS์ ๊ฐ์ฅ ์์ ๋ง์ง๋ง ๊ฐ
32+
33+ for (int i = 0 ; i < n; ++i)
34+ {
35+ auto it = lower_bound (d.begin (), d.end (), nums[i]);
36+ if (it == d.end ()) // nums[i]๊ฐ d์ ๋ชจ๋ ์์๋ณด๋ค ํฐ ๊ฒฝ์ฐ
37+ {
38+ d.push_back (nums[i]);
39+ }
40+ else // nums[i]์ ์๋ฆฌ๋ฅผ ์ฐพ์ ๊ฒฝ์ฐ
41+ {
42+ *it = nums[i]; // ๊ทธ๋ฅ ๋์ฒดํ๋๊ฒ ์ด๋์ -> ๊ธธ์ด๋ ์ ์ง๋๊ณ , ๋ ์ด์ด๋ถ์ผ ์ ์๋ ์์ ๋ฒ์๋ ๋์ด๋๋ฏ๋ก
43+ }
44+ }
45+
46+ return d.size ();
47+ }
48+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ vector<int > spiralOrder (vector<vector<int >>& matrix) {
4+ int m = matrix.size ();
5+ int n = matrix[0 ].size ();
6+
7+ int top = 0 ;
8+ int bottom = m - 1 ;
9+ int left = 0 ;
10+ int right = n - 1 ;
11+
12+ vector<int > v;
13+ v.reserve (m * n);
14+
15+ while (top <= bottom && left <= right)
16+ {
17+ // ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋
18+ for (int i = left; i <= right; ++i)
19+ {
20+ v.push_back (matrix[top][i]);
21+ }
22+ top++;
23+
24+ // ์๋์ชฝ์ผ๋ก ์ด๋
25+ for (int i = top; i <= bottom; ++i)
26+ {
27+ v.push_back (matrix[i][right]);
28+ }
29+ right--;
30+
31+ // ์ผ์ชฝ์ผ๋ก ์ด๋
32+ if (top <= bottom)
33+ {
34+ for (int i = right; i >= left; --i)
35+ {
36+ v.push_back (matrix[bottom][i]);
37+ }
38+ bottom--;
39+ }
40+
41+ // ์์ชฝ์ผ๋ก ์ด๋
42+ if (left <= right)
43+ {
44+ for (int i = bottom; i >= top; --i)
45+ {
46+ v.push_back (matrix[i][left]);
47+ }
48+ left++;
49+ }
50+ }
51+
52+ return v;
53+ }
54+ };
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+ {
8+ if (isOpening (ch)) // ์ฌ๋ ๊ดํธ๋ผ๋ฉด ์คํ์ push
9+ {
10+ st.push (ch);
11+ continue ;
12+ }
13+
14+ if (st.empty ()) // ์คํ์ด ๋น์๋๋ฐ ๋ซ๋ ๊ดํธ๋ฉด invalid
15+ {
16+ return false ;
17+ }
18+
19+ char t = st.top ();
20+ st.pop ();
21+
22+ // ๊ดํธ ์์ด ๋ง์ผ๋ฉด ๊ณ์ ์งํ
23+ if ((ch == ' )' && t == ' (' )
24+ || (ch == ' ]' && t == ' [' )
25+ || (ch == ' }' && t == ' {' ))
26+ {
27+ continue ;
28+ }
29+
30+ // ๊ดํธ ์์ด ๋ง์ง ์์ผ๋ฉด invalid
31+ return false ;
32+ }
33+
34+ return st.empty ();
35+ }
36+
37+ private:
38+ bool isOpening (char c)
39+ {
40+ switch (c)
41+ {
42+ case ' (' :
43+ case ' [' :
44+ case ' {' :
45+ return true ;
46+ }
47+
48+ return false ;
49+ }
50+ };
You canโt perform that action at this time.
0 commit comments