Skip to content

Commit 2c9a844

Browse files
committed
Solutions added for #542, #1396
1 parent ba4299b commit 2c9a844

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package org.sean.backtracking;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
7+
/***
8+
* 542. 01 Matrix
9+
*/
10+
public class MatrixMinDistFinder {
11+
private int[][] moves = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
12+
private boolean[][] visited;
13+
14+
// Lesson learnt : for searching target path, normally BFS is way more efficient than DFS
15+
// (e.g. similar problem : WordLadder).
16+
17+
// region Solution O(m*n) : BFS
18+
public int[][] updateMatrix(int[][] mat) {
19+
Deque<int[]> deque = new LinkedList<>();
20+
int rowLen = mat.length;
21+
int colLen = mat[0].length;
22+
visited = new boolean[rowLen][colLen];
23+
24+
for (int i = 0; i < rowLen; i++) {
25+
for (int j = 0; j < colLen; j++) {
26+
if (mat[i][j] == 0) {
27+
visited[i][j] = true;
28+
deque.offer(new int[]{i, j});
29+
}
30+
}
31+
}
32+
33+
search(mat, deque, rowLen, colLen);
34+
35+
return mat;
36+
}
37+
38+
private void search(int[][] mat, Queue<int[]> deque, int rowLen, int colLen) {
39+
int level = 0;
40+
41+
while (!deque.isEmpty()) {
42+
int sz = deque.size();
43+
44+
// ArrayList<int[]> list = new ArrayList<>(deque);
45+
// list.forEach(ints -> System.out.printf("(%d:%d) ", ints[0],ints[1]));
46+
// System.out.println("\n");
47+
48+
for (int k = 0; k < sz; k++) {
49+
int[] coord = deque.poll();
50+
int currR = coord[0];
51+
int currC = coord[1];
52+
53+
mat[currR][currC] = level;
54+
55+
for (int[] mv : moves) {
56+
int nR = currR + mv[0];
57+
int nC = currC + mv[1];
58+
59+
if (nR >= 0 && nR < rowLen && nC >= 0 && nC < colLen && !visited[nR][nC]) {
60+
visited[nR][nC] = true;
61+
deque.offer(new int[]{nR, nC});
62+
}
63+
}
64+
}
65+
66+
level++;
67+
}
68+
}
69+
// endregion
70+
71+
// region Solution O(m*n*4^(m*n)) : DFS - TLE!
72+
public int[][] updateMatrix0(int[][] mat) {
73+
int rowCnt = mat.length;
74+
int colCnt = mat[0].length;
75+
76+
visited = new boolean[rowCnt][colCnt];
77+
copy = new int[rowCnt][colCnt];
78+
79+
for (int i = 0; i < rowCnt; i++) {
80+
for (int j = 0; j < colCnt; j++) {
81+
if (mat[i][j] == 1) {
82+
visited[i][j] = true;
83+
copy[i][j] = traverse(mat, i, j, 0);
84+
visited[i][j] = false;
85+
}
86+
}
87+
}
88+
89+
return copy;
90+
}
91+
92+
int[][] copy;
93+
94+
private int traverse(int[][] mat, int i, int j, int len) {
95+
int rowCnt = mat.length;
96+
int colCnt = mat[0].length;
97+
98+
if (mat[i][j] == 0)
99+
return len;
100+
101+
int increased = -1;
102+
for (int[] move : moves) {
103+
int nR = i + move[0];
104+
int nC = j + move[1];
105+
106+
if (nR >= 0 && nR < rowCnt && nC >= 0 && nC < colCnt) {
107+
if (!visited[nR][nC]) {
108+
visited[nR][nC] = true;
109+
110+
if (mat[i][j] == 1) {
111+
int offset = traverse(mat, nR, nC, len + 1) - len;
112+
if (increased < 0) {
113+
increased = offset;
114+
} else {
115+
increased = Math.min(increased, offset);
116+
}
117+
} else
118+
return len;
119+
120+
visited[nR][nC] = false;
121+
}
122+
}
123+
}
124+
return len + Math.max(increased, 0);
125+
}
126+
127+
// endregion
128+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.sean.design;
2+
3+
import java.util.*;
4+
5+
/***
6+
* 1396. Design Underground System
7+
*/
8+
class UndergroundSystem {
9+
// <S:E, [timeSpent]>
10+
private final Map<String, List<Integer>> routineMap;
11+
12+
// <id, <checkedIn, time>>
13+
private final Map<Integer, String[]> checkinMap;
14+
15+
public UndergroundSystem() {
16+
routineMap = new HashMap<>();
17+
checkinMap = new HashMap<>();
18+
}
19+
20+
/***
21+
* Checks in at the station stationName at time t.
22+
*
23+
* @param id
24+
* @param stationName
25+
* @param t
26+
*/
27+
public void checkIn(int id, String stationName, int t) {
28+
checkinMap.put(id, new String[]{stationName, String.valueOf(t)});
29+
}
30+
31+
/***
32+
* Checks out from the station stationName at time t.
33+
*
34+
* @param id
35+
* @param stationName
36+
* @param t
37+
*/
38+
public void checkOut(int id, String stationName, int t) {
39+
String[] pair = checkinMap.get(id);
40+
if (pair != null) {
41+
String key = pair[0] + ":" + stationName;
42+
List<Integer> lst = routineMap.getOrDefault(key, new ArrayList<>());
43+
lst.add(t - Integer.parseInt(pair[1]));
44+
routineMap.put(key, lst);
45+
46+
checkinMap.remove(id);
47+
}
48+
}
49+
50+
/***
51+
* Returns the average time it takes to travel from startStation to endStation.
52+
*
53+
* The average time is computed from all the previous traveling times from startStation to endStation that happened
54+
* directly, meaning a check in at startStation followed by a checkout from endStation.
55+
*
56+
* @param startStation
57+
* @param endStation
58+
* @return
59+
*/
60+
public double getAverageTime(String startStation, String endStation) {
61+
List<Integer> lst = routineMap.getOrDefault(startStation + ":" + endStation, new ArrayList<>());
62+
63+
if (lst.size() == 0)
64+
return 0;
65+
66+
int sum = lst.stream().mapToInt(value -> value).sum();
67+
return sum * 1.0 / lst.size();
68+
}
69+
}
70+
71+
/**
72+
* Your UndergroundSystem object will be instantiated and called as such:
73+
* UndergroundSystem obj = new UndergroundSystem();
74+
* obj.checkIn(id,stationName,t);
75+
* obj.checkOut(id,stationName,t);
76+
* double param_3 = obj.getAverageTime(startStation,endStation);
77+
*/

0 commit comments

Comments
 (0)