Skip to content

Commit 8f0f41b

Browse files
committed
[Function add]
1. Add leetcode solutions with tag amazon.
1 parent 75491c4 commit 8f0f41b

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

leetcode/239. Sliding Window Maximum.md

+23
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,26 @@ class Solution {
115115
}
116116
}
117117
```
118+
119+
### Amazon Session
120+
* Method 1: Deque
121+
```Java
122+
class Solution {
123+
public int[] maxSlidingWindow(int[] nums, int k) {
124+
if(nums.length < k || nums.length == 0) return new int[0];
125+
int[] result = new int[nums.length - k + 1];
126+
Deque<Integer> deque = new ArrayDeque<>();
127+
int i = 0, index = 0;
128+
for(i = 0; i < nums.length; i++){
129+
if(i - k >= 0 && !deque.isEmpty() && deque.peek() == i - k) deque.pollFirst();
130+
int add = nums[i];
131+
while(!deque.isEmpty() && add >= nums[deque.getLast()]){
132+
deque.pollLast();
133+
}
134+
deque.addLast(i);
135+
if(i - k + 1 >= 0) result[index++] = nums[deque.getFirst()];
136+
}
137+
return result;
138+
}
139+
}
140+
```

leetcode/413. Arithmetic Slices.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
### 413. Arithmetic Slices
2+
3+
### Question
4+
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
5+
6+
For example, these are arithmetic sequence:
7+
8+
1, 3, 5, 7, 9
9+
7, 7, 7, 7
10+
3, -1, -5, -9
11+
12+
The following sequence is not arithmetic.
13+
14+
1, 1, 2, 5, 7
15+
16+
17+
A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.
18+
19+
A slice (P, Q) of array A is called arithmetic if the sequence:
20+
A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.
21+
22+
The function should return the number of arithmetic slices in the array A.
23+
24+
```
25+
Example:
26+
27+
A = [1, 2, 3, 4]
28+
29+
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
30+
```
31+
32+
### Solution
33+
* Method 1: Find number at end of each fast.
34+
```Java
35+
class Solution {
36+
public int numberOfArithmeticSlices(int[] A) {
37+
if(A == null || A.length < 3) return 0;
38+
int res = 0;
39+
int slow = 0, fast = 1;
40+
int diff = A[fast] - A[slow];
41+
fast++; // fast = 2 now.
42+
for(; fast < A.length; fast++){
43+
if(A[fast] - A[fast - 1] == diff){
44+
if(fast - slow + 1 >= 3){
45+
res += fast - 2 - slow + 1;
46+
}
47+
continue;
48+
}else{ // diff changes
49+
slow = fast - 1;
50+
diff = A[fast] - A[fast - 1];
51+
}
52+
}
53+
return res;
54+
}
55+
}
56+
```

leetcode/74. Search a 2D Matrix.md

+21
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,24 @@ class Solution {
8282
}
8383
}
8484
```
85+
86+
### Amazon session
87+
* Method 1: search
88+
```Java
89+
class Solution {
90+
public boolean searchMatrix(int[][] matrix, int target) {
91+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return false;
92+
int height = matrix.length, width = matrix[0].length;
93+
int x = 0, y = width - 1;
94+
while(x >= 0 && x < height && y >= 0 && y < width){
95+
if(matrix[x][y] == target) return true;
96+
else if(matrix[x][y] < target){
97+
x++;
98+
}else{
99+
y--;
100+
}
101+
}
102+
return false;
103+
}
104+
}
105+
```

0 commit comments

Comments
 (0)