kk
This commit is contained in:
57
src/main/java/com/dota/gridGraph/_2146/Solution.java
Normal file
57
src/main/java/com/dota/gridGraph/_2146/Solution.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.dota.gridGraph._2146;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Solution {
|
||||
public static void main(String[] args) {
|
||||
new Solution().highestRankedKItems(new int[][]{{1,2,0,1},{1,3,3,1},{0,2,5,1}},new int[]{2,3}, new int[]{2,3},2);
|
||||
}
|
||||
public List<List<Integer>> highestRankedKItems(int[][] grid, int[] pricing, int[] start, int k) {
|
||||
var res = new ArrayList<List<Integer>>();
|
||||
var queue = new ArrayDeque<int[]>();
|
||||
queue.add(start);
|
||||
var cnt = 0;
|
||||
var next = new int[][]{{0,-1},{0,1},{1,0},{-1,0}};
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
var arr = new ArrayList<int[]>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
var z = queue.poll();
|
||||
int x = z[0], y = z[1];
|
||||
if (grid[x][y]<=pricing[1]&&grid[x][y]>=pricing[0]) {
|
||||
arr.add(new int[]{cnt,x,y});
|
||||
}
|
||||
|
||||
if (grid[x][y] >0) {
|
||||
grid[x][y] = -grid[x][y];
|
||||
}
|
||||
for (int[] ns : next) {
|
||||
var nextx = x + ns[0];
|
||||
var nexty = y + ns[1];
|
||||
if (nextx<0||nexty<0||nextx>=grid.length||nexty>=grid[x].length||grid[nextx][nexty]<=0) continue;
|
||||
queue.offer(new int[]{nextx,nexty});
|
||||
}
|
||||
}
|
||||
arr.sort((a,b)->{
|
||||
if(a[0]!=b[0]) return a[0]-b[0];
|
||||
if (grid[a[1]][a[2]] != grid[b[1]][b[2]]) {
|
||||
return grid[b[1]][b[2]] - grid[a[1]][a[2]];
|
||||
}
|
||||
if (a[1]!=b[1]) {
|
||||
return a[1] - b[1];
|
||||
}
|
||||
return a[2]-b[2];
|
||||
});
|
||||
for (int[] ints : arr) {
|
||||
res.add(List.of(ints[1], ints[2]));
|
||||
if (res.size() == k) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
cnt++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
48
src/main/java/com/dota/gridGraph/_934/Solution.java
Normal file
48
src/main/java/com/dota/gridGraph/_934/Solution.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.dota.gridGraph._934;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
|
||||
class Solution {
|
||||
public int shortestBridge(int[][] grid) {
|
||||
var res = -1;
|
||||
int n = grid.length, m = grid[0].length;
|
||||
var deque = new ArrayDeque<int[]>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (grid[i][j] == 1) {
|
||||
df(i, j, grid, deque);
|
||||
}
|
||||
while (!deque.isEmpty()) {
|
||||
int size = deque.size();
|
||||
for (int l = 0; l < size; l++) {
|
||||
var k = deque.poll();
|
||||
var x = k[0];
|
||||
var y = k[1];
|
||||
if (x < 0 || y < 0 || x >= n || y >= m || grid[x][y] == 4) continue;
|
||||
if (grid[x][y] == 1) {
|
||||
return res;
|
||||
}
|
||||
grid[x][y] = 4;
|
||||
deque.offer(new int[]{x + 1, y});
|
||||
deque.offer(new int[]{x - 1, y});
|
||||
deque.offer(new int[]{x, y + 1});
|
||||
deque.offer(new int[]{x, y - 1});
|
||||
}
|
||||
res++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
void df(int i, int j, int[][] grid, ArrayDeque<int[]> deque) {
|
||||
if (i<0||j<0||i>=grid.length||j>=grid[0].length||grid[i][j]!=1) {
|
||||
return;
|
||||
}
|
||||
grid[i][j] = -1;
|
||||
deque.offer(new int[]{i, j});
|
||||
df(i+1,j,grid,deque);
|
||||
df(i-1,j,grid,deque);
|
||||
df(i,j-1,grid,deque);
|
||||
df(i,j+1,grid,deque);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user