Skip to content

Commit a6f4a33

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents e014aea + cc5da6c commit a6f4a33

File tree

130 files changed

+5558
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+5558
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
minimum = prices[0]
4+
answer = 0
5+
for i in range(1, len(prices)):
6+
if minimum > prices[i]:
7+
minimum = prices[i]
8+
else:
9+
diff = prices[i] - minimum
10+
if answer < diff:
11+
answer = diff
12+
return answer
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Solution: Brute Force
3+
Time: O(n^2)
4+
Space: O(1)
5+
"""
6+
7+
8+
class Solution:
9+
def maxArea(self, height: List[int]) -> int:
10+
max_water = 0
11+
for i in range(len(height) - 1):
12+
for j in range(i + 1, len(height)):
13+
h = min(height[i], height[j])
14+
w = j - i
15+
max_water = max(h * w, max_water)
16+
17+
return max_water
18+
19+
20+
"""
21+
Solution: Two Pointer
22+
1) ํฌ์ธํ„ฐ ์ด๋™์€ left height, right height ์ค‘ ์ž‘์€ value๋ฅผ ๊ฐ€์งˆ ๊ฒฝ์šฐ ์ด๋™
23+
Time: O(n)
24+
Space: O(1)
25+
"""
26+
27+
28+
class Solution:
29+
def maxArea(self, height: List[int]) -> int:
30+
l = 0
31+
r = len(height) - 1
32+
max_water = 0
33+
34+
while l < r:
35+
h = min(height[l], height[r])
36+
w = r - l
37+
max_water = max(h * w, max_water)
38+
39+
if height[l] < height[r]:
40+
l += 1
41+
else:
42+
r -= 1
43+
return max_water
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package leetcode_study
2+
3+
/*
4+
* ์ฃผ์–ด์ง„ ๋†’์ด์—์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.
5+
* Brute force๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ์ ธ๊ฐ€๋ฉฐ ์ตœ๋Œ€ ๋ฉด์ ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•.
6+
* ์ฃผ์–ด์ง„ ๋†’์ด(n)์˜ ๊ฐœ์ˆ˜๋Š” 2 ๋ณด๋‹ค ํฌ๊ฑฐ๊ฐ€ ๊ฐ™๊ณ  10^4 ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ.
7+
* ์ด์ค‘ Loop์œผ๋กœ ํ•ด๊ฒฐํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฐœ์ƒ (10^8 ์ดํ•˜๋กœ ํ•ด๊ฒฐํ•ด์•ผ ์ œํ•œ ์‹œ๊ฐ„ ์•ˆ์œผ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ)
8+
*
9+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^2)
10+
* -> ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋†’์ด๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰: O(n^2)
11+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
12+
* -> ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์—†์Œ.
13+
* */
14+
fun maxArea01(height: IntArray): Int {
15+
var maxValue = 0
16+
for (i in 0 until height.size) {
17+
for (j in i + 1 until height.size) {
18+
// ๋„ˆ๋น„๋Š” ๋‘ ์„ ๋ถ„ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ
19+
val width = j - i
20+
// ๋†’์ด๋Š” ๋‘ ์„ ๋ถ„ ์ค‘ ์ž‘์€ ๊ฐ’
21+
val containerHeight = Math.min(height[i], height[j])
22+
// ๋ฉด์  ๊ณ„์‚ฐ
23+
val area = width * containerHeight
24+
// ์ตœ๋Œ€๊ฐ’ ๊ฐฑ์‹ 
25+
maxValue = Math.max(maxValue, area)
26+
}
27+
}
28+
return maxValue
29+
}
30+
31+
/*
32+
* ์ด์ค‘ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ ๋ฌธ์ œ ํ’€์ด.
33+
* ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ์ฃผ๋Š” ์กฐ๊ฑด๊ณผ ์š”์†Œ
34+
* -> ๋†’์ด์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์™ผ์ชฝ๊ฐ’๊ณผ ์˜ค๋ฅธ์ชฝ ๊ฐ’์—์„œ ๋‘ ๊ฐ’ ์ค‘ ์ž‘์€ ๊ฐ’์ด ๋†’์ด๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ.
35+
* -> ์˜ค๋ฅธ์ชฝ์˜ ๊ฐ’์€ ์™ผ์ชฝ ๊ฐ’๋ณด๋‹ค ์ž‘์„ ์ˆ˜ ์—†์Œ.
36+
* -> ๋„ˆ๋น„ ๊ฐ’์€ ์˜ค๋ฅธ์ชฝ ์ธ๋ฑ์Šค์—์„œ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋ฅผ ๋บ€ ๊ฐ’์ž„.
37+
*
38+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
39+
* -> ์ฃผ์–ด์ง„ ๋†’์ด ๋ฐฐ์—ด์—์„œ ์–‘์ชฝ ๋ ๊ฐ’์„ ์ฆ๊ฐ/๊ฐ€๊ฐ ํ•ด๊ฐ€๋ฉฐ ๋ฐ˜๋ณต ์ง„ํ–‰: O(n)
40+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
41+
* -> ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์—†์Œ.
42+
* */
43+
fun maxArea02(height: IntArray): Int {
44+
var maxValue = 0
45+
var left = 0
46+
var right = height.size - 1
47+
while (left <= right) {
48+
val width = right - left
49+
val containerHeight = Math.min(height[left], height[right])
50+
val area = width * containerHeight
51+
maxValue = Math.max(maxValue, area)
52+
if (height[left] < height[right]) {
53+
left++
54+
} else {
55+
right--
56+
}
57+
}
58+
return maxValue
59+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
/**
4+
1. understanding
5+
- with two pair of line, each can contain "min(line1,line2) * (distance)" mount of water
6+
- find maximum amount of water can contain
7+
2. strategy
8+
- brute force
9+
- for each pair of lines, calculate amount and update maximum amount.
10+
- it can takes O(N), where N is the count of lines.
11+
- N is 10^5 at most, so it can takes 10^10, which can takes about 10 seconds
12+
- you need to swap out unnecessary calculation
13+
- so, let's find a unnecessary calculation in this problem.
14+
3. complexity
15+
- time: O(N)
16+
- space: O(1)
17+
*/
18+
int l = 0;
19+
int r = height.length - 1;
20+
int maxAmount = amountOf(height, l, r); // Math.min(height[l], height[r], r-l);
21+
while (l < r) { // O(N)
22+
maxAmount = Math.max(maxAmount, amountOf(height, l, r));
23+
if (height[l] < height[r]) {
24+
l++;
25+
} else if (height[l] > height[r]) {
26+
r--;
27+
} else {
28+
int nextLeftAmount = amountOf(height, l+1, r);
29+
int nextRightAmount = amountOf(height, l, r-1);
30+
if (nextLeftAmount < nextRightAmount) {
31+
r--;
32+
} else {
33+
l++;
34+
}
35+
}
36+
}
37+
38+
return maxAmount;
39+
}
40+
41+
private int amountOf(int[] height, int left, int right) {
42+
return (Math.min(height[left], height[right]) * (right - left));
43+
}
44+
}
45+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
3+
4+
/**
5+
* @param {number[]} height
6+
* @return {number}
7+
*/
8+
var maxArea = function (height) {
9+
let maxWater = 0;
10+
let left = 0;
11+
let right = height.length - 1;
12+
13+
while (left < right) {
14+
const lowerHeight = Math.min(height[left], height[right]);
15+
const width = right - left;
16+
maxWater = Math.max(maxWater, lowerHeight * width);
17+
18+
if (height[left] < height[right]) {
19+
left++;
20+
} else {
21+
right--;
22+
}
23+
}
24+
25+
return maxWater;
26+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(n)
2+
# ๊ณต๊ฐ„ ๋ณต์žก๋„ : O(1)
3+
# ๋ฌธ์ œ ์œ ํ˜• : Two Pointer
4+
class Solution:
5+
def maxArea(self, height: List[int]) -> int:
6+
left, right = 0, len(height) - 1
7+
max_area = 0
8+
9+
while left < right:
10+
max_area = max(max_area, (right - left) * min(height[left], height[right]))
11+
if height[left] < height[right]:
12+
left += 1
13+
else:
14+
right -= 1
15+
16+
return max_area
17+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
๏ปฟ๏ปฟ #ํ•ด์„
2+
#s๋Š” start index, e๋Š” ending index๋กœ ํ• ๋‹นํ•œ๋‹ค.
3+
#area ์— (e-s)*min(height[e], height[s]) ๋กœ ๋ฉด์ ์˜ ๋„“์ด๋ฅผ ๊ตฌํ•œ๋‹ค.
4+
#๋งŒ์•ฝ height[s]๊ฐ€ height[e] ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ํ˜„ area๋ณด๋‹ค ๋” ํฐ ๊ฒฐ๊ด๊ฐ’์„ ์œ„ํ•ด ๋ณ€ํ™”๋ฅผ ์ค€๋‹ค.
5+
# (e-s)์—์„œ e๋ฅผ ์ค„์–ด๋“ค๋ฉด ํ•„์—ฐ์ ์œผ๋กœ area ๊ฐ’์ด ๊ธฐ์กด๋ณด๋‹ค ์ ์–ด์ง„๋‹ค. ๋”ฐ๋ผ์„œ s์— 1์„ ๋”ํ•ด ์ธ๋ฑ์Šค๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™์‹œ์ผœ height[s] ์— ๋ณ€ํ™”๋ฅผ ์ค€๋‹ค.
6+
#๊ทธ ์™ธ์˜ ์ƒํ™ฉ์—๋Š” height[e]๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๊ธฐ ์œ„ํ•ด e์— 1๋ฅผ ๋นผ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
7+
#ํ•ด๋‹น ๋ฃจํ”„๋Š” s๊ฐ€ e๋ณด๋‹ค ์ž‘์„๋•Œ ์ž‘์šฉ๋œ๋‹ค. ๋งŒ์•ฝ s์˜ ์ฆ๊ฐ€์™€ e์˜ ๊ฐ์†Œ๋กœ ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๋งˆ์ฃผ์น˜๋ฉด ์ข…๋ฃŒํ•œ ํ›„ max_area๋ฅผ return์‹œํ‚จ๋‹ค.
8+
9+
10+
11+
#Big O
12+
#- N: height์˜ element ๊ฐฏ์ˆ˜
13+
14+
#Time Complexity: O(N)
15+
#- while : s์™€ e๊ฐ€ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€ ์ตœ๋Œ€ N๋ฒˆ ๋ฐ˜๋ณต๋œ๋‹ค. ๊ฐ ๋ฐ˜๋ณต์—์„œ์˜ ์—ฐ์‚ฐ๋“ค์€ O(1)์— ํ•ด๋‹น๋œ๋‹ค. -> O(N)
16+
17+
18+
#Space Complexity: O(1)
19+
#- s,e,max_area: ๋ณ€์ˆ˜๋Š” ์ƒ์ˆ˜๋กœ ์ž‘์šฉ๋œ๋‹ค -> O(1)
20+
####
21+
#
22+
#
23+
class Solution(object):
24+
def maxArea(self, height):
25+
"""
26+
:type height: List[int]
27+
:rtype: int
28+
"""
29+
max_area = 0 #Saving for answer
30+
s,e=0,len(height)-1 #Assign the first index and last index
31+
while s<e:
32+
area = (e-s) * min(height[s],height[e]) #Current area using e,s
33+
max_area = max(area, max_area) #Re-assing the max_area comparing with area
34+
if height[s]< height[e]:
35+
s+=1
36+
else:
37+
e -=1
38+
return max_area
39+
40+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
6+
// Time Complexity: O(n)
7+
// Space Complexity: O(1)
8+
var maxArea = function (height) {
9+
let start = 0;
10+
let end = height.length - 1;
11+
let maxArea = 0;
12+
13+
while (start < end) {
14+
const area = (end - start) * Math.min(height[start], height[end]);
15+
maxArea = Math.max(area, maxArea);
16+
17+
// The shorter height limits the area.
18+
// By moving the pointer associated with the shorter height,
19+
// the algorithm maximizes the chance of finding a taller line that can increase the area.
20+
// This is the essence of the two-pointer strategy for the container problem.
21+
if (height[start] < height[end]) {
22+
start += 1;
23+
} else {
24+
end -= 1;
25+
}
26+
}
27+
return maxArea;
28+
};
29+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Constraints:
3+
1. n == height.length
4+
2. 2 <= n <= 10^5
5+
3. 0 <= height[i] <= 10^4
6+
7+
Time Complexity:
8+
-
9+
10+
Space Complexity:
11+
-
12+
"""
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* ๋ฐฐ์—ด์˜ ๋‘๊ฐœ์˜ ๋†’์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์ตœ๋Œ€ ์šฉ๋Ÿ‰์„ ๊ตฌํ•˜๊ธฐ
3+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
4+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
5+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
6+
* @param height
7+
*/
8+
function maxArea(height: number[]): number {
9+
// end - start * min(height[start], height[end])๊ฐ€ ํฐ ๊ฒƒ
10+
11+
// 8 - 0 * min(1, 7) = 8
12+
// 8 - 1 * min(8, 7) = 49
13+
// 7 - 1 * min(8, 3) = 18
14+
// 6 - 1 * min(8, 8) = 40
15+
// ...
16+
17+
let s = 0
18+
let e = height.length - 1
19+
let curr = 0
20+
let max = 0
21+
22+
while(s < e) {
23+
curr = (e - s) * Math.min(height[s], height[e])
24+
max = Math.max(curr, max)
25+
if(height[s] < height[e]){
26+
s += 1
27+
} else {
28+
e -= 1
29+
}
30+
}
31+
32+
return max
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉด์ ์—์„œ ๊ฐ€์žฅ ํฐ ๋ฉด์  ๊ตฌํ•˜๊ธฐ.
3+
*
4+
* @param {number[]} height - x ์ถ• ๋ฐฉํ–ฅ์˜ ๋†’์ด ๋ฐฐ์—ด
5+
* @returns {number} - ๊ฐ€์žฅ ๋„“์€ ๋ฉด์  ๋ฐ˜ํ™˜
6+
*
7+
* ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)
8+
* - n์€ height ๋ฐฐ์—ด์˜ ๊ธธ์ด
9+
*
10+
* ๊ณต๊ฐ„ ๋ณต์žก๋„ O(1)
11+
* - ํฌ์ธํ„ฐ ๋ฐ ๊ฒฐ๊ณผ๊ฐ’ ์ €์žฅ ๊ณต๊ฐ„๋งŒ ์‚ฌ์šฉ
12+
*/
13+
function maxArea(height: number[]): number {
14+
let start = 0; // ์‹œ์ž‘ ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™”
15+
let end = height.length - 1; // ๋ ํฌ์ธํ„ฐ ์ดˆ๊ธฐํ™”
16+
let result = 0; // ์ตœ๋Œ€ ๋ฉด์  ์ €์žฅ
17+
18+
while (start < end) {
19+
// ํ˜„์žฌ ๋ฉด์  ๊ณ„์‚ฐ
20+
const currentArea = Math.min(height[start], height[end]) * (end - start);
21+
// ์ตœ๋Œ€ ๋ฉด์  ์—…๋ฐ์ดํŠธ
22+
result = Math.max(result, currentArea);
23+
24+
// ํฌ์ธํ„ฐ ์ด๋™ (๋†’์ด๊ฐ€ ๋‚ฎ์€ ์ชฝ์„ ์ด๋™)
25+
if (height[start] > height[end]) {
26+
end--;
27+
} else {
28+
start++;
29+
}
30+
}
31+
32+
return result;
33+
}
34+
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+
right = len(height) - 1
4+
left = 0
5+
max_size = 0
6+
for line in range(len(height)):
7+
if left >= right:
8+
break
9+
cur_size = (right - left) * min(height[left], height[right])
10+
if height[left] < height[right]: # ์™ผ์ชฝ์ด ์ž‘์œผ๋ฉด left ์ด๋™
11+
left += 1
12+
else:
13+
right -= 1
14+
max_size = max(max_size, cur_size)
15+
return max_size

0 commit comments

Comments
ย (0)