Skip to content

Commit 2521eb6

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 2b712d7 + a2976d0 commit 2521eb6

Some content is hidden

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

63 files changed

+2234
-22
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
func maxArea(_ height: [Int]) -> Int {
3+
var maxArea = 0
4+
var startPointIndex = 0
5+
var endPointIndex = height.count - 1
6+
while startPointIndex < endPointIndex {
7+
let minHeight = min(height[startPointIndex], height[endPointIndex])
8+
let area = minHeight * (endPointIndex - startPointIndex)
9+
maxArea = max(maxArea, area)
10+
if height[startPointIndex] < height[endPointIndex] {
11+
startPointIndex += 1
12+
} else {
13+
endPointIndex -= 1
14+
}
15+
}
16+
return maxArea
17+
}
18+
}
19+
20+
// Time Complexity O(N)
21+
// Space Complexity O(1)
22+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Time Complexity : O(n)
3+
Space Complexity : O(1)
4+
*/
5+
class Solution {
6+
public int maxArea(int[] height) {
7+
int left = 0;
8+
int right = height.length - 1;
9+
int area = 0;
10+
11+
while (left < right) {
12+
int currentArea = (right - left) * Math.min(height[left], height[right]);
13+
area = Math.max(area, currentArea);
14+
if (height[left] < height[right]) {
15+
left++;
16+
} else {
17+
right--;
18+
}
19+
}
20+
return area;
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Solution:
3+
"""
4+
1. 브루트포스와 같이 전부 길이를 대조해보고 하면 시간복잡도가 터질 것.
5+
투포인터 방식을 활용하면 됨. 투포인터에 대한 문제가 코딩테스트로 많이 나올 것 같음.
6+
확실하게 공부 필요.
7+
"""
8+
def maxArea(self, height: List[int]) -> int:
9+
max_area = 0
10+
start, end = 0, len(height) - 1
11+
while start < end:
12+
area = (end - start) * min(height[start], height[end])
13+
max_area = max(area, max_area)
14+
15+
if height[start] <= height[end]:
16+
start += 1
17+
else:
18+
end -= 1
19+
20+
return max_area
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function maxArea(height: number[]): number {
2+
const n = height.length;
3+
4+
let maximumArea = 0;
5+
let l = 0;
6+
let r = n - 1;
7+
8+
while (l < r) {
9+
const currentArea = (r - l) * Math.min(height[l], height[r]);
10+
11+
maximumArea = Math.max(maximumArea, currentArea);
12+
13+
if (height[l] > height[r]) {
14+
r--;
15+
} else {
16+
l++;
17+
}
18+
}
19+
20+
return maximumArea;
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(1)
4+
*/
5+
6+
class Solution {
7+
fun maxArea(height: IntArray): Int {
8+
var i = 0
9+
var j = height.size - 1
10+
var max = 0
11+
12+
while (i < j) {
13+
val h = minOf(height[i], height[j])
14+
max = maxOf(max, (j - i) * h)
15+
16+
if (height[i] <= height[j]) i++
17+
else j--
18+
}
19+
20+
return max
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 구하려는 것은 최대 영역값이기 때문에 x,y축을 계산한 현재 영역과 기존 영역을 비교해 max값을 반환하면 됩니다.
3+
* 포인터를 어떻게 좁힐까 생각할 수 있는데 쉽게 말해서 start와 end 중 더 작은 쪽을 앞 또는 뒤로 이동하면 됩니다.
4+
* 영역은 두 막대가 모두 충족가능한 길이가 되어야 하므로 Math.min()으로 설정합니다.
5+
*/
6+
class Solution {
7+
public int maxArea(int[] height) {
8+
int start = 0;
9+
int end = height.length - 1;
10+
int area = 0;
11+
12+
while (start < end) {
13+
int y = Math.min(height[start], height[end]); // y축은 더 작은 값으로 설정
14+
int x = Math.abs(start - end); // end - start도 가능
15+
int calculatedArea = x * y;
16+
area = Math.max(area, calculatedArea);
17+
18+
// [중요] 포인터 이동 로직
19+
if (height[start] <= height[end])
20+
start++;
21+
else
22+
end--;
23+
}
24+
return area;
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function (height) {
6+
// 더 낮은 쪽을 안쪽으로 이동하는 방식으로
7+
8+
let start = 0;
9+
let end = height.length - 1;
10+
let res = -1;
11+
while (start <= end) {
12+
const v = Math.min(height[start], height[end]) * (end - start);
13+
if (v > res) {
14+
res = v;
15+
}
16+
17+
if (height[start] > height[end]) {
18+
end -= 1;
19+
} else {
20+
start += 1;
21+
}
22+
}
23+
return res;
24+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time Complexity: O(n), n: height.length
2+
// Space Complexity: O(1)
3+
class Solution {
4+
public int maxArea(int[] height) {
5+
int maxWaterAmount = 0;
6+
int leftLineIdx = 0;
7+
int rightLineIdx = height.length-1;
8+
9+
while (leftLineIdx < rightLineIdx) {
10+
int leftHeight = height[leftLineIdx];
11+
int rightHeight = height[rightLineIdx];
12+
int tempAmount = 0;
13+
14+
if (leftHeight < rightHeight) {
15+
tempAmount = leftHeight * (rightLineIdx - leftLineIdx);
16+
leftLineIdx++;
17+
} else {
18+
tempAmount = rightHeight * (rightLineIdx - leftLineIdx);
19+
rightLineIdx--;
20+
}
21+
22+
// update maximum amount
23+
maxWaterAmount = tempAmount > maxWaterAmount ? tempAmount : maxWaterAmount;
24+
}
25+
26+
return maxWaterAmount;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
int left=0;
4+
int right=height.length-1;
5+
int max=0;
6+
7+
while(left<right) {
8+
int water = (right-left) * Math.min(height[left], height[right]);
9+
max = Math.max(max, water);
10+
11+
if(height[left]<height[right]) {
12+
left++;
13+
} else {
14+
right--;
15+
}
16+
}
17+
return max;
18+
}
19+
}
20+
21+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func maxArea(_ height: [Int]) -> Int {
3+
var heights = height
4+
var start = 0
5+
var end = heights.count - 1
6+
var maxAmount = 0
7+
8+
while start < end {
9+
let startHeight = heights[start]
10+
let endHeight = heights[end]
11+
let amount = min(startHeight, endHeight) * (end - start)
12+
maxAmount = max(amount, maxAmount)
13+
14+
if startHeight < endHeight {
15+
start += 1
16+
} else {
17+
end -= 1
18+
}
19+
}
20+
21+
return maxAmount
22+
23+
//시간 O(n)
24+
//공간 O(1)
25+
}
26+
}
27+

0 commit comments

Comments
 (0)