Skip to content

Commit 3c862ad

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 27dedf6 + b5e01c3 commit 3c862ad

53 files changed

Lines changed: 1788 additions & 10 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function maxProfit(prices: number[]): number {
2+
let minPrice = Number.MAX_SAFE_INTEGER;
3+
let maxProfit = 0;
4+
5+
for (let p of prices) {
6+
if (minPrice >= p) {
7+
minPrice = p;
8+
continue;
9+
} else {
10+
let profit = p - minPrice;
11+
if (profit > maxProfit) maxProfit = profit;
12+
}
13+
}
14+
return maxProfit;
15+
}
16+
17+
function maxProfit(prices: number[]): number {
18+
let minPrice = Infinity;
19+
let maxProfit = 0;
20+
21+
for (let p of prices) {
22+
minPrice = Math.min(minPrice, p);
23+
maxProfit = Math.max(maxProfit, p - minPrice);
24+
}
25+
return maxProfit;
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// tc: O(n);
2+
// sc: O(1);
3+
const maxArea = function (height) {
4+
let max = 0;
5+
let leftIdx = 0;
6+
let rightIdx = height.length - 1;
7+
8+
while (leftIdx < rightIdx) {
9+
const width = rightIdx - leftIdx;
10+
const minHeight = Math.min(height[leftIdx], height[rightIdx]);
11+
const area = width * minHeight;
12+
max = Math.max(max, area);
13+
14+
if (height[leftIdx] > height[rightIdx]) {
15+
rightIdx -= 1;
16+
} else {
17+
leftIdx += 1;
18+
}
19+
}
20+
21+
return max;
22+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn max_area(height: Vec<i32>) -> i32 {
5+
let mut max_area = 0;
6+
let (mut left, mut right) = (0, height.len() - 1);
7+
while left < right {
8+
let width = (right - left) as i32;
9+
let min_height = height[left].min(height[right]);
10+
let area = width * min_height;
11+
max_area = max_area.max(area);
12+
if height[left] < height[right] {
13+
left += 1;
14+
} else {
15+
right -= 1;
16+
}
17+
}
18+
max_area
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
# Approach
3+
ํˆฌํฌ์ธํ„ฐ๋กœ ์–‘ ๋์—์„œ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค.
4+
๋” ๋‚ฎ์€ height์ธ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ€๋Š”๋ฐ, ๋†’์ด๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์™ผ์ชฝ ํฌ์ธํ„ฐ๋งŒ ์›€์ง์ž…๋‹ˆ๋‹ค.
5+
6+
# Complexity
7+
height์˜ ๊ธธ์ด๊ฐ€ N์ผ ๋•Œ
8+
- Time complexity: O(N)
9+
- Space complexity: O(1)
10+
"""
11+
12+
13+
class Solution:
14+
def maxArea(self, height: list[int]) -> int:
15+
n = len(height)
16+
left, right = 0, n - 1
17+
best = 0
18+
while left < right:
19+
best = max(best, (right - left) * min(height[left], height[right]))
20+
if height[left] > height[right]:
21+
right -= 1
22+
else:
23+
left += 1
24+
return best
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
[๊ฒฐ๊ณผ ์š”์•ฝ]
3+
# ์‹œ๋„ํ•œ ๋กœ์ง ์ˆ˜: 3
4+
1. ๋ชจ๋“  ๊ฒฐ๊ณผ๋ฃฐ ์ˆœํšŒํ•˜๋ฉด์„œ ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2)์„ ์‹œ๋„ -> LeetCode์—์„œ Time Limit Exceed ๋ฐœ์ƒ
5+
2. ๋ฆฌ์ŠคํŠธ ์–‘์ชฝ ๋์—์„œ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜: ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ชจ๋‘ O(n)
6+
3. (2)์˜ ๋กœ์ง์€ ์œ ์ง€ํ•œ ์ฑ„๋กœ ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์„ ๊ฐœ์„ (์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ๋ฆ„์„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก)
7+
"""
8+
9+
10+
class Solution:
11+
def maxArea(self, height: list[int]) -> int:
12+
left, right = 0, len(height) - 1
13+
max_area = 0
14+
15+
while left < right:
16+
left_height, right_height = height[left], height[right]
17+
min_height = min(left_height, right_height)
18+
width = right - left
19+
20+
current_area = width * min_height
21+
max_area = max(max_area, current_area)
22+
23+
if left_height < right_height:
24+
left += 1
25+
else:
26+
right -= 1
27+
28+
return max_area
29+
30+
31+
if __name__ == "__main__":
32+
test_cases = [
33+
([1, 1], 1),
34+
([1, 2, 1], 2),
35+
([4, 3, 2, 1, 4], 16),
36+
([1, 8, 6, 2, 5, 4, 8, 3, 7], 49),
37+
([1, 2, 4, 3], 4),
38+
([2, 3, 10, 5, 7, 8, 9], 36),
39+
([1, 3, 2, 5, 25, 24, 5], 24),
40+
([5, 5, 5, 5, 5], 20),
41+
([1, 1000, 1, 1000, 1], 2000),
42+
([1, 2, 3, 4, 5, 6], 9),
43+
]
44+
45+
solution = Solution()
46+
for idx, case_ in enumerate(test_cases):
47+
height, answer = case_
48+
result = solution.maxArea(height)
49+
assert (
50+
answer == result
51+
), f"Test Case {idx + 1} Failed: Expected {answer}, Got {result}"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function maxArea(height: number[]): number {
2+
let left = 0;
3+
let right = height.length - 1;
4+
let max = -1;
5+
6+
while (right > left) {
7+
const v = Math.min(height[left], height[right]) * (right - left);
8+
max = Math.max(max, v);
9+
if (height[left] >= height[right]) {
10+
right--;
11+
} else {
12+
left++;
13+
}
14+
}
15+
return max;
16+
}
17+
18+
/*
19+
1. Math.min(height[left], height[right]) * (right - left);
20+
2. left ์™€ right ๋ฅผ ์–‘ ๋์—์„œ ๊ฐ€๋ฆฌํ‚ด
21+
3. ๋„ˆ๋น„๋ฅผ ๊ณ„์‚ฐํ•ด์„œ max ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ณ , ๋‘˜ ์ค‘ ๋” ์งง์€ ์ชฝ์„ ์•ˆ์ชฝ์œผ๋กœ ์ด๋™
22+
4. left ์™€ right ๊ฐ€ ๋งŒ๋‚˜๋ฉด ์ข…๋ฃŒ
23+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
'''
4+
1.๋ฌธ์ œ: ๊ฐ€์žฅ ๋งŽ์€ ์–‘์˜ ๋ฌผ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” max value return
5+
2.์กฐ๊ฑด
6+
- n: ๋†’์ด๋ฅผ ์˜๋ฏธ, ์ตœ์†Œ = 5, ์ตœ๋Œ€ = 10^5
7+
- ์›์†Œ๊ฐ’ ์ตœ์†Œ = 0, ์ตœ๋Œ€ = 10^4
8+
3.ํ’€์ด
9+
- ๋†’์ด๋Š” height[i], height[j] ์ค‘์— ์ž‘์€ ๊ฐ’, ๊ฐ€๋กœ๋Š” abs(i-j)
10+
- output = ๋†’์ด x ๊ฐ€๋กœ
11+
-> 2์ค‘ loop ๋Š” O(n^2) ๋กœ TLE ๋ฐœ์ƒ.
12+
-> two pointer ๋กœ O(n) ์œผ๋กœ ํ•ด๊ฒฐ!
13+
'''
14+
15+
n = len(height)
16+
maxArea = 0
17+
18+
left = 0
19+
right = n-1
20+
21+
while left < right:
22+
curArea = abs(right-left) * min(height[left], height[right])
23+
maxArea = max(curArea, maxArea)
24+
25+
#height ์ด ๋‚ฎ์€์ชฝ pointer update
26+
if height[left] < height[right]:
27+
left += 1
28+
else:
29+
right -= 1
30+
31+
return maxArea
32+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxArea(self, height: list[int]) -> int:
3+
# ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ ธ ์žˆ์œผ๋ฉด์„œ ๋†’์ด๊ฐ€ ๋น„์Šทํ•œ ๋ง‰๋Œ€๊ธฐ?
4+
max_area = 0
5+
i, j = 0, len(height) - 1
6+
while i < j:
7+
left, right = height[i], height[j]
8+
area = min(left, right) * (j - i)
9+
if area > max_area:
10+
max_area = area
11+
if left < right:
12+
i += 1
13+
else:
14+
j -= 1
15+
return max_area

โ€Žcontainer-with-most-water/liza0525.pyโ€Ž

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,36 @@ def maxArea(self, height: List[int]) -> int:
2929
)
3030

3131
return result_area
32+
33+
34+
# 7๊ธฐ ํ’€์ด
35+
# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
36+
# - two point ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— heights ๋ณ€์ˆ˜์˜ ๊ธธ์ด์ธ n์— ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ์ •ํ•ด์ง
37+
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
38+
# - ๋ช‡ ๊ฐœ์˜ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•จ
39+
class Solution:
40+
def maxArea(self, height: List[int]) -> int:
41+
# ์–‘ ๋ ํฌ์ธํ„ฐ ์ง€์ •
42+
start, end = 0, len(height) - 1
43+
44+
# max_area๋Š” ํ˜„์žฌ ํฌ์ธํ„ฐ๋“ค์„ ์ด์šฉํ–ˆ์„ ๋•Œ์˜ ๋„“์ด ๊ณ„์‚ฐ
45+
# ์ด๋•Œ ๋†’์ด๋Š” ์ž‘์€ ์ชฝ์œผ๋กœ ์„ ํƒํ•˜์—ฌ ๊ณ„์‚ฐํ•ด์•ผ ํ•จ
46+
max_area = min(height[start], height[end]) * (end - start)
47+
48+
# start๊ฐ€ end๋ณด๋‹ค ์ž‘์„ ๋•Œ ๋ฃจํ”„ ๋Œ๊ธฐ
49+
while start < end:
50+
# ๋ฌธ์ œ์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š”, ๋†’์ด๊ฐ€ ํฐ ์ชฝ์„ ์›€์ง์ด๋ฉด ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ์„ ์ด ํ˜„์žฌ๋ณด๋‹ค ํด ์ˆ˜ ์—†๋‹ค.
51+
# ์ด๋Š”, start์˜ ๋†’์ด์™€ end์˜ ๋†’์ด ์ค‘ ํฐ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ๊ณ ์ •ํ•˜์—ฌ ์›€์ง์—ฌ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ
52+
# (๋ฌผ์˜ ๋†’์ด๋Š” ์ž‘์€ ์ชฝ์— ์˜ํ•ด ์ •ํ•ด์ง€๋Š”๋ฐ, ํฐ ์ชฝ ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ด๋ฉด x ๊ธธ์ด๋งŒ ์งง์•„์ง€๊ฒŒ ๋˜์–ด ์˜คํžˆ๋ ค ๋„“์ด์ด ์ค„์–ด๋“ฆ)
53+
if height[start] < height[end]:
54+
# start ์ชฝ์˜ ๋†’์ด๊ฐ€ ๋‚ฎ์œผ๋ฉด start๋ฅผ ์›€์ง์ž„
55+
start += 1
56+
else:
57+
# end ์ชฝ์˜ ๋†’์ด๊ฐ€ ๋‚ฎ์œผ๋ฉด end๋ฅผ ์›€์ง์ž„
58+
end -= 1
59+
60+
# ํฌ์ธํ„ฐ๋ฅผ ์˜ฎ๊ธฐ๊ณ  ๋‚œ ํ›„์˜ ๋„“์ด๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ, ๊ธฐ์กด์˜ max_area์™€ ๋น„๊ตํ•˜์—ฌ ํฐ ์ชฝ์œผ๋กœ ์—…๋ฐ์ดํŠธ
61+
curr_area = min(height[start], height[end]) * (end - start)
62+
max_area = max(max_area, curr_area)
63+
64+
return max_area

0 commit comments

Comments
ย (0)