Skip to content

Commit b0c8ddf

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 7cbc714 + 89edeec commit b0c8ddf

Some content is hidden

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

47 files changed

+1694
-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: 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: 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+

container-with-most-water/aa601.py

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
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*
5+
* complexity
6+
* time: O(n)
7+
* space: O(1)
8+
*/
9+
10+
var maxArea = function(height) {
11+
let answer = 0;
12+
let l = 0, r = height.length - 1;
13+
14+
while(l < r) {
15+
const area = (r - l) * Math.min(height[l], height[r]);
16+
17+
answer = area > answer ? area : answer;
18+
19+
if(height[l] < height[r]) {
20+
l += 1;
21+
}else {
22+
r -= 1;
23+
}
24+
}
25+
26+
return answer;
27+
};
28+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 투포인터를 활용한 시간복잡도 O(N)으로 풀면된다.
2+
// 중요한건 언제 이동하냐?를 정의하는건데, 전체 물양이 줄어들면 상대적으로 작은 쪽을 이동시키면된다.
3+
class Solution {
4+
public int maxArea(int[] height) {
5+
int left = 0;
6+
int right = height.length-1;
7+
int maxWater = 0;
8+
while(left < right) {
9+
int max = Math.min(height[left], height[right]) * (right - left);
10+
maxWater = Math.max(max,maxWater);
11+
if (height[left] < height[right]) {
12+
left++;
13+
} else {
14+
right--;
15+
}
16+
}
17+
return maxWater;
18+
}
19+
}

container-with-most-water/jeldo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
# O(n), n = len(height)
3+
def maxArea(self, height: list[int]) -> int:
4+
max_amount = 0
5+
l, r = 0, len(height) - 1
6+
while l < r:
7+
amount = min(height[l], height[r]) * (r - l)
8+
max_amount = max(max_amount, amount)
9+
if height[l] < height[r]:
10+
l += 1
11+
else:
12+
r -= 1
13+
return max_amount
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/***
2+
*
3+
* @problem
4+
* - 배열에서 두 선을 선택하여 최대 물을 담을 수 있는 면적을 구하는 문제
5+
*
6+
* @constraints
7+
* - 배열의 길이는 2 이상 10^5 이하입니다.
8+
* - 각 높이는 0 이상 10^4 이하의 정수입니다.
9+
*
10+
* @example
11+
* - 입력: height = [1,8,6,2,5,4,8,3,7]
12+
* - 출력: 49
13+
* (높이 8과 7을 선택하여 넓이 = min(8, 7) * (8 - 1) = 49)
14+
*
15+
* @description
16+
* - 배열의 양 끝에서 시작하는 두 개의 포인터를 사용하여 문제를 해결합니다.
17+
* - 현재 포인터 위치에서의 넓이를 계산하고 최대값을 갱신합니다.
18+
* - 더 작은 높이를 가진 포인터를 이동하여 더 큰 넓이를 탐색합니다.
19+
*
20+
* @complexity
21+
* - 시간 복잡도: O(n)
22+
* (배열을 한 번만 순회하며 최대 넓이를 계산)
23+
* - 공간 복잡도: O(1)
24+
* (추가 메모리 사용 없이 포인터만 사용)
25+
*
26+
*/
27+
function maxArea(height: number[]): number {
28+
let left = 0,
29+
right = height.length - 1,
30+
maxArea = 0;
31+
32+
while (left < right) {
33+
// 현재 넓이 계산 및 최대값 갱신
34+
maxArea = Math.max(maxArea, Math.min(height[left], height[right]) * (right - left));
35+
36+
// 더 작은 높이의 포인터를 이동
37+
height[left] < height[right] ? left++ : right--;
38+
}
39+
40+
return maxArea;
41+
}

0 commit comments

Comments
 (0)