Skip to content

Commit 9114e28

Browse files
committed
[Function add]
1. Add leetcode solutions with amazon tag.
1 parent 9a69639 commit 9114e28

3 files changed

+87
-0
lines changed

Diff for: leetcode/33. Search in Rotated Sorted Array.md

+25
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,29 @@ class Solution {
139139
}
140140
}
141141
}
142+
```
143+
144+
### Amazon session
145+
* Method 1: binary search
146+
```Java
147+
class Solution {
148+
public int search(int[] nums, int target) {
149+
if(nums == null || nums.length == 0) return -1;
150+
return binarySearch(nums, 0, nums.length - 1, target);
151+
}
152+
private int binarySearch(int[] nums, int low, int high, int target){
153+
if(low > high) return -1;
154+
int mid = low + (high - low) / 2;
155+
if(nums[mid] == target) return mid;
156+
else if(nums[mid] >= nums[low]){ // left side is in order
157+
if(target >= nums[low] && target < nums[mid]){
158+
return binarySearch(nums, low, mid - 1, target);
159+
}else return binarySearch(nums, mid + 1, high, target);
160+
}else{ // right side is in order
161+
if(target > nums[mid] && target <= nums[high])
162+
return binarySearch(nums, mid + 1, high, target);
163+
return binarySearch(nums, low, mid - 1, target);
164+
}
165+
}
166+
}
142167
```

Diff for: leetcode/340. Longest Substring with At Most K Distinct Characters.md

+31
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,35 @@ Explanation: T is "aa" which its length is 2.
4848
return max;
4949
}
5050
}
51+
```
52+
53+
### Amazon Session
54+
* Method 1: sliding window
55+
```Java
56+
class Solution {
57+
public int lengthOfLongestSubstringKDistinct(String s, int k) {
58+
int slow = 0, fast = 0;
59+
int result = 0;
60+
char[] arr = s.toCharArray();
61+
Map<Character, Integer> map = new HashMap<>();
62+
while(fast < arr.length){
63+
map.put(arr[fast], map.getOrDefault(arr[fast], 0) + 1);
64+
if(map.size() <= k){
65+
result = Math.max(result, fast - slow + 1);
66+
}else if(map.size() > k){
67+
while(map.size() > k){
68+
char c = arr[slow];
69+
int freq = map.get(c);
70+
if(freq == 1){
71+
map.remove(c);
72+
}else map.put(c, freq - 1);
73+
slow++;
74+
}
75+
result = Math.max(result, fast - slow + 1);
76+
}
77+
fast++;
78+
}
79+
return result;
80+
}
81+
}
5182
```

Diff for: leetcode/56. Merge Intervals.md

+31
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,34 @@ class Solution {
9494
}
9595
}
9696
```
97+
98+
### Amazon session
99+
* Method 1: Sort
100+
```Java
101+
class Solution {
102+
public int[][] merge(int[][] intervals) {
103+
if(intervals == null || intervals.length <= 1) return intervals;
104+
Arrays.sort(intervals, (a, b)->{
105+
return a[0] == b[0] ? a[1] - b[1]: a[0] - b[0];
106+
});
107+
int len = intervals.length;
108+
List<int[]> result = new ArrayList<>();
109+
int start = intervals[0][0];
110+
int end = intervals[0][1];
111+
for(int i = 1; i < len; i++){
112+
if(intervals[i][0] <= end){
113+
end = Math.max(intervals[i][1],end);
114+
}else{
115+
result.add(new int[]{start, end});
116+
start = intervals[i][0];
117+
end = Math.max(intervals[i][1],end);
118+
}
119+
}
120+
result.add(new int[]{start, end});
121+
return result.toArray(new int[result.size()][2]);
122+
}
123+
}
124+
```
125+
126+
### Reference
127+
1. [Algorithm | Interval Schedule](https://seanforfun.github.io/algorithm/2019/05/03/IntervalScheduleWithGreedy.html)

0 commit comments

Comments
 (0)