-
-
Notifications
You must be signed in to change notification settings - Fork 245
[박종훈] 6주차 답안 제출 #115
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
Merged
Merged
[박종훈] 6주차 답안 제출 #115
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
- 문제: https://leetcode.com/problems/container-with-most-water/ | ||
- time complexity : O(n) | ||
- space complexity : O(1) | ||
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/02/leetcode-11 | ||
|
||
```java | ||
class Solution { | ||
public int maxArea(int[] height) { | ||
int start = 0; | ||
int end = height.length - 1; | ||
|
||
int max = getArea(height, start, end); | ||
|
||
while(start < end) { | ||
if(height[start] >= height[end]) { | ||
end--; | ||
max = Math.max(max, getArea(height, start, end)); | ||
} else { | ||
start++; | ||
max = Math.max(max, getArea(height, start, end)); | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
|
||
public int getArea(int[] height, int start, int end) { | ||
if (start == end) { | ||
return 0; | ||
} | ||
return getMinHeight(height[end], height[start]) * (end - start); | ||
} | ||
|
||
public int getMinHeight(int height1, int height2) { | ||
return Math.min(height1, height2); | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
- 문제: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ | ||
- time complexity : O(log n) | ||
- space complexity : O(1) | ||
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/03/leetcode-153 | ||
|
||
```java | ||
public int findMin(int[] nums) { | ||
int left = 0, right = nums.length - 1; | ||
while(left < right) { | ||
int mid = left + (right - left) / 2; | ||
|
||
if(nums[mid] < nums[right]) { | ||
right = mid; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
|
||
return nums[left]; | ||
} | ||
``` |
36 changes: 36 additions & 0 deletions
36
longest-repeating-character-replacement/dev-jonghoonpark.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
- 문제: https://leetcode.com/problems/longest-repeating-character-replacement/ | ||
- time complexity : O(n) | ||
- space complexity : O(1) | ||
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/04/29/leetcode-424 | ||
|
||
```java | ||
class Solution { | ||
public int characterReplacement(String s, int k) { | ||
int[] counter = new int[26]; | ||
int countOfMostFrequent = -1; | ||
|
||
int headPointer = 0; | ||
int tailPointer = 0; | ||
|
||
int maxLength = 0; | ||
|
||
while (headPointer < s.length()) { | ||
char head = s.charAt(headPointer); | ||
char tail = s.charAt(tailPointer); | ||
counter[head - 'A']++; | ||
headPointer++; | ||
|
||
countOfMostFrequent = Math.max(counter[head - 'A'], countOfMostFrequent); | ||
while (headPointer - tailPointer > countOfMostFrequent + k) { | ||
counter[tail - 'A']--; | ||
tailPointer++; | ||
tail = s.charAt(tailPointer); | ||
} | ||
|
||
maxLength = Math.max(maxLength, headPointer - tailPointer); | ||
} | ||
|
||
return maxLength; | ||
} | ||
} | ||
``` |
34 changes: 34 additions & 0 deletions
34
longest-substring-without-repeating-characters/dev-jonghoonpark.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
- 문제: https://leetcode.com/problems/longest-substring-without-repeating-characters/ | ||
- time complexity : O(n) | ||
- space complexity : O(n) | ||
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/02/18/leetcode-3 | ||
|
||
```java | ||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
if (s.isEmpty()) { | ||
return 0; | ||
} | ||
|
||
Set<Character> set = new HashSet<>(); | ||
int pointer = 0; | ||
int longest = 0; | ||
|
||
while (pointer < s.length()) { | ||
char _char = s.charAt(pointer); | ||
while (set.contains(_char)) { | ||
set.remove(s.charAt(pointer - set.size())); | ||
} | ||
set.add(_char); | ||
longest = Math.max(longest, set.size()); | ||
pointer++; | ||
} | ||
Comment on lines
+17
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오, 집합을 요렇게 활용하시다니 상당히 신선한데요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중복이 되면 안된다는 조건이 있어서 Set을 사용해보았습니다 : ) |
||
|
||
return longest; | ||
} | ||
} | ||
``` | ||
|
||
## TC 추가 설명 | ||
|
||
내부 while이 있지만 O(n)으로 적을 수 있는 이유는 hashset의 경우 search 하는데 드는 비용이 O(1) 이기 때문이다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
- 문제: https://leetcode.com/problems/search-in-rotated-sorted-array/ | ||
- time complexity : O(log n) | ||
- space complexity : O(1) | ||
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/03/leetcode-33 | ||
|
||
```java | ||
public int search(int[] nums, int target) { | ||
int left = 0, right = nums.length - 1; | ||
while(left <= right) { | ||
int mid = left + (right - left) / 2; | ||
|
||
if (nums[mid] == target) { | ||
return mid; | ||
} | ||
|
||
if(nums[mid] < nums[right]) { | ||
if (target < nums[mid] || target > nums[right]) { | ||
right = mid - 1; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} else { | ||
if (target < nums[left] || target > nums[mid]) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이런 류의 체크는 큰 의미없이 코드의 길이만 늘릴 수 있지 않을까요? 얼마큼의 연산량이 아껴지는지 생각해보시면 좋을 것 같습니다. 특히 자바 같이 코드가 긴 편에 속하는 언어에서는 오히려 득보다 해가 될 수 있다고 생각합니다.