|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.PriorityQueue; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +public class wk362 { |
| 12 | + //暴力 |
| 13 | + /* public int numberOfPoints(List<List<Integer>> nums) { |
| 14 | + Set<Integer> set = new HashSet<>(); |
| 15 | + for (List<Integer> num : nums) { |
| 16 | + int start = num.get(0), end = num.get(1); |
| 17 | + for (int i = start; i <= end; i++) { |
| 18 | + set.add(i); |
| 19 | + } |
| 20 | + } |
| 21 | + int ans = 0; |
| 22 | + for (int i = 1; i <= 100; i++) { |
| 23 | + if (set.contains(i)) { |
| 24 | + ans++; |
| 25 | + } |
| 26 | + } |
| 27 | + return ans; |
| 28 | + } |
| 29 | +*/ |
| 30 | + //差分数组 |
| 31 | + public int numberOfPoints(List<List<Integer>> nums) { |
| 32 | + int[] res = new int[100 + 2]; |
| 33 | + for (List<Integer> num : nums) { |
| 34 | + res[num.get(0)]++; |
| 35 | + res[num.get(1) + 1]--; |
| 36 | + } |
| 37 | + int ans = 0; |
| 38 | + for (int i = 1; i < res.length; i++) { |
| 39 | + res[i] += res[i - 1]; |
| 40 | + if (res[i] > 0) ans++; |
| 41 | + } |
| 42 | + return ans; |
| 43 | + } |
| 44 | + |
| 45 | + //贪心 先斜着走 |
| 46 | + public boolean isReachableAtTime(int sx, int sy, int fx, int fy, int t) { |
| 47 | + if (sx == fx && sy == fy) { |
| 48 | + if (t == 1) return false; |
| 49 | + return true; |
| 50 | + } |
| 51 | + //斜着走 |
| 52 | + int min = Math.min(Math.abs(fx - sx), Math.abs(fy - sy)); |
| 53 | + int sub = Math.abs(fx - sx) - min + Math.abs(fy - sy) - min; |
| 54 | + if (sub + min > t) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + //全排列 |
| 62 | + public int minimumMoves(int[][] grid) { |
| 63 | + List<int[]> zero = new ArrayList<>(); |
| 64 | + List<int[]> more = new ArrayList<>(); |
| 65 | + for (int i = 0; i < grid.length; i++) { |
| 66 | + for (int j = 0; j < grid[i].length; j++) { |
| 67 | + if (grid[i][j] == 0) { |
| 68 | + zero.add(new int[]{i, j, 0}); |
| 69 | + } else if (grid[i][j] > 1) { |
| 70 | + for (int k = 1; k < grid[i][j]; k++) { |
| 71 | + more.add(new int[]{i, j, 0}); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return help(0, 0, zero, more); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + int help(int len, int dp, List<int[]> zero, List<int[]> more) { |
| 82 | + if (len >= zero.size()) { |
| 83 | + return 0; |
| 84 | + } |
| 85 | + int[] z = zero.get(len); |
| 86 | + int min = Integer.MAX_VALUE; |
| 87 | + for (int j = 0; j < more.size(); j++) { |
| 88 | + int[] m = more.get(j); |
| 89 | + if (m[2] == 1) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + m[2] = 1; |
| 93 | + int ndp = dp | (1 << j); |
| 94 | + int c = 0; |
| 95 | + |
| 96 | + c = help(len + 1, ndp, zero, more) + Math.abs(z[0] - m[0]) + Math.abs(z[1] - m[1]); |
| 97 | + |
| 98 | + min = Math.min(min, c); |
| 99 | + m[2] = 0; |
| 100 | + } |
| 101 | + |
| 102 | + return min; |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + public int numberOfWays(String s, String t, long k) { |
| 107 | + int MOD = 1000000007; |
| 108 | + int n = s.length(); |
| 109 | + long count = 0; |
| 110 | + |
| 111 | + for (int l = 1; l < n; l++) { |
| 112 | + String rotated = s.substring(n - l) + s.substring(0, n - l); |
| 113 | + if (rotated.equals(t)) { |
| 114 | + int gcd = gcd(l, n); |
| 115 | + int lcm = l * n / gcd; |
| 116 | + count = (count + (k * gcd) / lcm) % MOD; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (s.equals(t)) { |
| 121 | + count = (count + k) % MOD; |
| 122 | + } |
| 123 | + |
| 124 | + return (int) count; |
| 125 | + } |
| 126 | + |
| 127 | + public int gcd(int a, int b) { |
| 128 | + while (b != 0) { |
| 129 | + int temp = a % b; |
| 130 | + a = b; |
| 131 | + b = temp; |
| 132 | + } |
| 133 | + return a; |
| 134 | + } |
| 135 | + |
| 136 | + public static void main(String[] args) { |
| 137 | + wk362 w = new wk362(); |
| 138 | + w.minimumMoves(new int[][]{ |
| 139 | + {1, 2, 2}, {1, 1, 0}, {0, 1, 1} |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments