Skip to content

Commit 1312ecf

Browse files
committed
[Function add]
1. Add leetcode solutions with amazon tag.
1 parent 25cb3d6 commit 1312ecf

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed

leetcode/165. Compare Version Numbers.md

+28
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,32 @@ class Solution {
120120
return 0;
121121
}
122122
}
123+
```
124+
125+
### Amazon Session
126+
* Method 1: String
127+
```Java
128+
class Solution {
129+
public int compareVersion(String version1, String version2) {
130+
String[] tokens1 = version1.split("\\.");
131+
String[] tokens2 = version2.split("\\.");
132+
int index = 0;
133+
while(index < tokens1.length || index < tokens2.length){
134+
if(index < tokens1.length && index < tokens2.length){
135+
int first = Integer.parseInt(tokens1[index]);
136+
int second = Integer.parseInt(tokens2[index]);
137+
if(first < second) return -1;
138+
else if(first > second) return 1;
139+
}else if(index < tokens1.length){
140+
int first = Integer.parseInt(tokens1[index]);
141+
if(first != 0) return 1;
142+
}else if(index < tokens2.length){
143+
int second = Integer.parseInt(tokens2[index]);
144+
if(second != 0) return -1;
145+
}
146+
index++;
147+
}
148+
return 0;
149+
}
150+
}
123151
```

leetcode/253. Meeting Rooms II.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,30 @@ return 2.
7272
return max;
7373
}
7474
}
75-
```
75+
```
76+
77+
### Amazon Seesion
78+
* Method 1: Pq + sort
79+
```Java
80+
class Solution {
81+
public int minMeetingRooms(int[][] intervals) {
82+
if(intervals.length <= 1) return intervals.length;
83+
int result = 1;
84+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b)->{
85+
return a[1] - b[1];
86+
});
87+
Arrays.sort(intervals, (a, b)->{
88+
return a[0] != b[0] ? a[0] - b[0]: a[1] - b[1];
89+
});
90+
pq.offer(intervals[0]);
91+
for(int i = 1; i < intervals.length; i++){
92+
if(intervals[i][0] >= pq.peek()[1]){
93+
pq.poll();
94+
}
95+
pq.offer(intervals[i]);
96+
result = Math.max(result, pq.size());
97+
}
98+
return result;
99+
}
100+
}
101+
```

leetcode/505. The Maze II.md

+100
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,105 @@ Meanwhile, we need to use some ideas from the shortest path to update the path.
210210
}
211211
```
212212

213+
### Amazon Session
214+
* Method 1: dfs
215+
```Java
216+
class Solution {
217+
private static final int[][] dir = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
218+
private int height, width;
219+
private int[][] maze;
220+
private int[] destination;
221+
private int result;
222+
private int[][] dist;
223+
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
224+
this.height = maze.length;
225+
this.width = maze[0].length;
226+
this.maze = maze;
227+
this.destination = destination;
228+
this.result = Integer.MAX_VALUE;
229+
this.dist = new int[height][width];
230+
for(int[] dd: dist) Arrays.fill(dd, Integer.MAX_VALUE);
231+
dist[start[0]][start[1]] = 0;
232+
if(start[0] == destination[0] && start[1] == destination[1]) return 0;
233+
for(int d = 0; d < 4; d++){
234+
dfs(start, 0, d);
235+
}
236+
return this.result == Integer.MAX_VALUE ? -1: this.result;
237+
}
238+
private void dfs(int[] cur, int temp, int pre){
239+
// cur: current position
240+
// temp: current step
241+
// pre: previous direction
242+
if(cur[0] == this.destination[0] && cur[1] == this.destination[1]){
243+
this.result = Math.min(this.result, temp);
244+
}else if(dist[cur[0]][cur[1]] < this.result){
245+
int tx = 0, ty = 0;
246+
for(int d = 0; d < 4; d++){ // 4 directions
247+
if(d + pre == 3 || d == pre) continue;
248+
int currentStep = temp + 1;
249+
tx = cur[0] + dir[d][0];
250+
ty = cur[1] + dir[d][1];
251+
while(tx >= 0 && tx < height && ty >= 0 && ty < width && maze[tx][ty] != 1){
252+
tx += dir[d][0];
253+
ty += dir[d][1];
254+
currentStep++;
255+
}
256+
tx -= dir[d][0];
257+
ty -= dir[d][1];
258+
--currentStep;
259+
if(dist[tx][ty] <= currentStep) continue;
260+
else{
261+
dist[tx][ty] = currentStep;
262+
dfs(new int[]{tx, ty}, currentStep, d);
263+
}
264+
}
265+
}
266+
}
267+
}
268+
```
269+
270+
* Method 2: bfs
271+
```Java
272+
class Solution {
273+
private static final int[][] dir = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
274+
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
275+
int height = maze.length, width = maze[0].length;
276+
if(start[0] == destination[0] && start[1] == destination[1]) return 0;
277+
int[][] dist = new int[height][width];
278+
for(int[] dd : dist) Arrays.fill(dd, Integer.MAX_VALUE);
279+
dist[start[0]][start[1]] = 0;
280+
Queue<int[]> q = new LinkedList<>();
281+
q.offer(start);
282+
int result = Integer.MAX_VALUE;
283+
while(!q.isEmpty()){
284+
int[] cur = q.poll();
285+
if(cur[0] == destination[0] && cur[1] == destination[1]){
286+
result = Math.min(result, dist[cur[0]][cur[1]]);
287+
continue;
288+
}
289+
int tx = 0, ty = 0;
290+
for(int d = 0; d < 4; d++){
291+
int step = dist[cur[0]][cur[1]] + 1;
292+
tx = cur[0] + dir[d][0];
293+
ty = cur[1] + dir[d][1];
294+
while(tx >= 0 && tx < height && ty >= 0 && ty < width && maze[tx][ty] != 1){
295+
tx += dir[d][0];
296+
ty += dir[d][1];
297+
++step;
298+
}
299+
--step;
300+
tx -= dir[d][0];
301+
ty -= dir[d][1];
302+
if(tx == cur[0] && ty == cur[1]) continue;
303+
if(dist[tx][ty] > step){
304+
dist[tx][ty] = step;
305+
if(dist[tx][ty] < result) q.offer(new int[]{tx, ty});
306+
}
307+
}
308+
}
309+
return result == Integer.MAX_VALUE ? -1: result;
310+
}
311+
}
312+
```
213313
### Reference
214314
1. [Java 6ms BFS by optimizing solution #2](https://leetcode.com/problems/the-maze-ii/discuss/290079/Java-6ms-BFS-by-optimizing-solution-2)

0 commit comments

Comments
 (0)