|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.PriorityQueue; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +public class wk334 { |
| 10 | + //ranking: 635 / 5501 |
| 11 | + |
| 12 | + //前缀和 |
| 13 | + public int[] leftRigthDifference(int[] nums) { |
| 14 | + int[] sum = new int[nums.length + 1]; |
| 15 | + for (int i = nums.length - 1; i >= 0; i--) { |
| 16 | + sum[i] = sum[i + 1] + nums[i]; |
| 17 | + } |
| 18 | + |
| 19 | + int[] ans = new int[nums.length]; |
| 20 | + int pre = 0; |
| 21 | + for (int i = 0; i < nums.length; i++) { |
| 22 | + ans[i] = Math.abs(pre - sum[i + 1]); |
| 23 | + pre += nums[i]; |
| 24 | + } |
| 25 | + return ans; |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + //取模 |
| 30 | + public int[] divisibilityArray(String word, int m) { |
| 31 | + |
| 32 | + int[] ans = new int[word.length()]; |
| 33 | + long sum = 0; |
| 34 | + for (int i = 0; i < word.length(); i++) { |
| 35 | + int c = word.charAt(i) - '0'; |
| 36 | + sum = sum * 10 + c; |
| 37 | + sum %= m; |
| 38 | + if (sum == 0) { |
| 39 | + ans[i] = 1; |
| 40 | + } |
| 41 | + } |
| 42 | + return ans; |
| 43 | + } |
| 44 | + |
| 45 | + //贪心 |
| 46 | + //排序后 前一半和后一半比较即可,最多n/2对 |
| 47 | + static public int maxNumOfMarkedIndices(int[] nums) { |
| 48 | + Arrays.sort(nums); |
| 49 | + int right = nums.length - 1; |
| 50 | + int ans = 0; |
| 51 | + for (int i = (nums.length) / 2 - 1; i >= 0; i--) { |
| 52 | + if (right >= 0 && nums[i] * 2 <= nums[right]) { |
| 53 | + ans += 2; |
| 54 | + right--; |
| 55 | + } |
| 56 | + } |
| 57 | + return ans; |
| 58 | + } |
| 59 | + |
| 60 | + public static void main(String[] args) { |
| 61 | + minimumTime(new int[][]{ |
| 62 | + {0,1,99},{3,99,99},{4,5,6} |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + // Dijkstra变形 |
| 68 | + static int[][] moves = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; |
| 69 | + |
| 70 | + static public int minimumTime(int[][] grid) { |
| 71 | + if (grid[0][1] > 1 && grid[1][0] > 1) // 如果迂回 |
| 72 | + return -1; |
| 73 | + int[][] dp = new int[grid.length][grid[0].length]; |
| 74 | + for (int[] ints : dp) { |
| 75 | + Arrays.fill(ints, (int) 1e9 + 7); |
| 76 | + } |
| 77 | + dp[0][0] = 0; |
| 78 | + PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
| 79 | + priorityQueue.add(new int[]{0, 0, 0}); |
| 80 | + |
| 81 | + while (!priorityQueue.isEmpty()) { |
| 82 | + int[] cur = priorityQueue.poll(); |
| 83 | + for (int[] move : moves) { |
| 84 | + int nx = cur[1] + move[0]; |
| 85 | + int ny = cur[2] + move[1]; |
| 86 | + if (nx < grid.length && ny < grid[0].length && nx >= 0 && ny >= 0) { |
| 87 | + |
| 88 | + int step = cur[0]; |
| 89 | + //无法按时访问 |
| 90 | + if (grid[nx][ny] > cur[0] + 1) { |
| 91 | + //在起点无法迂回 |
| 92 | + if (cur[1] == 0 && cur[2] == 0) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + step += grid[nx][ny] - cur[0]; |
| 96 | + //此时必须要多一步 |
| 97 | + if ((grid[nx][ny] - cur[0]) % 2 == 0) step++; |
| 98 | + |
| 99 | + } else { |
| 100 | + step++; |
| 101 | + } |
| 102 | + if (dp[nx][ny] > step) { |
| 103 | + priorityQueue.add(new int[]{step, nx, ny}); |
| 104 | + dp[nx][ny] = step; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + return dp[grid.length - 1][grid[0].length - 1] == (int) 1e9 + 7 ? -1 : dp[grid.length - 1][grid[0].length - 1]; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments