Skip to content

Commit ed62d9d

Browse files
committed
Added 2 solutions
1 parent 6158c75 commit ed62d9d

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
3+
int[][] points = new int[R * C][2];
4+
Queue<int[]> queue = new LinkedList<>();
5+
int idx = 0;
6+
queue.add(new int[]{r0, c0});
7+
boolean[][] visited = new boolean[R][C];
8+
9+
while (!queue.isEmpty()) {
10+
int[] curr = queue.poll();
11+
int r = curr[0];
12+
int c = curr[1];
13+
14+
if (r < 0 || r >= R || c < 0 || c >= C || visited[r][c]) {
15+
continue;
16+
}
17+
18+
points[idx++] = curr;
19+
visited[r][c] = true;
20+
21+
queue.add(new int[]{r, c - 1});
22+
queue.add(new int[]{r, c + 1});
23+
queue.add(new int[]{r - 1, c});
24+
queue.add(new int[]{r + 1, c});
25+
}
26+
27+
return points;
28+
}
29+
}

Easy/Two City Scheduling.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int twoCitySchedCost(int[][] costs) {
3+
Arrays.sort(costs, new Comparator<int[]>() {
4+
@Override
5+
public int compare(int[] o1, int[] o2) {
6+
return o1[0] - o1[1] - (o2[0] - o2[1]);
7+
}
8+
});
9+
10+
int total = 0;
11+
for (int i = 0; i < costs.length; i++) {
12+
if (i < costs.length / 2) {
13+
total += costs[i][0];
14+
}
15+
else {
16+
total += costs[i][1];
17+
}
18+
}
19+
20+
return total;
21+
}
22+
}

0 commit comments

Comments
 (0)