File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * ํ์ด
3
+ * - container์ ๋ฐ๋ณ์ด ๋๊ณ ๋์ด๊ฐ ๋์ ์๋ก ์ ์ฅํ๋ ๋ฌผ์ ์์ด ๋ง์ต๋๋ค
4
+ * - ๋ฐ๋ผ์ ๋ฐ๋ณ์ด ๊ฐ์ฅ ๋์ container๋ถํฐ ํ์ํ๋ฉด ํ์ ํจ์จ์ ๋์ผ ์ ์๋ค๋ ์๊ฐ์ ํ์ต๋๋ค
5
+ * - ์ ๋์์๋ถ two pointer๋ฅผ ์ด์ฉํ์ฌ ํ์์ ์์ํฉ๋๋ค
6
+ * - lo, hi ์ค์์ ๋์ด๊ฐ ๋ ๋ฎ์ pointer๋ฅผ ์์ชฝ์ผ๋ก ์ด๋์ํต๋๋ค
7
+ * - ์๋ํ๋ฉด ๋์ด๊ฐ ๋ ๋ฎ์ pointer๋ฅผ ์ด๋์์ผฐ์ ๋ ๊ธฐ์กด container๋ณด๋ค ๋ ๋์ด๊ฐ ๋์ container๋ฅผ ๋ง๋ค ์ ์๋ ๊ฐ๋ฅ์ฑ์ด ์๊ธฐ๊ธฐ ๋๋ฌธ์
๋๋ค
8
+ *
9
+ * Big O
10
+ * - N: ์ฃผ์ด์ง ๋ฐฐ์ด height์ ํฌ๊ธฐ
11
+ *
12
+ * - Time complexity: O(N)
13
+ * - ๋ฐฐ์ด height๋ฅผ ์กฐํํ๋ฏ๋ก ์ ์ฒด ์คํ์๊ฐ ๋ํ N์ ๋น๋กํ์ฌ ์ ํ์ ์ผ๋ก ์ฆ๊ฐํฉ๋๋ค
14
+ *
15
+ * - Space complexity: O(1)
16
+ */
17
+
18
+ class Solution {
19
+ public:
20
+ int maxArea (vector<int >& height) {
21
+ int n = height.size ();
22
+ int lo = 0 ;
23
+ int hi = n - 1 ;
24
+
25
+ int res = 0 ;
26
+ while (lo < hi) {
27
+ int w = hi - lo;
28
+ int h = min (height[lo], height[hi]);
29
+
30
+ res = max (res, w * h);
31
+
32
+ if (height[lo] > height[hi]) {
33
+ --hi;
34
+ } else {
35
+ ++lo;
36
+ }
37
+ }
38
+
39
+ return res;
40
+ }
41
+ };
You canโt perform that action at this time.
0 commit comments