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

Commit03a2113

Browse files
committed
[Function add]
1. Add leetcode solutions with amazon tag.
1 parent1312ecf commit03a2113

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
##1000. Minimum Cost to Merge Stones
2+
3+
###Question
4+
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones.
5+
6+
A move consists of merging exactly K consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these K piles.
7+
8+
Find the minimum cost to merge all piles of stones into one pile. If it is impossible, return -1.
9+
10+
```
11+
Example 1:
12+
13+
Input: stones = [3,2,4,1], K = 2
14+
Output: 20
15+
Explanation:
16+
We start with [3, 2, 4, 1].
17+
We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1].
18+
We merge [4, 1] for a cost of 5, and we are left with [5, 5].
19+
We merge [5, 5] for a cost of 10, and we are left with [10].
20+
The total cost was 20, and this is the minimum possible.
21+
22+
Example 2:
23+
24+
Input: stones = [3,2,4,1], K = 3
25+
Output: -1
26+
Explanation: After any merge operation, there are 2 piles left, and we can't merge anymore. So the task is impossible.
27+
28+
Example 3:
29+
30+
Input: stones = [3,5,1,2,6], K = 3
31+
Output: 25
32+
Explanation:
33+
We start with [3, 5, 1, 2, 6].
34+
We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6].
35+
We merge [3, 8, 6] for a cost of 17, and we are left with [17].
36+
The total cost was 25, and this is the minimum possible.
37+
```
38+
39+
Note:
40+
1. 1 <= stones.length <= 30
41+
2. 2 <= K <= 30
42+
3. 1 <= stones[i] <= 100
43+
44+
###Thinking:
45+
* Method: dp
46+
```Java
47+
classSolution {
48+
publicintmergeStones(int[]stones,intK) {
49+
int n= stones.length;
50+
if((n-1)% (K-1)!=0)return-1;
51+
int[][][] dp=newint[n][n][K+1];
52+
int[] sum=newint[n+1];
53+
for(int i=0; i< n; i++)
54+
sum[i+1]= sum[i]+ stones[i];
55+
for(int[][] dd: dp)
56+
for(int[] d: dd)
57+
Arrays.fill(d,Integer.MAX_VALUE);
58+
for(int i=0; i< n; i++)
59+
dp[i][i][1]=0;
60+
for(int l=2; l<= n; l++){
61+
for(int start=0; start<= n- l; start++){
62+
int end= start+ l-1;
63+
for(int k=2; k<=K; k++){
64+
for(int mid= start; mid< end; mid+=K-1)
65+
dp[start][end][k]=Math.min(dp[start][end][k], dp[start][mid][1]+ dp[mid+1][end][k-1]);
66+
dp[start][end][1]= dp[start][end][K]+ sum[end+1]- sum[start];
67+
}
68+
}
69+
}
70+
return dp[0][n-1][1];
71+
}
72+
}
73+
```
74+
75+
###Reference
76+
1. [花花酱LeetCode1000.MinimumCost toMergeStones](https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1000-minimum-cost-to-merge-stones/)

‎leetcode/207. Course Schedule.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,39 @@ class Solution {
176176
}
177177
```
178178

179+
###Amazon session
180+
* Method 1: Topological sort
181+
```Java
182+
class Solution {
183+
public boolean canFinish(int numCourses, int[][] prerequisites) {
184+
Map<Integer, Set<Integer>> map = new HashMap<>(); //key: current course, value: courses that cur is pre
185+
int[] indegree = new int[numCourses]; // indegree of course
186+
for(int[] requests : prerequisites){
187+
indegree[requests[0]]++;
188+
Set<Integer> req = map.getOrDefault(requests[1], new HashSet<>());
189+
req.add(requests[0]);
190+
map.put(requests[1], req);
191+
}
192+
Queue<Integer> q = new LinkedList<>();
193+
int finished = 0;
194+
for(int i = 0; i < numCourses; i++){
195+
if(indegree[i] == 0){
196+
q.offer(i);
197+
}
198+
}
199+
while(!q.isEmpty()){
200+
int c = q.poll();
201+
finished++;
202+
if(!map.containsKey(c)) continue; // current course is not any pre-request for any course.
203+
for(int course : map.get(c)){
204+
if(--indegree[course] == 0)
205+
q.offer(course);
206+
}
207+
}
208+
return finished == numCourses;
209+
}
210+
}
211+
```
212+
179213
###Reference
180214
1.[【图论】有向无环图的拓扑排序](https://www.cnblogs.com/en-heng/p/5085690.html)

‎leetcode/733. Flood Fill.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,36 @@ Note:
8383
}
8484
}
8585
```
86+
87+
###Amazon session
88+
* Method 1: dfs
89+
```Java
90+
class Solution {
91+
private static final int[][] dir = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
92+
private int cur;
93+
private int[][] image;
94+
private int newColor;
95+
private int height, width;
96+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
97+
this.cur = image[sr][sc];
98+
if(this.cur == newColor) return image;
99+
this.image = image;
100+
this.newColor = newColor;
101+
this.height = image.length;
102+
this.width = image[0].length;
103+
dfs(sr, sc);
104+
return this.image;
105+
}
106+
private void dfs(int x, int y){
107+
int tx = 0, ty = 0;
108+
image[x][y] = newColor;
109+
for(int d = 0; d < 4; d++){
110+
tx = x + dir[d][0];
111+
ty = y + dir[d][1];
112+
if(tx >= 0 && tx < height && ty >= 0 && ty < width && image[tx][ty] == cur){
113+
dfs(tx, ty);
114+
}
115+
}
116+
}
117+
}
118+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp