Skip to content

Commit bcb2945

Browse files
authored
Merge pull request #121 from bky373/main
[bky373] Week 6 Solutions
2 parents 7f3c92f + a43d5bc commit bcb2945

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

container-with-most-water/bky373.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* https://leetcode.com/problems/container-with-most-water/solution/
3+
*
4+
* time: O(N)
5+
* space: O(1)
6+
*/
7+
class Solution {
8+
9+
public int maxArea(int[] height) {
10+
int left = 0;
11+
int right = height.length - 1;
12+
int max = 0;
13+
while (left < right) {
14+
int area = (right - left) * Math.min(height[left], height[right]);
15+
if (area > max) {
16+
max = area;
17+
}
18+
if (height[left] < height[right]) {
19+
left++;
20+
} else {
21+
right--;
22+
}
23+
}
24+
return max;
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
3+
*
4+
* time: O(log N)
5+
* space: O(1)
6+
*/
7+
class Solution {
8+
9+
public int findMin(int[] nums) {
10+
int left = 0;
11+
int right = nums.length - 1;
12+
while (nums[left] > nums[right]) {
13+
int mid = (left + right) / 2;
14+
if (nums[mid] < nums[right]) {
15+
right = mid;
16+
} else {
17+
left = mid + 1;
18+
}
19+
}
20+
return nums[left];
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* https://leetcode.com/problems/longest-substring-without-repeating-characters/
3+
*
4+
* time: O(N)
5+
* space: O(1)
6+
*/
7+
class Solution {
8+
9+
public int characterReplacement(String s, int k) {
10+
int[] alp = new int[26];
11+
int start = 0;
12+
int maxCnt = 0;
13+
int maxLen = 0;
14+
for (int end = 0; end < s.length(); end++) {
15+
maxCnt = Math.max(maxCnt, ++alp[s.charAt(end) - 'A']);
16+
while (end - start + 1 - maxCnt > k) {
17+
alp[s.charAt(start) - 'A']--;
18+
start++;
19+
}
20+
maxLen = Math.max(maxLen, end - start + 1);
21+
}
22+
return maxLen;
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* https://leetcode.com/problems/longest-substring-without-repeating-characters/
3+
*
4+
* time: O(N)
5+
* space: O(N)
6+
*/
7+
class Solution {
8+
9+
public int lengthOfLongestSubstring(String s) {
10+
int start = 0;
11+
int k = 0;
12+
int longest = 0;
13+
int mapVersion = 0;
14+
Map<Character, Integer> charMap = new HashMap<>();
15+
while (start + k < s.length()) {
16+
char cur = s.charAt(start + k);
17+
int curVersion = charMap.getOrDefault(cur, 0);
18+
if (curVersion > mapVersion) {
19+
longest = Math.max(longest, k);
20+
start++;
21+
k = 0;
22+
mapVersion = curVersion;
23+
} else {
24+
charMap.put(cur, mapVersion + 1);
25+
k++;
26+
}
27+
}
28+
longest = Math.max(longest, k);
29+
return longest;
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* https://leetcode.com/problems/search-in-rotated-sorted-array/
3+
*
4+
* time: O(log n)
5+
* space: O(1)
6+
*/
7+
class Solution {
8+
9+
public int search(int[] nums, int target) {
10+
int res = -1;
11+
if (nums.length == 1 && nums[0] == target) {
12+
return 0;
13+
}
14+
int minIndex = findMinIndex(nums);
15+
int leftResult = binarySearch(nums, 0, minIndex - 1, target);
16+
if (leftResult > -1) {
17+
return leftResult;
18+
}
19+
return binarySearch(nums, minIndex, nums.length - 1, target);
20+
}
21+
22+
private int findMinIndex(int[] nums) {
23+
int left = 0;
24+
int right = nums.length - 1;
25+
while (nums[left] > nums[right]) {
26+
int mid = (left + right) / 2;
27+
if (nums[mid] < nums[right]) {
28+
right = mid;
29+
} else {
30+
left = mid + 1;
31+
}
32+
}
33+
return left;
34+
}
35+
36+
private int binarySearch(int[] nums, int left, int right, int target) {
37+
while (left <= right) {
38+
int mid = (left + right) / 2;
39+
if (nums[mid] == target) {
40+
return mid;
41+
} else if (nums[mid] < target) {
42+
left = mid + 1;
43+
} else {
44+
right = mid - 1;
45+
}
46+
}
47+
return -1;
48+
}
49+
}

0 commit comments

Comments
 (0)