-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DaleStudy:main' into main
- Loading branch information
Showing
47 changed files
with
1,694 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
minimum = prices[0] | ||
answer = 0 | ||
for i in range(1, len(prices)): | ||
if minimum > prices[i]: | ||
minimum = prices[i] | ||
else: | ||
diff = prices[i] - minimum | ||
if answer < diff: | ||
answer = diff | ||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* ์ฃผ์ด์ง ๋์ด์์ ๋ง๋ค ์ ์๋ ๊ฐ์ฅ ํฐ ๋ฉด์ ์ ๊ตฌํ๋ ๋ฌธ์ . | ||
* Brute force๋ก ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ฐ์ ธ๊ฐ๋ฉฐ ์ต๋ ๋ฉด์ ์ ๊ตฌํ๋ ๋ฐฉ๋ฒ. | ||
* ์ฃผ์ด์ง ๋์ด(n)์ ๊ฐ์๋ 2 ๋ณด๋ค ํฌ๊ฑฐ๊ฐ ๊ฐ๊ณ 10^4 ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์. | ||
* ์ด์ค Loop์ผ๋ก ํด๊ฒฐํ ๊ฒฝ์ฐ ์๊ฐ ์ด๊ณผ ๋ฐ์ (10^8 ์ดํ๋ก ํด๊ฒฐํด์ผ ์ ํ ์๊ฐ ์์ผ๋ก ๋ฌธ์ ํด๊ฒฐ ๊ฐ๋ฅ) | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(n^2) | ||
* -> ๋ ๊ฐ์ ์๋ก ๋ค๋ฅธ ๋์ด๋ฅผ ๊ตฌํ๊ธฐ ์ํด ์ด์ค ๋ฐ๋ณต๋ฌธ ์คํ: O(n^2) | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* -> ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์. | ||
* */ | ||
fun maxArea01(height: IntArray): Int { | ||
var maxValue = 0 | ||
for (i in 0 until height.size) { | ||
for (j in i + 1 until height.size) { | ||
// ๋๋น๋ ๋ ์ ๋ถ ์ฌ์ด์ ๊ฑฐ๋ฆฌ | ||
val width = j - i | ||
// ๋์ด๋ ๋ ์ ๋ถ ์ค ์์ ๊ฐ | ||
val containerHeight = Math.min(height[i], height[j]) | ||
// ๋ฉด์ ๊ณ์ฐ | ||
val area = width * containerHeight | ||
// ์ต๋๊ฐ ๊ฐฑ์ | ||
maxValue = Math.max(maxValue, area) | ||
} | ||
} | ||
return maxValue | ||
} | ||
|
||
/* | ||
* ์ด์ค ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ ๋ฌธ์ ํ์ด. | ||
* ๊ฒฐ๊ณผ์ ์ํฅ์ ์ฃผ๋ ์กฐ๊ฑด๊ณผ ์์ | ||
* -> ๋์ด์ ์์น๋ฅผ ๋ํ๋ด๋ ์ผ์ชฝ๊ฐ๊ณผ ์ค๋ฅธ์ชฝ ๊ฐ์์ ๋ ๊ฐ ์ค ์์ ๊ฐ์ด ๋์ด๊ฐ ๋ ์ ์์. | ||
* -> ์ค๋ฅธ์ชฝ์ ๊ฐ์ ์ผ์ชฝ ๊ฐ๋ณด๋ค ์์ ์ ์์. | ||
* -> ๋๋น ๊ฐ์ ์ค๋ฅธ์ชฝ ์ธ๋ฑ์ค์์ ์ผ์ชฝ ์ธ๋ฑ์ค๋ฅผ ๋บ ๊ฐ์. | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(n) | ||
* -> ์ฃผ์ด์ง ๋์ด ๋ฐฐ์ด์์ ์์ชฝ ๋ ๊ฐ์ ์ฆ๊ฐ/๊ฐ๊ฐ ํด๊ฐ๋ฉฐ ๋ฐ๋ณต ์งํ: O(n) | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* -> ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์. | ||
* */ | ||
fun maxArea02(height: IntArray): Int { | ||
var maxValue = 0 | ||
var left = 0 | ||
var right = height.size - 1 | ||
while (left <= right) { | ||
val width = right - left | ||
val containerHeight = Math.min(height[left], height[right]) | ||
val area = width * containerHeight | ||
maxValue = Math.max(maxValue, area) | ||
if (height[left] < height[right]) { | ||
left++ | ||
} else { | ||
right-- | ||
} | ||
} | ||
return maxValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class Solution { | ||
public int maxArea(int[] height) { | ||
/** | ||
1. understanding | ||
- with two pair of line, each can contain "min(line1,line2) * (distance)" mount of water | ||
- find maximum amount of water can contain | ||
2. strategy | ||
- brute force | ||
- for each pair of lines, calculate amount and update maximum amount. | ||
- it can takes O(N), where N is the count of lines. | ||
- N is 10^5 at most, so it can takes 10^10, which can takes about 10 seconds | ||
- you need to swap out unnecessary calculation | ||
- so, let's find a unnecessary calculation in this problem. | ||
3. complexity | ||
- time: O(N) | ||
- space: O(1) | ||
*/ | ||
int l = 0; | ||
int r = height.length - 1; | ||
int maxAmount = amountOf(height, l, r); // Math.min(height[l], height[r], r-l); | ||
while (l < r) { // O(N) | ||
maxAmount = Math.max(maxAmount, amountOf(height, l, r)); | ||
if (height[l] < height[r]) { | ||
l++; | ||
} else if (height[l] > height[r]) { | ||
r--; | ||
} else { | ||
int nextLeftAmount = amountOf(height, l+1, r); | ||
int nextRightAmount = amountOf(height, l, r-1); | ||
if (nextLeftAmount < nextRightAmount) { | ||
r--; | ||
} else { | ||
l++; | ||
} | ||
} | ||
} | ||
|
||
return maxAmount; | ||
} | ||
|
||
private int amountOf(int[] height, int left, int right) { | ||
return (Math.min(height[left], height[right]) * (right - left)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# ์๊ฐ ๋ณต์ก๋ : O(n) | ||
# ๊ณต๊ฐ ๋ณต์ก๋ : O(1) | ||
# ๋ฌธ์ ์ ํ : Two Pointer | ||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
left, right = 0, len(height) - 1 | ||
max_area = 0 | ||
|
||
while left < right: | ||
max_area = max(max_area, (right - left) * min(height[left], height[right])) | ||
if height[left] < height[right]: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return max_area | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
๏ปฟ๏ปฟ #ํด์ | ||
#s๋ start index, e๋ ending index๋ก ํ ๋นํ๋ค. | ||
#area ์ (e-s)*min(height[e], height[s]) ๋ก ๋ฉด์ ์ ๋์ด๋ฅผ ๊ตฌํ๋ค. | ||
#๋ง์ฝ height[s]๊ฐ height[e] ๋ณด๋ค ์๋ค๋ฉด, ํ area๋ณด๋ค ๋ ํฐ ๊ฒฐ๊ด๊ฐ์ ์ํด ๋ณํ๋ฅผ ์ค๋ค. | ||
# (e-s)์์ e๋ฅผ ์ค์ด๋ค๋ฉด ํ์ฐ์ ์ผ๋ก area ๊ฐ์ด ๊ธฐ์กด๋ณด๋ค ์ ์ด์ง๋ค. ๋ฐ๋ผ์ s์ 1์ ๋ํด ์ธ๋ฑ์ค๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋์์ผ height[s] ์ ๋ณํ๋ฅผ ์ค๋ค. | ||
#๊ทธ ์ธ์ ์ํฉ์๋ height[e]๋ฅผ ๋ณํ์ํค๊ธฐ ์ํด e์ 1๋ฅผ ๋นผ ์ผ์ชฝ ์ธ๋ฑ์ค๋ก ์ด๋์ํจ๋ค. | ||
#ํด๋น ๋ฃจํ๋ s๊ฐ e๋ณด๋ค ์์๋ ์์ฉ๋๋ค. ๋ง์ฝ s์ ์ฆ๊ฐ์ e์ ๊ฐ์๋ก ๋ ๋ณ์๊ฐ ๋ง์ฃผ์น๋ฉด ์ข ๋ฃํ ํ max_area๋ฅผ return์ํจ๋ค. | ||
|
||
|
||
|
||
#Big O | ||
#- N: height์ element ๊ฐฏ์ | ||
|
||
#Time Complexity: O(N) | ||
#- while : s์ e๊ฐ ๋ง๋ ๋๊น์ง ์ต๋ N๋ฒ ๋ฐ๋ณต๋๋ค. ๊ฐ ๋ฐ๋ณต์์์ ์ฐ์ฐ๋ค์ O(1)์ ํด๋น๋๋ค. -> O(N) | ||
|
||
|
||
#Space Complexity: O(1) | ||
#- s,e,max_area: ๋ณ์๋ ์์๋ก ์์ฉ๋๋ค -> O(1) | ||
#### | ||
# | ||
# | ||
class Solution(object): | ||
def maxArea(self, height): | ||
""" | ||
:type height: List[int] | ||
:rtype: int | ||
""" | ||
max_area = 0 #Saving for answer | ||
s,e=0,len(height)-1 #Assign the first index and last index | ||
while s<e: | ||
area = (e-s) * min(height[s],height[e]) #Current area using e,s | ||
max_area = max(area, max_area) #Re-assing the max_area comparing with area | ||
if height[s]< height[e]: | ||
s+=1 | ||
else: | ||
e -=1 | ||
return max_area | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
right = len(height) - 1 | ||
left = 0 | ||
max_size = 0 | ||
for line in range(len(height)): | ||
if left >= right: | ||
break | ||
cur_size = (right - left) * min(height[left], height[right]) | ||
if height[left] < height[right]: # ์ผ์ชฝ์ด ์์ผ๋ฉด left ์ด๋ | ||
left += 1 | ||
else: | ||
right -= 1 | ||
max_size = max(max_size, cur_size) | ||
return max_size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @param {number[]} height | ||
* @return {number} | ||
* | ||
* complexity | ||
* time: O(n) | ||
* space: O(1) | ||
*/ | ||
|
||
var maxArea = function(height) { | ||
let answer = 0; | ||
let l = 0, r = height.length - 1; | ||
|
||
while(l < r) { | ||
const area = (r - l) * Math.min(height[l], height[r]); | ||
|
||
answer = area > answer ? area : answer; | ||
|
||
if(height[l] < height[r]) { | ||
l += 1; | ||
}else { | ||
r -= 1; | ||
} | ||
} | ||
|
||
return answer; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// ํฌํฌ์ธํฐ๋ฅผ ํ์ฉํ ์๊ฐ๋ณต์ก๋ O(N)์ผ๋ก ํ๋ฉด๋๋ค. | ||
// ์ค์ํ๊ฑด ์ธ์ ์ด๋ํ๋?๋ฅผ ์ ์ํ๋๊ฑด๋ฐ, ์ ์ฒด ๋ฌผ์์ด ์ค์ด๋ค๋ฉด ์๋์ ์ผ๋ก ์์ ์ชฝ์ ์ด๋์ํค๋ฉด๋๋ค. | ||
class Solution { | ||
public int maxArea(int[] height) { | ||
int left = 0; | ||
int right = height.length-1; | ||
int maxWater = 0; | ||
while(left < right) { | ||
int max = Math.min(height[left], height[right]) * (right - left); | ||
maxWater = Math.max(max,maxWater); | ||
if (height[left] < height[right]) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
return maxWater; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
# O(n), n = len(height) | ||
def maxArea(self, height: list[int]) -> int: | ||
max_amount = 0 | ||
l, r = 0, len(height) - 1 | ||
while l < r: | ||
amount = min(height[l], height[r]) * (r - l) | ||
max_amount = max(max_amount, amount) | ||
if height[l] < height[r]: | ||
l += 1 | ||
else: | ||
r -= 1 | ||
return max_amount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*** | ||
* | ||
* @problem | ||
* - ๋ฐฐ์ด์์ ๋ ์ ์ ์ ํํ์ฌ ์ต๋ ๋ฌผ์ ๋ด์ ์ ์๋ ๋ฉด์ ์ ๊ตฌํ๋ ๋ฌธ์ | ||
* | ||
* @constraints | ||
* - ๋ฐฐ์ด์ ๊ธธ์ด๋ 2 ์ด์ 10^5 ์ดํ์ ๋๋ค. | ||
* - ๊ฐ ๋์ด๋ 0 ์ด์ 10^4 ์ดํ์ ์ ์์ ๋๋ค. | ||
* | ||
* @example | ||
* - ์ ๋ ฅ: height = [1,8,6,2,5,4,8,3,7] | ||
* - ์ถ๋ ฅ: 49 | ||
* (๋์ด 8๊ณผ 7์ ์ ํํ์ฌ ๋์ด = min(8, 7) * (8 - 1) = 49) | ||
* | ||
* @description | ||
* - ๋ฐฐ์ด์ ์ ๋์์ ์์ํ๋ ๋ ๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค. | ||
* - ํ์ฌ ํฌ์ธํฐ ์์น์์์ ๋์ด๋ฅผ ๊ณ์ฐํ๊ณ ์ต๋๊ฐ์ ๊ฐฑ์ ํฉ๋๋ค. | ||
* - ๋ ์์ ๋์ด๋ฅผ ๊ฐ์ง ํฌ์ธํฐ๋ฅผ ์ด๋ํ์ฌ ๋ ํฐ ๋์ด๋ฅผ ํ์ํฉ๋๋ค. | ||
* | ||
* @complexity | ||
* - ์๊ฐ ๋ณต์ก๋: O(n) | ||
* (๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํ๋ฉฐ ์ต๋ ๋์ด๋ฅผ ๊ณ์ฐ) | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* (์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์ด ํฌ์ธํฐ๋ง ์ฌ์ฉ) | ||
* | ||
*/ | ||
function maxArea(height: number[]): number { | ||
let left = 0, | ||
right = height.length - 1, | ||
maxArea = 0; | ||
|
||
while (left < right) { | ||
// ํ์ฌ ๋์ด ๊ณ์ฐ ๋ฐ ์ต๋๊ฐ ๊ฐฑ์ | ||
maxArea = Math.max(maxArea, Math.min(height[left], height[right]) * (right - left)); | ||
|
||
// ๋ ์์ ๋์ด์ ํฌ์ธํฐ๋ฅผ ์ด๋ | ||
height[left] < height[right] ? left++ : right--; | ||
} | ||
|
||
return maxArea; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
|
||
package main | ||
|
||
import "testing" | ||
|
||
func TestMaxArea(t *testing.T) { | ||
result1 := maxArea([]int{1, 8, 6, 2, 5, 4, 8, 3, 7}) | ||
|
||
if result1 != 49 { | ||
t.Errorf("Expected 49, but got %v", result1) | ||
} | ||
|
||
result2 := maxArea([]int{1, 1}) | ||
|
||
if result2 != 1 { | ||
t.Errorf("Expected 1, but got %v", result2) | ||
} | ||
} | ||
|
||
func maxArea(height []int) int { | ||
result := 0 | ||
|
||
pointer_left := 0 | ||
pointer_right := len(height) - 1 | ||
|
||
for pointer_left < pointer_right { | ||
area := (pointer_right - pointer_left) * min(height[pointer_left], height[pointer_right]) | ||
result = max(result, area) | ||
|
||
if height[pointer_left] < height[pointer_right] { | ||
pointer_left++ | ||
} else { | ||
pointer_right-- | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
''' | ||
์๊ฐ ๋ณต์ก๋: O(n) | ||
- ๋ ํฌ์ธํฐ๋ฅผ ์ด๋ํ๋ฉด์ ๋ฐฐ์ด์ ํ ๋ฒ๋ง ์ํํ๋ฏ๋ก ์๊ฐ ๋ณต์ก๋๋ O(n)์ ๋๋ค. | ||
๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
- ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ณ์๋ง ์ฌ์ฉํ๋ฏ๋ก O(1)์ ๋๋ค. | ||
''' | ||
|
||
from typing import List | ||
|
||
|
||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
max_area = 0 | ||
left, right = 0, len(height) - 1 | ||
|
||
while left < right: | ||
current_area = (right - left) * min(height[left], height[right]) | ||
max_area = max(current_area, max_area) | ||
|
||
if height[left] < height[right]: | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return max_area |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
ํ์ด : | ||
left, right ์ปค์๋ฅผ ์ ๋์ ๋๊ณ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ฐ๋ํธ์ผ๋ก ์ด๋์ํค๋ฉฐ ๋ฌผ ์ ๋น๊ต | ||
๋ ์ค ๋ ๋์ ๋์ด๋ฅผ ์ด๋์ํค๋ฉด ๋ฌด์กฐ๊ฑด ๋ฌผ์ ์์ด ์ค์ด๋ค๊ธฐ ๋๋ฌธ์ ๋ ๋ฎ๊ฑฐ๋ ๊ฐ์ ์ปค์๋ฅผ ์ด๋์ํค๋ฉฐ ์ ๋ฐ์ดํธ | ||
๋์ด ๋ง๋๋ฉด ๋น๊ต ์ข ๋ฃ | ||
len(height) : N | ||
TC : O(N) | ||
l, r์ ์ด๋์ด ์ ์ฒด height ๊ฐ์๋งํผ ์ผ์ด๋๋ฏ๋ก | ||
SC : O(1) | ||
""" | ||
|
||
class Solution: | ||
def maxArea(self, height: List[int]) -> int: | ||
l = 0 | ||
r = len(height) - 1 | ||
max_area = 0 | ||
while (l != r) : | ||
cur_area = (r - l) * min(height[l], height[r]) | ||
max_area = max(cur_area, max_area) | ||
if (height[l] >= height[r]) : | ||
r -= 1 | ||
else : | ||
l += 1 | ||
return max_area |
Oops, something went wrong.