Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[이현수] 6주차 제출합니다. #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions week-06/container-with-most-water/hyeonsu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Intuition
투포인터로 양쪽 끝에서 부터 범위를 조정하면서 가장 큰 넓이를 구하면 되는 문제이다.

# Approach
1. 두 개의 포인터가 서로 엇갈리기 전까지 while문 반복
2. 왼쪽 포인터의 높이가 오른쪽 포인터의 높이보다 낮다면 왼쪽 포인터를 오른쪾으로 한칸, 아닌 경우는 오른쪽 포인터를 왼쪽으로 한칸 좁힌다.
3. 이 과정에서 가장 큰 넓이를 구한다.

# Complexity
- Time complexity: $O(N)$
- 두 개의 포인터를 이용해서 조사하기 노드를 한번씩 조사하기 때문에 → $O(N)$

- Space complexity: $O(1)$
- 별도의 공간을 사용하지 않기 때문에 → $O(1)$

# Code
```java []
class Solution {
public int maxArea(int[] height) {
int width = height.length - 1;
int low = 0, high = height.length - 1;
int maxArea = Math.min(height[low], height[high]) * width;
while(low < high) {
if (height[low] < height[high]) ++low;
else --high;
--width;
maxArea = Math.max(maxArea, Math.min(height[low], height[high]) * width);
}
return maxArea;
}
}
```

# Learned
개념적으로 잘 잡혀있지 않은 투 포인터를 익힐 수 있어서 좋았습니다. 2개의 포인터를 가지고 index를 관리하는 방법에 익숙해질 수 있었습니다.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 투포인터가 은근 접근법이 생각안나면 골치아프더라구요..

31 changes: 31 additions & 0 deletions week-06/find-minimum-in-rotated-sorted-array/hyeonsu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Intuition
이분탐색으로 풀면 되는 문제이다.

# Approach
1. low가 high보다 작을때 동안 while문을 반복한다.
2. 중간값이 오른쪽 끝값보다 작으면 가장 작은값은 왼쪽에 있다. 아닌경우는 반대

# Complexity
- Time complexity: $O(logN)$
- 이분탐색이기 때문에 $O(logN)$

- Space complexity: $O(1)$
- 별도의 추가공간을 사용하지 않기 때문에 $O(1)$

# Code
```java []
class Solution {
public int findMin(int[] nums) {
int l = 0, h = nums.length - 1;
while(l < h) {
int m = (h - l) / 2 + l;
if (nums[m] < nums[h]) h = m;
else l = m + 1;
}
return nums[l];
}
}
```

# Learned
이분탐색 문제와 투 포인터 문제의 매커니즘이 비슷한 것 같다. 정렬된 문제를 봤을 때 잘 떠올릴 수 있을 것 같다.
33 changes: 33 additions & 0 deletions week-06/longest-repeating-character-replacement/hyeonsu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Intuition
슬라이딩 윈도우 기법을 이용해서 해결할 수 있는 문제이다.

# Approach
1. 윈도으의 끝값이 s보다 작을때 동안 while문을 반복한다.
2. 빈도수를 계산하고, 슬라이딩 윈도우기 때문에 가장 긴 문자가 몇개인지 업데이트한다.

# Complexity
- Time complexity: $O(N)$
- 슬라이딩 윈도우로 전수조사 하기 때문에 $O(N)$

- Space complexity: $O(1)$
- 빈도수배열의 크기 26이기 때문에 → $O(1)$

# Code
```java []
class Solution {
public int characterReplacement(String s, int k) {
int l = 0, h = 0, ret = 0, maxFreq = 0;
int[] freq = new int[26];
while(h < s.length()) {
++freq[s.charAt(h) - 'A'];
maxFreq = Math.max(maxFreq, freq[s.charAt(h) - 'A']);
if (h - l + 1 - maxFreq > k) --freq[s.charAt(l++) - 'A'];
ret = Math.max(ret, h++ - l + 1);
}
return ret;
}
}
```

# Learned
슬라이딩 윈도우의 기본적인 문제만 풀어봤었는데 이렇게도 응용할 수 있다는걸 새롭게 배웠습니다.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

35 changes: 35 additions & 0 deletions week-06/longest-substring-without-repeating-characters/hyeonsu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Intuition
투 포인터로 해결할 수 있는 문제이다.

# Approach
1. set을 이용해서 중복된 문자를 체크한다.
2. 만약 이미 존재하는 문자인 경우 low에 있는 문자를 지운 뒤 포인터를 옮겨준다.
3. 존재하지 않는다면 set에 넣고 오른쪽 포인터를 하나 옮기고 max를 업데이트한다.

# Complexity
- Time complexity: $O(N)$
- 한번만 순회하기 때문에 $O(N)$

- Space complexity: $O(1)$
- 별도의 추가공간을 사용하지 않기 때문에 $O(1)$

# Code
```java []
class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>(s.length());
int l = 0, h = 0, ret = 0;
while(h < s.length()) {
if (set.contains(s.charAt(h))) set.remove(s.charAt(l++));
else {
set.add(s.charAt(h++));
ret = Math.max(ret, h - l);
}
}
return ret;
}
}
```

# Learned
투포인터 트레이닝 하는중.. 해도해도 아이디어는 생각나는데 구현능력이 부족한듯. 투포인터 문제를 많이 풀어보면 해결될 문제라고 생각
41 changes: 41 additions & 0 deletions week-06/search-in-rotated-sorted-array/hyeonsu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Intuition
이분탐색을 이용해서 풀 수 있다.

# Approach
1. l <= h일 동안 while문 반복
2. nums[m] == target이면 m 반환
3. 분할의 한쪽이 정렬되어있는지 확인하는 방법은 해당 분할의 가장 왼쪽, 가장 오른쪽 값을 비교해보는 것.
4. 정렬되어 있는 분할이라면 그 분할에 target이 들어가는지 확인하고 들어간다면 h를 m - 1로 업데이트, 아니라면 l을 m + 1로 업데이트
5. while문에서 같은 값을 찾지 못했다면 -1반환

# Complexity
- Time complexity: $O(logN)$
- 이분탐색이기 때문에 → $O(logN)$

- Space complexity: $O(1)$
- 입력에 따른 별도의 추가공간을 사용하지 않기 때문에 → $O(1)$

# Code
```java []
class Solution {
public int search(int[] nums, int target) {
if (nums.length == 1 && nums[0] == target) return 0;
int l = 0, h = nums.length - 1;
while(l <= h) {
int m = (h - l) / 2 + l;
if (nums[m] == target) return m;
if (nums[l] <= nums[m]) {
if (nums[l] <= target && nums[m] > target) h = m - 1;
else l = m + 1;
} else {
if (nums[m] < target && nums[h] >= target) l = m + 1;
else h = m - 1;
}
}
return -1;
}
}
```

# Learned
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋ 좋네요~

이번 문제는 지금까지 이분탐색을 트레이닝 한 덕분인지 뭔가 잘 풀렸던 것 같다.(연습의 성과!?)