-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.java
More file actions
404 lines (389 loc) · 9.85 KB
/
BFS.java
File metadata and controls
404 lines (389 loc) · 9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package algorithmtest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
public class BFS {
//#127 word ladder out of time //for better solution similar to dijkstra, look google note
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
helper127(beginWord, endWord, wordList, new boolean[wordList.size()], 0);
if(min==Integer.MAX_VALUE)
{
return 0;
}
return min;
}
int min=Integer.MAX_VALUE;
public void helper127(String beginWord, String endWord, List<String> wordList,boolean []visited,int size)
{
if(endWord.equals(beginWord))
{
min=Math.min(min, size+1);
return;
}
for(int i=0;i<wordList.size();i++)
{
if(visited[i])
{
continue;
}
int diff=diff127(beginWord,wordList.get(i));
if(diff==1)
{
visited[i]=true;
helper127(wordList.get(i), endWord, wordList, visited, size+1);
visited[i]=false;
}
else if(diff==0)
{
visited[i]=true;
helper127(wordList.get(i), endWord, wordList, visited, size);
visited[i]=false;
}
}
}
public int diff127(String a, String b) {
int res = 0;
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) != b.charAt(i)) {
res++;
}
}
return res;
}
//286 walls and gates
public void wallsAndGates(int[][] rooms) {
for (int i = 0; i < rooms.length; ++i) {
for (int j = 0; j < rooms[i].length; ++j) {
if (rooms[i][j]==0)
{
helper_dfs_286(rooms, i, j, 0,new boolean[rooms.length][rooms[0].length]);
}
}
}
}
public void helper_dfs_286(int[][] rooms,int x,int y,int num,boolean [][]visited)//start from 0 93ms
{
if(x<0||y<0||x>=rooms.length||y>=rooms[x].length||rooms[x][y]==-1||rooms[x][y]<num||visited[x][y])
{
return;
}
visited[x][y]=true;
rooms[x][y]=num;
helper_dfs_286(rooms, x, y+1, num+1, visited);
helper_dfs_286(rooms, x+1, y, num+1, visited);
helper_dfs_286(rooms, x, y-1, num+1, visited);
helper_dfs_286(rooms, x-1, y, num+1, visited);
}
public void wallsAndGates_v1(int[][] rooms) {
for (int i = 0; i < rooms.length; ++i) {
for (int j = 0; j < rooms[i].length; ++j) {
if (rooms[i][j]>0)
{
rooms[i][j]=helper_dfs_286_v1(rooms, i, j, 0,new boolean[rooms.length][rooms[0].length]);
}
}
}
}
public int helper_dfs_286_v1(int[][] rooms,int x,int y,int num,boolean [][]visited)//version:start from every room 647ms
{
if(x<0||y<0||x>=rooms.length||y>=rooms[x].length||rooms[x][y]==-1||visited[x][y])
{
return Integer.MAX_VALUE;
}
if(rooms[x][y]==0)
{
return num;
}
else
{
int min=Integer.MAX_VALUE;
visited[x][y]=true;
min=Math.min(helper_dfs_286_v1(rooms, x+1, y, num+1,visited),min);
min=Math.min(helper_dfs_286_v1(rooms, x-1, y, num+1,visited),min);
min=Math.min(helper_dfs_286_v1(rooms, x, y-1, num+1,visited),min);
min=Math.min(helper_dfs_286_v1(rooms, x, y+1, num+1,visited),min);
visited[x][y]=false;
return min;
}
}
//317. Shortest Distance from All Buildings bfs to complicated buy understand the idea in google note
//public int shortestDistance(int[][] grid) {
//
//}
//public int helper317(int[][] grid)
//{
//
// int min=Integer.MAX_VALUE;
//
// for (int i = 0; i < grid.length; i++) {
// for (int j = 0; j < grid[i].length; j++) {
// int sum=0;
// Queue <int[]>q=new LinkedList<int[]>();
// q.add(new int[]{i,j});
// while(!q.isEmpty())
// {
// int []a=q.poll();
// int x=a[0];
// int y=a[1];
// if(x<0||y<0||x>=grid.length||y>=grid[x].length||grid[x][y]==2||visited[x][y])
// }
// }
// }
//
//}
//207. Course Schedule
//try union find (can't do it) because parent child relation may be reverse
//try a method similar to union find but I can't find where. it work
//try dfs
public boolean canFinish(int numCourses, int[][] prerequisites) {
Map<Integer,List<Integer>> path= new HashMap<Integer,List<Integer>> ();
for (int i = 0; i < prerequisites.length; i++) {
int from=prerequisites[i][0];
int to=prerequisites[i][1];
if(!helper207(path, to, from))
{
if(!path.containsKey(from))
{
path.put(from,new ArrayList<Integer>());
path.get(from).add(to);
}
else
{
path.get(from).add(to);
}
}
else
{
return false;
}
}
return true;
}
public boolean helper207(Map<Integer,List<Integer>> path,int from,int to)
{
Queue<Integer> q=new LinkedList<Integer>();
q.offer(from);
while(!q.isEmpty())
{
int now=q.poll();
if(now==to)
{
return true;
}
if(path.containsKey(now))
{
List<Integer> temp=path.get(now);
for(int i=0;i<temp.size();i++)
{
q.offer(temp.get(i));
}
}
}
return false;
}
//public boolean helper207(Map<Integer,List<Integer>> path,Map<Integer,Integer> visited,int from,int to)
//{
// if(from==to)
// {
// return true;
// }
// if(!path.containsKey(from)||visited.get(from)>=path.get(from).size())
// {
// return false;
// }
// boolean flag=false;
// for(int i=0;i<path.get(from).size();i++)
// {
// flag=flag||helper207(path, visited, path.get(from).get(i), to);
// }
// return flag;
//}
//207 bfs from others leetcode
public boolean canFinish_bfs(int numCourses, int[][] prerequisites) {
ArrayList[] graph = new ArrayList[numCourses];
int[] degree = new int[numCourses];
Queue queue = new LinkedList();
int count=0;
for(int i=0;i<numCourses;i++)
graph[i] = new ArrayList();
for(int i=0; i<prerequisites.length;i++){
degree[prerequisites[i][1]]++;
graph[prerequisites[i][0]].add(prerequisites[i][1]);
}
for(int i=0; i<degree.length;i++){
if(degree[i] == 0){
queue.add(i);
count++;
}
}
while(queue.size() != 0){
int course = (int)queue.poll();
for(int i=0; i<graph[course].size();i++){
int pointer = (int)graph[course].get(i);
degree[pointer]--;
if(degree[pointer] == 0){
queue.add(pointer);
count++;
}
}
}
if(count == numCourses)
return true;
else
return false;
}
// 210 course schedule
public int[] findOrder(int numCourses, int[][] prerequisites) {
int []res= new int[numCourses];
int index=0;
ArrayList[] graph = new ArrayList[numCourses];
Queue<Integer>queue = new LinkedList();
for(int i=0;i<numCourses;i++)
graph[i] = new ArrayList();
for(int i = 0;i<prerequisites.length;i++)
{
graph[prerequisites[i][0]].add(prerequisites[i][1]);
}
for (int i = 0; i < graph.length; i++) {
if(graph[i].size()==0)
{
queue.offer(i);
}
}
if(queue.size()==0)
{
return new int[] {};
}
while(!queue.isEmpty())
{
int head=queue.poll();
res[index++]=head;
for(int i=0;i<graph.length;i++)
{
if(graph[i].contains(head))
{
int j=graph[i].indexOf(head);
graph[i].remove(j);
if(graph[i].size()==0)
{
queue.offer(i);
}
}
}
}
if(index==numCourses)
{
return res;
}
return new int[]{};
}
//630 Course Schedule III use dp
public int scheduleCourse(int[][] courses) {
Arrays.sort(courses,(a,b)->{return a[1]-b[1];});
//int []dp=new int[courses.length];
return helper630(courses, 0, 0);
}
public int helper630(int[][] courses,int index,int time)
{
if(index<0||index==courses.length)
{
return 0;
}
int notake=0;
int take=0;
if(time+courses[index][0]>courses[index][1])
{
notake = helper630(courses, index+1, time);
}
else
{
notake = helper630(courses, index+1, time);
take=helper630(courses, index+1, time+courses[index][0])+1;
}
return Math.max(notake,take);
}
//305 Number of Islands II beats 95% 100%space
public List<Integer> numIslands2(int m, int n, int[][] positions) {
int []all=new int[m*n];
List<Integer> res=new ArrayList<Integer>();
int now=0;
for(int i=0; i<all.length; i++) {
all[i]=-1;
}
for(int i=0;i<positions.length;i++)
{
int num=positions[i][0]*n+positions[i][1];
all[num]=num;
int up=positions[i][0]==0?0:union305(num,num-n,all);
int down=positions[i][0]==m-1?0:union305(num,num+n,all);
int left=positions[i][1]==0?0:union305(num,num-1,all);
int right=positions[i][1]==n-1?0:union305(num,num+1,all);
now = now-(up+down+left+right)+1;
res.add(now);
}
return res;
}
public int union305(int original,int near,int[] all)
{
if(all[near]==-1)
{
return 0;
}
int f1=find305(original,all);
int f2=find305(near,all);
if(f1!=f2)
{
all[f1]=all[f2];
return 1;
}
return 0;
}
public int find305(int num,int[] all)
{
while(all[num]!=(num))
{
num=all[num];
}
return num;
}
//200 number of island 1 try union find will work
public int numIslands(char[][] grid) {
int []all=new int[grid.length*grid[0].length];
int now=0;
for(int i=0; i<all.length; i++) {
all[i]=-1;
}
for(int i=0;i<grid.length;i++)
{
for (int j = 0; j < grid[i].length; j++) {
if(grid[i][j]=='1')
{
int num=i*grid[i].length+j;
all[num]=num;
int up=i==0?0:union305(num,num-grid[i].length,all);
int down=i==grid.length-1?0:union305(num,num+grid[i].length,all);
int left=j==0?0:union305(num,num-1,all);
int right=j==grid[i].length-1?0:union305(num,num+1,all);
now = now-(up+down+left+right)+1;
}
}
}
return now;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BFS b= new BFS();
int [][]t=new int[][] {{0,1},{1,2},{2,1},{1,0},{0,2},{0,0},{1,1}};
char [][]grid=new char [][] {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};
b.numIslands(grid);
b.findOrder(2,new int[][]{{1,0},{0,1}});
}
}