File tree Expand file tree Collapse file tree 4 files changed +108
-6
lines changed
container-with-most-water
longest-increasing-subsequence Expand file tree Collapse file tree 4 files changed +108
-6
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ ํ์ด :
3+ ์ต๋ ๋์ด๋ ์ ๋ ๊ธฐ๋ฅ ๊ธธ์ด ์ค ์งง์ ์ชฝ์ ๊ธฐ์ค์ผ๋ก ์ ํด์ง๋ค
4+ ์ ๋์์ ๊ธฐ๋ฅ์ ์์ํ๊ณ ๋ ์ค ์งง์ ์ชฝ์ ์์ชฝ์ผ๋ก ์ด๋ํ๋ฉด์ ์ต๋ ๋์ด๋ฅผ ์ฐพ๋๋ค
5+
6+ height์ ๊ฐ์ : N
7+
8+ TC : O(N)
9+
10+ SC : O(1)
11+ */
12+
113class Solution {
214 public:
315 int maxArea (vector<int >& height) {
416 int left = 0 ;
517 int right = height.size () - 1 ;
6- int ans = 0 ;
18+ int max_area = 0 ;
719
820 while (left < right)
921 {
10- int cur = (right - left) * min (height[left], height[right]);
11- if (cur > ans )
12- ans = cur ;
22+ int cur_area = (right - left) * min (height[left], height[right]);
23+ if (cur_area > max_area )
24+ max_area = cur_area ;
1325 if (height[left] <= height[right])
1426 left++;
1527 else
1628 right--;
1729 }
18- return ans ;
30+ return max_area ;
1931 }
2032 };
Original file line number Diff line number Diff line change 1+ /*
2+ nums์ ๊ธธ์ด๊ฐ ๊ฐ์ dp๋ฐฐ์ด์ ๋ง๋ค๊ณ 1๋ก ์ด๊ธฐํํ๋ค
3+ dp[i]์ ๊ฐ์ nums[i]์ผ๋ก ๋๋๋ LIS์ ๊ธธ์ด
4+ i ์ดํ์ j์ ๋ํด nums[j] > nums[i]์ผ ๊ฒฝ์ฐ nums[j]๋ฅผ nums[i]๋ก ๋๋๋ LIS์ ๋ถ์ผ ์ ์์ผ๋ฏ๋ก
5+ dp[j]์ dp[i + 1]์ ๋น๊ตํด ํฐ ๊ฐ์ผ๋ก ์
๋ฐ์ดํธ
6+
7+ nums์ ๊ธธ์ด : N
8+
9+ TC : O(N^2)
10+ ๋ฐ๋ณต๋ฌธ ๋ด๋ถ์ ๋ฐ๋ณต๋ฌธ
11+
12+ SC : O(N)
13+ dp์ ๊ธธ์ด๋ nums๊ธธ์ด์ ๋น๋ก
14+ */
15+
16+ class Solution {
17+ public:
18+ int lengthOfLIS (vector<int >& nums) {
19+ vector<int > dp (nums.size (), 1 );
20+ int max_len = 1 ;
21+
22+ for (int i = 0 ; i < nums.size (); i++)
23+ for (int j = i + 1 ; j < nums.size (); j++)
24+ {
25+ if (nums[j] > nums[i])
26+ {
27+ dp[j] = max (dp[i] + 1 , dp[j]);
28+ max_len = max (dp[j], max_len);
29+ }
30+ }
31+ return max_len;
32+ }
33+ };
Original file line number Diff line number Diff line change 1+ /*
2+ ํ์ด :
3+ ์ํ์ข์ฐ ๋ฒ์์ง์ ํด์ ์ด๋ํ๋ ๊ฒ์ด ์๋๋ผ n_rows, n_cols ๊ธธ์ด๋งํผ ์ด๋์ผ๋ก ํ์ด
4+ col ๋ฐฉํฅ์ผ๋ก ์์ง์ด๋ฉด ์ด๋ํ row ๊ฐ์, row ๋ฐฉํฅ ์์ง์ด๋ฉด n_cols 1 ๊ฐ์
5+ row, col ํ๋ฒ์ฉ ์ด๋ํ๋ฉด direction *= -1๋ก ๋ฐฉํฅ์ ๋ฐ๊ฟ์ค๋ค
6+
7+ matrix ํฌ๊ธฐ : M * N
8+
9+ TC : O(M * N)
10+ matrix ์ ์ฒด ์ํ
11+
12+ SC : O(1)
13+ ๋ฆฌํดํ ans ์ ์ธํ๋ฉด ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์์ ๊ฐ์์ ๋ณ์
14+ */
15+
16+ class Solution {
17+ public:
18+ vector<int > spiralOrder (vector<vector<int >>& matrix) {
19+ int n_rows = matrix.size ();
20+ int n_cols = matrix[0 ].size ();
21+ vector<int > ans;
22+
23+ int row = 0 , col = -1 ;
24+ int direction = 1 ;
25+
26+ while (n_rows > 0 && n_cols > 0 )
27+ {
28+ for (int i = 0 ; i < n_cols; i++)
29+ {
30+ col += direction;
31+ ans.push_back (matrix[row][col]);
32+ }
33+ n_rows--;
34+
35+ for (int i = 0 ; i < n_rows; i++)
36+ {
37+ row += direction;
38+ ans.push_back (matrix[row][col]);
39+ }
40+ n_cols--;
41+
42+ direction *= -1 ;
43+ }
44+ return ans;
45+ }
46+ };
Original file line number Diff line number Diff line change 11/*
2-
2+ ํ์ด :
3+ stack์ ์ด์ฉํด์ ๊ดํธ๋ฅผ ๋ฃ๊ณ ๋บธ๋ค.
4+ ๋ซ๋ ๊ดํธ ),],}๋ฅผ ๋ง๋ฌ์ ๋ ๊ฐ์ฅ ์์ ๊ดํธ๊ฐ ๋์ํ๋ ๊ดํธ๋ฉด ์ ๊ฑฐํ๊ณ ๋น์ด์๊ฑฐ๋ ๋ง์ง ์๋ ๊ดํธ๋ฉด return false
5+ string์ ๋ชจ๋ ์ํํ์ ๋ stack์ด ๋น์ด์์ง ์์ผ๋ฉด return false
6+
7+ s์ ๊ธธ์ด N
8+
9+ TC : O(N)
10+ s์ ๋ํด ๋ฐ๋ณต 1ํ
11+
12+ SC : O(N)
13+ ์ต์
์ ๊ฒฝ์ฐ stack ํฌ๊ธฐ๋ s์ ๊ธธ์ด์ ๋น๋ก
314*/
415
516class Solution {
You canโt perform that action at this time.
0 commit comments