Skip to content

Commit 16eb955

Browse files
week6 mission done
1 parent bff493a commit 16eb955

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
- 문제: https://leetcode.com/problems/container-with-most-water/
2+
- time complexity : O(n)
3+
- space complexity : O(1)
4+
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/02/leetcode-11
5+
6+
```java
7+
class Solution {
8+
public int maxArea(int[] height) {
9+
int start = 0;
10+
int end = height.length - 1;
11+
12+
int max = getArea(height, start, end);
13+
14+
while(start < end) {
15+
if(height[start] >= height[end]) {
16+
end--;
17+
max = Math.max(max, getArea(height, start, end));
18+
} else {
19+
start++;
20+
max = Math.max(max, getArea(height, start, end));
21+
}
22+
}
23+
24+
return max;
25+
}
26+
27+
public int getArea(int[] height, int start, int end) {
28+
if (start == end) {
29+
return 0;
30+
}
31+
return getMinHeight(height[end], height[start]) * (end - start);
32+
}
33+
34+
public int getMinHeight(int height1, int height2) {
35+
return Math.min(height1, height2);
36+
}
37+
}
38+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- 문제: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
2+
- time complexity : O(log n)
3+
- space complexity : O(1)
4+
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/03/leetcode-153
5+
6+
```java
7+
public int findMin(int[] nums) {
8+
int left = 0, right = nums.length - 1;
9+
while(left < right) {
10+
int mid = left + (right - left) / 2;
11+
12+
if(nums[mid] < nums[right]) {
13+
right = mid;
14+
} else {
15+
left = mid + 1;
16+
}
17+
}
18+
19+
return nums[left];
20+
}
21+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
- 문제: https://leetcode.com/problems/longest-repeating-character-replacement/
2+
- time complexity : O(n)
3+
- space complexity : O(1)
4+
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/04/29/leetcode-424
5+
6+
```java
7+
class Solution {
8+
public int characterReplacement(String s, int k) {
9+
int[] counter = new int[26];
10+
int countOfMostFrequent = -1;
11+
12+
int headPointer = 0;
13+
int tailPointer = 0;
14+
15+
int maxLength = 0;
16+
17+
while (headPointer < s.length()) {
18+
char head = s.charAt(headPointer);
19+
char tail = s.charAt(tailPointer);
20+
counter[head - 'A']++;
21+
headPointer++;
22+
23+
countOfMostFrequent = Math.max(counter[head - 'A'], countOfMostFrequent);
24+
while (headPointer - tailPointer > countOfMostFrequent + k) {
25+
counter[tail - 'A']--;
26+
tailPointer++;
27+
tail = s.charAt(tailPointer);
28+
}
29+
30+
maxLength = Math.max(maxLength, headPointer - tailPointer);
31+
}
32+
33+
return maxLength;
34+
}
35+
}
36+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
- 문제: https://leetcode.com/problems/longest-substring-without-repeating-characters/
2+
- time complexity : O(n)
3+
- space complexity : O(n)
4+
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/02/18/leetcode-3
5+
6+
```java
7+
class Solution {
8+
public int lengthOfLongestSubstring(String s) {
9+
if (s.isEmpty()) {
10+
return 0;
11+
}
12+
13+
Set<Character> set = new HashSet<>();
14+
int pointer = 0;
15+
int longest = 0;
16+
17+
while (pointer < s.length()) {
18+
char _char = s.charAt(pointer);
19+
while (set.contains(_char)) {
20+
set.remove(s.charAt(pointer - set.size()));
21+
}
22+
set.add(_char);
23+
longest = Math.max(longest, set.size());
24+
pointer++;
25+
}
26+
27+
return longest;
28+
}
29+
}
30+
```
31+
32+
## TC 추가 설명
33+
34+
내부 while이 있지만 O(n)으로 적을 수 있는 이유는 hashset의 경우 search 하는데 드는 비용이 O(1) 이기 때문이다.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
- 문제: https://leetcode.com/problems/search-in-rotated-sorted-array/
2+
- time complexity : O(log n)
3+
- space complexity : O(1)
4+
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/03/leetcode-33
5+
6+
```java
7+
public int search(int[] nums, int target) {
8+
int left = 0, right = nums.length - 1;
9+
while(left <= right) {
10+
int mid = left + (right - left) / 2;
11+
12+
if (nums[mid] == target) {
13+
return mid;
14+
}
15+
16+
if(nums[mid] < nums[right]) {
17+
if (target < nums[mid] || target > nums[right]) {
18+
right = mid - 1;
19+
} else {
20+
left = mid + 1;
21+
}
22+
} else {
23+
if (target < nums[left] || target > nums[mid]) {
24+
left = mid + 1;
25+
} else {
26+
right = mid - 1;
27+
}
28+
}
29+
}
30+
31+
return -1;
32+
}
33+
```

0 commit comments

Comments
 (0)