kk
This commit is contained in:
175
src/main/java/com/dota/binarySearch/_2258/Solution.java
Normal file
175
src/main/java/com/dota/binarySearch/_2258/Solution.java
Normal file
@@ -0,0 +1,175 @@
|
||||
package com.dota.binarySearch._2258;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
|
||||
record N(int x, int y) {
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public static void main(String[] args) {
|
||||
new Solution().maximumMinutes(new int[][]{{0,2,0,0,0,0,0},{0,0,0,2,2,1,0},{0,2,0,0,1,2,0},{0,0,2,2,2,0,2},{0,0,0,0,0,0,0}});
|
||||
}
|
||||
int[][] next = new int[][]{{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
|
||||
public int maximumMinutes(int[][] grid) {
|
||||
int n = grid.length, m = grid[0].length;
|
||||
int[][] copy = new int[n][];
|
||||
for (int i = 0; i < n; i++) {
|
||||
copy[i] = new int[m];
|
||||
System.arraycopy(grid[i], 0, copy[i], 0, m);
|
||||
}
|
||||
if (errorRen(copy)) return -1;
|
||||
for (int i = 0; i < n; i++) {
|
||||
copy[i] = new int[m];
|
||||
System.arraycopy(grid[i], 0, copy[i], 0, m);
|
||||
}
|
||||
if (error(copy)) return 1000_000_000;
|
||||
int l = -1, r = n * m;
|
||||
var queue = new ArrayDeque<N>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (grid[i][j] == 1) {
|
||||
for (int[] ints : next) {
|
||||
var ne = new N(i + ints[0], j + ints[1]);
|
||||
if (checkNodeFire(grid, ne)) {
|
||||
queue.add(ne);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while (l + 1 < r) {
|
||||
int mid = l + (r - l) / 2;
|
||||
for (int i = 0; i < n; i++) {
|
||||
copy[i] = new int[m];
|
||||
System.arraycopy(grid[i], 0, copy[i], 0, m);
|
||||
}
|
||||
if (check(copy, mid, queue.clone())){
|
||||
l = mid;
|
||||
} else {
|
||||
r = mid;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
boolean check(int[][] grid, int k, Deque<N> queue) {
|
||||
if (k == -1) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < k; i++) {
|
||||
int size = queue.size();
|
||||
for (int j = 0; j < size; j++) {
|
||||
var node = queue.pollFirst();
|
||||
for (int[] ints : next) {
|
||||
var nextn = new N(node.x() +ints[0], node.y() + ints[1]);
|
||||
if (checkNodeFire(grid, nextn)) {
|
||||
queue.add(nextn);
|
||||
}
|
||||
}
|
||||
|
||||
grid[node.x()][node.y()] = 1;
|
||||
}
|
||||
}
|
||||
if (grid[0][0] == 1) return false;
|
||||
|
||||
int n=grid.length, m=grid[0].length;
|
||||
var renQueue = new ArrayDeque<N>();
|
||||
if (grid[0][1]==0) {
|
||||
renQueue.add(new N(0, 1));
|
||||
}
|
||||
if (grid[1][0]==0) {
|
||||
renQueue.add(new N(1, 0));
|
||||
}
|
||||
while (!renQueue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
var node = queue.pollFirst();
|
||||
for (int[] ints : next) {
|
||||
var nextN = new N(node.x() + ints[0], node.y() + ints[1]);
|
||||
if (checkNodeFire(grid, nextN)) {
|
||||
queue.add(nextN);
|
||||
}
|
||||
}
|
||||
grid[node.x()][node.y()] = 1;
|
||||
if (node.x()==n-1 && node.y()==m-1) {
|
||||
return grid[node.x()-1][node.y()] == 3 || grid[node.x()][node.y()-1] == 3;
|
||||
}
|
||||
}
|
||||
|
||||
size = renQueue.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
var node = renQueue.pollFirst();
|
||||
if (!checkNodeRen(grid,node)) continue;
|
||||
for (int[] ints : next) {
|
||||
var nextN = new N(node.x() + ints[0], node.y() + ints[1]);
|
||||
if (node.x()==n-1&&node.y()==m-1 && grid[node.x()][node.y()]==0) {
|
||||
return true;
|
||||
}
|
||||
if (checkNodeRen(grid, nextN)) {
|
||||
renQueue.add(nextN);
|
||||
}
|
||||
}
|
||||
grid[node.x()][node.y()] = 3;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean error(int[][] grid) {
|
||||
var queue = new ArrayDeque<N>();
|
||||
int n = grid.length, m = grid[0].length;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
if (grid[i][j] == 1) {
|
||||
queue.add(new N(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
var nn = queue.pollFirst();
|
||||
if (nn.x() == 0 && nn.y() == 0) return false;
|
||||
grid[nn.x()][nn.y()] = 1;
|
||||
for (int[] ints : next) {
|
||||
var nextn = new N(nn.x()+ints[0], nn.y()+ints[1]);
|
||||
if (checkNodeFire(grid, nextn)) {
|
||||
queue.addLast(nextn);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean errorRen(int[][] grid) {
|
||||
var queue = new ArrayDeque<N>();
|
||||
int n = grid.length, m = grid[0].length;
|
||||
queue.add(new N(0, 0));
|
||||
while (!queue.isEmpty()) {
|
||||
var nn = queue.pollFirst();
|
||||
if (nn.x() == n-1 && nn.y() == m-1) return false;
|
||||
grid[nn.x()][nn.y()] = 2;
|
||||
for (int[] ints : next) {
|
||||
var nextn = new N(nn.x()+ints[0], nn.y()+ints[1]);
|
||||
if (checkNodeRen(grid, nextn)) {
|
||||
queue.addLast(nextn);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean checkNodeFire(int[][] grid, N nn) {
|
||||
int n = grid.length, m = grid[0].length;
|
||||
if (nn.x() < 0 || nn.y() < 0 || nn.x() >= n || nn.y() >= m) return false;
|
||||
return grid[nn.x()][nn.y()] != 1 && grid[nn.x()][nn.y()] != 2;
|
||||
}
|
||||
|
||||
boolean checkNodeRen(int[][] grid, N nn) {
|
||||
int n = grid.length, m = grid[0].length;
|
||||
if (nn.x() < 0 || nn.y() < 0 || nn.x() >= n || nn.y() >= m) return false;
|
||||
return grid[nn.x()][nn.y()] == 0;
|
||||
}
|
||||
}
|
25
src/main/java/com/dota/easy/_3442/Solution.java
Normal file
25
src/main/java/com/dota/easy/_3442/Solution.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.dota.easy._3442;
|
||||
|
||||
class Solution {
|
||||
public int maxDifference(String s) {
|
||||
var dp = new int[26];
|
||||
int jMax = 0, oMax = 0;
|
||||
int jMin = 100, oMin = 100;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
dp[s.charAt(i) - 'a']++;
|
||||
}
|
||||
for (int i : dp) {
|
||||
if (i>0) {
|
||||
if (i%2!=0) {
|
||||
jMax = Math.max(jMax, i);
|
||||
jMin = Math.min(jMin, i);
|
||||
} else {
|
||||
oMax = Math.max(oMax, i);
|
||||
oMin = Math.min(oMin, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Math.max(jMax-oMin, oMax-jMin);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user