Skip to content

Commit c89ef41

Browse files
add post leetcode-11, leetcode-153
1 parent 3288e35 commit c89ef41

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

_posts/2024-06-03-leetcode-11.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
layout: post
3+
title: (Leetcode) 11 - https://leetcode.com/problems/container-with-most-water/
4+
categories: [스터디-알고리즘]
5+
tags:
6+
[자바, java, 리트코드, Leetcode, 알고리즘, algorithm, pointer, penalty, array]
7+
date: 2024-06-02 13:00:00 +0900
8+
toc: true
9+
---
10+
11+
기회가 되어 [달레님의 스터디](https://github.com/DaleStudy/leetcode-study)에 참여하여 시간이 될 때마다 한문제씩 풀어보고 있다.
12+
13+
[https://neetcode.io/practice](https://neetcode.io/practice)
14+
15+
---
16+
17+
[https://leetcode.com/problems/container-with-most-water/](https://leetcode.com/problems/container-with-most-water/)
18+
19+
## 내가 작성한 풀이
20+
21+
일단 문제를 봤을 때 포인터를 2개를 써야겠다는 느낌이 왔다. 그래서 일단 포인터를 양 끝에 두고 끝에서 부터 조금씩 조여봐야겠다는 생각을 하였다.
22+
조일 때 어떤 기준으로 줄여야 하나 고민을 해보았는데, 두 포인터중 현재 높이가 낮은 포인터를 조이는 방향으로 진행하면 되겠다 생각을 하였다.
23+
24+
```java
25+
class Solution {
26+
public int maxArea(int[] height) {
27+
int start = 0;
28+
int end = height.length - 1;
29+
30+
int max = getArea(height, start, end);
31+
32+
while(start < end) {
33+
if(height[start] >= height[end]) {
34+
end--;
35+
max = Math.max(max, getArea(height, start, end));
36+
} else {
37+
start++;
38+
max = Math.max(max, getArea(height, start, end));
39+
}
40+
}
41+
42+
return max;
43+
}
44+
45+
public int getArea(int[] height, int start, int end) {
46+
if (start == end) {
47+
return 0;
48+
}
49+
return getMinHeight(height[end], height[start]) * (end - start);
50+
}
51+
52+
public int getMinHeight(int height1, int height2) {
53+
return Math.min(height1, height2);
54+
}
55+
}
56+
```
57+
58+
### TC, SC
59+
60+
시간 복잡도는 O(n)이고, 공간 복잡도는 O(1)이다.

_posts/2024-06-03-leetcode-153.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
layout: post
3+
title: (Leetcode) 11 - https://leetcode.com/problems/container-with-most-water/
4+
categories: [스터디-알고리즘]
5+
tags:
6+
[자바, java, 리트코드, Leetcode, 알고리즘, algorithm, pointer, rotate, array]
7+
date: 2024-06-02 13:00:00 +0900
8+
toc: true
9+
---
10+
11+
기회가 되어 [달레님의 스터디](https://github.com/DaleStudy/leetcode-study)에 참여하여 시간이 될 때마다 한문제씩 풀어보고 있다.
12+
13+
[https://neetcode.io/practice](https://neetcode.io/practice)
14+
15+
---
16+
17+
[https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
18+
19+
## 내가 작성한 풀이
20+
21+
생각보다 쉽게 해결되었다.
22+
23+
문제에서는 다음과 같은 예시들을 제공해준다.
24+
25+
- `[3,4,5,1,2]`
26+
- `[4,5,6,7,0,1,2]`
27+
- `[11,13,15,17]`
28+
29+
여기서 rotate 가 되었다면 중간에 숫자가 작아지는 지점이 생긴다. 만약 rotate 가 되지 않았다면 맨 앞자리가 최소의 수이다.
30+
31+
이 규칙을 수식으로 옮기면 된다.
32+
33+
```java
34+
class Solution {
35+
public int findMin(int[] nums) {
36+
int p = 0;
37+
int min = nums[0];
38+
while(true) {
39+
if (p == nums.length) {
40+
break;
41+
}
42+
43+
if (nums[p] > nums[p + 1]) {
44+
min = nums[p + 1];
45+
break;
46+
}
47+
48+
p++;
49+
}
50+
51+
return min;
52+
}
53+
}
54+
```
55+
56+
### TC, SC
57+
58+
시간 복잡도는 O(n)이고, 공간 복잡도는 O(1)이다.

0 commit comments

Comments
 (0)