-
-
Notifications
You must be signed in to change notification settings - Fork 339
[bky373] Week 6 Solutions #121
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * https://leetcode.com/problems/container-with-most-water/solution/ | ||
| * | ||
| * time: O(N) | ||
| * space: O(1) | ||
| */ | ||
| class Solution { | ||
|
|
||
| public int maxArea(int[] height) { | ||
| int left = 0; | ||
| int right = height.length - 1; | ||
| int max = 0; | ||
| while (left < right) { | ||
| int area = (right - left) * Math.min(height[left], height[right]); | ||
| if (area > max) { | ||
| max = area; | ||
| } | ||
| if (height[left] < height[right]) { | ||
| left++; | ||
| } else { | ||
| right--; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ | ||
| * | ||
| * time: O(log N) | ||
| * space: O(1) | ||
| */ | ||
| class Solution { | ||
|
|
||
| public int findMin(int[] nums) { | ||
| int left = 0; | ||
| int right = nums.length - 1; | ||
| while (nums[left] > nums[right]) { | ||
| int mid = (left + right) / 2; | ||
| if (nums[mid] < nums[right]) { | ||
| right = mid; | ||
| } else { | ||
| left = mid + 1; | ||
| } | ||
| } | ||
| return nums[left]; | ||
| } | ||
| } |
|
Contributor
Author
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. 이 문제는 지독히 안 풀리는 문제 중 하나네요.. ㅠㅠ 역시 세상엔 고수가 많은 것 같습니다 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * https://leetcode.com/problems/longest-substring-without-repeating-characters/ | ||
| * | ||
| * time: O(N) | ||
| * space: O(1) | ||
| */ | ||
| class Solution { | ||
|
|
||
| public int characterReplacement(String s, int k) { | ||
| int[] alp = new int[26]; | ||
| int start = 0; | ||
| int maxCnt = 0; | ||
| int maxLen = 0; | ||
| for (int end = 0; end < s.length(); end++) { | ||
| maxCnt = Math.max(maxCnt, ++alp[s.charAt(end) - 'A']); | ||
| while (end - start + 1 - maxCnt > k) { | ||
| alp[s.charAt(start) - 'A']--; | ||
| start++; | ||
| } | ||
| maxLen = Math.max(maxLen, end - start + 1); | ||
| } | ||
| return maxLen; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * https://leetcode.com/problems/longest-substring-without-repeating-characters/ | ||
| * | ||
| * time: O(N) | ||
| * space: O(N) | ||
| */ | ||
| class Solution { | ||
|
|
||
| public int lengthOfLongestSubstring(String s) { | ||
| int start = 0; | ||
| int k = 0; | ||
| int longest = 0; | ||
| int mapVersion = 0; | ||
|
Invidam marked this conversation as resolved.
|
||
| Map<Character, Integer> charMap = new HashMap<>(); | ||
| while (start + k < s.length()) { | ||
| char cur = s.charAt(start + k); | ||
| int curVersion = charMap.getOrDefault(cur, 0); | ||
| if (curVersion > mapVersion) { | ||
| longest = Math.max(longest, k); | ||
| start++; | ||
| k = 0; | ||
| mapVersion = curVersion; | ||
| } else { | ||
| charMap.put(cur, mapVersion + 1); | ||
| k++; | ||
| } | ||
| } | ||
| longest = Math.max(longest, k); | ||
| return longest; | ||
| } | ||
| } | ||
|
Contributor
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. 오 search-in-rotated-sorted-array/dev-jonghoonpark.md
Contributor
Author
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. @dev-jonghoonpark |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * https://leetcode.com/problems/search-in-rotated-sorted-array/ | ||
| * | ||
| * time: O(log n) | ||
| * space: O(1) | ||
| */ | ||
| class Solution { | ||
|
|
||
| public int search(int[] nums, int target) { | ||
| int res = -1; | ||
| if (nums.length == 1 && nums[0] == target) { | ||
| return 0; | ||
| } | ||
| int minIndex = findMinIndex(nums); | ||
| int leftResult = binarySearch(nums, 0, minIndex - 1, target); | ||
| if (leftResult > -1) { | ||
| return leftResult; | ||
| } | ||
| return binarySearch(nums, minIndex, nums.length - 1, target); | ||
|
Invidam marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private int findMinIndex(int[] nums) { | ||
| int left = 0; | ||
| int right = nums.length - 1; | ||
| while (nums[left] > nums[right]) { | ||
| int mid = (left + right) / 2; | ||
| if (nums[mid] < nums[right]) { | ||
| right = mid; | ||
| } else { | ||
| left = mid + 1; | ||
| } | ||
| } | ||
| return left; | ||
| } | ||
|
|
||
| private int binarySearch(int[] nums, int left, int right, int target) { | ||
| while (left <= right) { | ||
| int mid = (left + right) / 2; | ||
| if (nums[mid] == target) { | ||
| return mid; | ||
| } else if (nums[mid] < target) { | ||
| left = mid + 1; | ||
| } else { | ||
| right = mid - 1; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } | ||
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.
요 부분을
요렇게 고치는걷 괜찮아 보이네요!
Uh oh!
There was an error while loading. Please reload this page.
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.
네네 그렇네요 한 줄 줄이는 것도 가독성 측면에서 좋은 것 같습니다 의견 감사합니다!