This commit is contained in:
kkunkka
2025-05-07 13:40:55 +08:00
parent fdcfae8c9a
commit ae4cbd4de1
4 changed files with 137 additions and 28 deletions

View File

@@ -0,0 +1,45 @@
package com.dota.graph._1721;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
class Solution {
public static void main(String[] args) {
new Solution().minTimeToReach(new int[][]{{0,4},{4,4}});
}
public int minTimeToReach(int[][] moveTime) {
int n = moveTime.length;
int m = moveTime[0].length;
var dis = new int[n][m];
for (int[] di : dis) {
Arrays.fill(di, Integer.MAX_VALUE);
}
dis[0][0] = 0;
var p = new PriorityQueue<int[]>(Comparator.comparingInt(a -> a[0]));
p.add(new int[]{0, 0, 0});
int[][] go = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while (!p.isEmpty()) {
var t = p.poll();
if (t[1] == n - 1 && t[2] == m - 1) {
return t[0];
}
if (t[0]>dis[t[1]][t[2]]) continue;
for (int[] g : go) {
int nx = t[1] + g[0];
int ny = t[2] + g[1];
if (nx < 0 || ny < 0 || nx >= n || ny >= m) {
continue;
}
int d = Math.max(t[0], moveTime[nx][ny]) + 1;
if (dis[nx][ny] > d) {
dis[nx][ny] = d;
p.add(new int[]{d, nx, ny});
}
}
}
return 1;
}
}

View File

@@ -0,0 +1,45 @@
package com.dota.graph._3342;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
class Solution {
public static void main(String[] args) {
new Solution().minTimeToReach(new int[][]{{0,4},{4,4}});
}
public int minTimeToReach(int[][] moveTime) {
int n = moveTime.length;
int m = moveTime[0].length;
var dis = new int[n][m];
for (int[] di : dis) {
Arrays.fill(di, Integer.MAX_VALUE);
}
dis[0][0] = 0;
var p = new PriorityQueue<int[]>(Comparator.comparingInt(a -> a[0]));
p.add(new int[]{0, 0, 0});
int[][] go = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while (!p.isEmpty()) {
var t = p.poll();
if (t[1] == n - 1 && t[2] == m - 1) {
return t[0];
}
if (t[0]>dis[t[1]][t[2]]) continue;
int time = (t[1] + t[2]) % 2 + 1;
for (int[] g : go) {
int nx = t[1] + g[0];
int ny = t[2] + g[1];
if (nx < 0 || ny < 0 || nx >= n || ny >= m) {
continue;
}
int d = Math.max(t[0], moveTime[nx][ny]) + time;
if (dis[nx][ny] > d) {
dis[nx][ny] = d;
p.add(new int[]{d, nx, ny});
}
}
}
return 1;
}
}

View File

@@ -1,35 +1,21 @@
package com.dota.slidingWindow._1712;
class Solution {
public static void main(String[] args) {
new Solution().waysToSplit(new int[]{0,0,3,3});
}
public int waysToSplit(int[] nums) {
var mod = (int) (1e9+7);
int n = nums.length;
int[] p = new int[n + 1];
for (int i = 0, t = 0; i < n; i++) {
t += nums[i];
p[i + 1] = t;
}
long res = 0;
int a = 0;
int sum = 0;
for (int num : nums) {
sum += num;
for (int i = 1, j = 2, k = 1; i < n - 1 && p[i] * 3 <= p[n]; i++) {
j = Math.max(i + 1, j);
while (j < n && p[j] - p[i] < p[i]) j++;
while (k < n - 1 && p[k + 1] - p[i] <= p[n] - p[k + 1]) k++;
res += k - j + 1;
}
int limit = sum / 3;
for (int i = 0; a <= limit; i++) {
a += nums[i];
if (a>limit) {
break;
}
int j = i + 1;
int t = (sum - a) / 2;
int b = 0;
while (b <= a) {
b += nums[j++];
}
int k = j;
while (b <= t) {
b += nums[k++];
}
res += (k - j);
}
return (int) res%1000000007;
return (int) (res % mod);
}
}

View File

@@ -0,0 +1,33 @@
package com.dota.slidingWindow._923;
import java.util.Arrays;
class Solution {
public int threeSumMulti(int[] A, int target) {
int MOD = 1_000_000_007;
long ans = 0;
long[] cnt= new long[101];
for (int i : A) {
cnt[i]++;
}
for (int i = 0; i < 101; i++) {
for (int j = i; j < 101; j++) {
if (cnt[j]==0) continue;
if (i+j>target) {
break;
}
int k = target-i-j;
if (k>100)continue;
if (i==j&&i==k) {
ans += (cnt[i]*(cnt[i]-1)*(cnt[i]-2))/6;
}else if (j==i) {
ans += cnt[i]*(cnt[i]-1)/2 * cnt[k];
} else if (j<k){
ans += cnt[i]*cnt[j]*cnt[k];
}
}
}
return (int) (ans%MOD);
}
}