kk
This commit is contained in:
45
src/main/java/com/dota/graph/_1721/Solution.java
Normal file
45
src/main/java/com/dota/graph/_1721/Solution.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
45
src/main/java/com/dota/graph/_3342/Solution.java
Normal file
45
src/main/java/com/dota/graph/_3342/Solution.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,35 +1,21 @@
|
|||||||
package com.dota.slidingWindow._1712;
|
package com.dota.slidingWindow._1712;
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public static void main(String[] args) {
|
|
||||||
new Solution().waysToSplit(new int[]{0,0,3,3});
|
|
||||||
}
|
|
||||||
public int waysToSplit(int[] nums) {
|
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;
|
long res = 0;
|
||||||
int a = 0;
|
for (int i = 1, j = 2, k = 1; i < n - 1 && p[i] * 3 <= p[n]; i++) {
|
||||||
int sum = 0;
|
j = Math.max(i + 1, j);
|
||||||
for (int num : nums) {
|
while (j < n && p[j] - p[i] < p[i]) j++;
|
||||||
sum += num;
|
while (k < n - 1 && p[k + 1] - p[i] <= p[n] - p[k + 1]) k++;
|
||||||
|
res += k - j + 1;
|
||||||
}
|
}
|
||||||
|
return (int) (res % mod);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
src/main/java/com/dota/slidingWindow/_923/Solution.java
Normal file
33
src/main/java/com/dota/slidingWindow/_923/Solution.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user