Skip to content
Merged
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
26 changes: 26 additions & 0 deletions container-with-most-water/bky373.java
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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

요 부분을

max = Math.max(max, area)

요렇게 고치는걷 괜찮아 보이네요!

Copy link
Copy Markdown
Contributor Author

@bky373 bky373 Jun 9, 2024

Choose a reason for hiding this comment

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

네네 그렇네요 한 줄 줄이는 것도 가독성 측면에서 좋은 것 같습니다 의견 감사합니다!

max = area;
}
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return max;
}
}
22 changes: 22 additions & 0 deletions find-minimum-in-rotated-sorted-array/bky373.java
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];
}
}
24 changes: 24 additions & 0 deletions longest-repeating-character-replacement/bky373.java
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}
31 changes: 31 additions & 0 deletions longest-substring-without-repeating-characters/bky373.java
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;
Comment thread
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;
}
}
49 changes: 49 additions & 0 deletions search-in-rotated-sorted-array/bky373.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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


find-minimum-in-rotated-sorted-array 문제를 통해 만든
min 값 찾는 로직을 사용해서 푸셨군요

search-in-rotated-sorted-array/dev-jonghoonpark.md
저는 그냥 아예 새로 작성했는데 논리가 분리되는 면에서는 작성해주신 코드도 재밌네요 ㅎㅎ

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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);
Comment thread
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;
}
}