Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfdb0087

Browse files
committed
[Function add]
1. Add leetcode solutions with amazon tag.
1 parent9114e28 commitfdb0087

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

‎leetcode/45. Jump Game II.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,33 @@ class Solution {
5858
return result;
5959
}
6060
}
61-
```
61+
```
62+
63+
###Amazon Session
64+
* Method 1: Greedy
65+
```Java
66+
class Solution {
67+
public int jump(int[] nums) {
68+
//[2,3,1,1,4]
69+
if(nums.length <= 1) return 0;
70+
if(nums[0] >= nums.length - 1) return 1;
71+
int res = 1, cur = 0, max = 0;
72+
while(max < nums.length - 1){
73+
int nextCur = 0;
74+
int nextMax = 0;
75+
max = cur + nums[cur];
76+
for(int i = cur + 1; i <= max && i < nums.length; i++){
77+
if(i + nums[i] > nextMax){
78+
nextCur = i;
79+
nextMax = i + nums[i];
80+
}
81+
}
82+
cur = nextCur;
83+
max = nextMax;
84+
if(max >= nums.length - 1) return res + 1;
85+
res++;
86+
}
87+
return res;
88+
}
89+
}
90+
```

‎leetcode/79. Word Search.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,45 @@ class Solution {
215215
}
216216
}
217217
```
218+
219+
###Amazon Session
220+
* Method 1: dfs
221+
```Java
222+
class Solution {
223+
private static final int[][] dir = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
224+
private char[][] board;
225+
private int height, width;
226+
private String word;
227+
public boolean exist(char[][] board, String word) {
228+
if(board == null || board.length == 0 || board[0].length == 0) return false;
229+
this.board = board;
230+
this.height = board.length;
231+
this.width = board[0].length;
232+
this.word = word;
233+
for(int i = 0; i < height; i++){
234+
for(int j = 0; j < width; j++){
235+
if(word.charAt(0) == board[i][j]){
236+
Set<Integer> visited = new HashSet<>();
237+
visited.add(i * width + j);
238+
if(dfs(i, j, visited, 0)) return true;
239+
}
240+
}
241+
}
242+
return false;
243+
}
244+
private boolean dfs(int x, int y, Set<Integer> visited, int index){
245+
if(index == word.length() - 1) return true;
246+
int tx = 0, ty = 0;
247+
for(int d = 0; d < 4; d++){
248+
tx = x + dir[d][0];
249+
ty = y + dir[d][1];
250+
if(tx >= 0 && tx < height && ty >= 0 && ty < width && !visited.contains(tx * width + ty) && board[tx][ty] == word.charAt(index + 1)){
251+
visited.add(tx * width + ty);
252+
if(dfs(tx, ty, visited, index + 1)) return true;
253+
visited.remove(tx * width + ty);
254+
}
255+
}
256+
return false;
257+
}
258+
}
259+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
##992. Subarrays with K Different Integers
2+
3+
###Question
4+
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.
5+
6+
(For example,[1,2,3,1,2] has 3 different integers: 1, 2, and 3.)
7+
8+
Return the number of good subarrays of A.
9+
10+
```
11+
Example 1:
12+
13+
Input: A = [1,2,1,2,3], K = 2
14+
Output: 7
15+
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
16+
17+
Example 2:
18+
19+
Input: A = [1,2,1,3,4], K = 3
20+
Output: 3
21+
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
22+
```
23+
24+
Note:
25+
1. 1 <= A.length <= 20000
26+
2. 1 <= A[i] <= A.length
27+
3. 1 <= K <= A.length
28+
29+
###Thinking:
30+
* Method1:
31+
1. atMost(A, K), number of subarrays with at most K elements in A.
32+
2. atMost(A, K) - atMost(A, K - 1): sub-array with exactly K elements.
33+
```Java
34+
classSolution {
35+
publicintsubarraysWithKDistinct(int[]A,intK) {
36+
return atMost(A,K)- atMost(A,K-1);
37+
}
38+
privateintatMost(int[]A,intk){
39+
Map<Integer,Integer> map=newHashMap<>();
40+
int slow=0, fast=0;
41+
int res=0;
42+
while(fast<A.length){
43+
if(!map.containsKey(A[fast])) k--;
44+
map.put(A[fast], map.getOrDefault(A[fast],0)+1);
45+
while(k<0){
46+
int cur= map.get(A[slow]);
47+
if(cur==1){
48+
map.remove(A[slow]);
49+
k++;
50+
}else map.put(A[slow], cur-1);
51+
slow++;
52+
}
53+
res+= fast- slow+1;
54+
++fast;
55+
}
56+
return res;
57+
}
58+
}
59+
```
60+
61+
###Reference
62+
1. [[Java/C++/Python]SlidingWindow withVideo](https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/234482/JavaC++Python-Sliding-Window-with-Video)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp