|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
| 5 | + |
| 6 | +public class wk398 { |
| 7 | + |
| 8 | + |
| 9 | + //遍历 |
| 10 | + public boolean isArraySpecial(int[] nums) { |
| 11 | + for (int i = 1; i < nums.length; i++) { |
| 12 | + if (nums[i] % 2 == nums[i - 1] % 2) { |
| 13 | + return false; |
| 14 | + } |
| 15 | + } |
| 16 | + return true; |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + //前缀和 |
| 21 | + static public boolean[] isArraySpecial(int[] nums, int[][] queries) { |
| 22 | + |
| 23 | + int[] dp = new int[nums.length + 1]; |
| 24 | + int index = 1; |
| 25 | + dp[1] = index; |
| 26 | + |
| 27 | + for (int i = 1; i < nums.length; i++) { |
| 28 | + if (nums[i] % 2 == nums[i - 1] % 2) { |
| 29 | + index++; |
| 30 | + } |
| 31 | + dp[i + 1] = index; |
| 32 | + } |
| 33 | + |
| 34 | + int[] pre = new int[nums.length + 1]; |
| 35 | + for (int i = 1; i < dp.length; i++) { |
| 36 | + pre[i] = pre[i - 1] + dp[i]; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + boolean[] res = new boolean[queries.length]; |
| 41 | + for (int i = 0; i < queries.length; i++) { |
| 42 | + int[] query = queries[i]; |
| 43 | + int c = pre[query[1] + 1] - pre[query[0]]; |
| 44 | + if (c == dp[query[0] + 1] * (query[1] - query[0] + 1)) { |
| 45 | + res[i] = true; |
| 46 | + } |
| 47 | + } |
| 48 | + return res; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + //遍历 |
| 53 | + public long sumDigitDifferences(int[] nums) { |
| 54 | + int d = 1; |
| 55 | + long res = 0; |
| 56 | + while (nums[0] / d > 0) { |
| 57 | + int[] count = new int[10]; |
| 58 | + long tmp = 1; |
| 59 | + for (int num : nums) { |
| 60 | + int c = (num / d) % 10; |
| 61 | + count[c]++; |
| 62 | + } |
| 63 | + for (int i = 0; i < count.length; i++) { |
| 64 | + for (int j = i + 1; j < count.length; j++) { |
| 65 | + res += (long) count[i] * count[j]; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + d *= 10; |
| 70 | + } |
| 71 | + return res; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + // 记忆化 |
| 76 | + /* public int waysToReachStair(int k) { |
| 77 | + return dfs(1, k, 0, 0); |
| 78 | +
|
| 79 | + } |
| 80 | +
|
| 81 | + HashMap<Long,Integer> memo=new HashMap<>(); |
| 82 | +
|
| 83 | +
|
| 84 | + int dfs(int cur, int k, int jump, int back) { |
| 85 | + if (cur > k + 1) return 0; |
| 86 | + long key=((long)cur<<32)|((long) jump<<1)|back; |
| 87 | + if (memo.containsKey(key)) return memo.get(key); |
| 88 | + int res = 0; |
| 89 | + if (cur == k) res++; |
| 90 | + if (back == 0 && cur > 0) { |
| 91 | + res += dfs(cur - 1, k, jump, 1); |
| 92 | + } |
| 93 | + res += dfs(cur + (int) Math.pow(2, jump), k, jump + 1, 0); |
| 94 | + memo.put(key,res); |
| 95 | + return res; |
| 96 | + } |
| 97 | +*/ |
| 98 | + |
| 99 | + static int[][] dp = new int[32][2]; |
| 100 | + |
| 101 | + static { |
| 102 | + int a = 1; |
| 103 | + dp[0][1] = 1; |
| 104 | + for (int i = 0; i < 31; i++) { |
| 105 | + dp[i + 1][0] = dp[i][0] + a; |
| 106 | + dp[i + 1][1] = i + 2; |
| 107 | + a *= 2; |
| 108 | + } |
| 109 | +// for (int i = 0; i < dp.length; i++) { |
| 110 | +// System.out.println(dp[i][0] + " " + dp[i][1]); |
| 111 | +// } |
| 112 | + } |
| 113 | + |
| 114 | + // 组合数 |
| 115 | + public int waysToReachStair(int k) { |
| 116 | + int res = 0; |
| 117 | + for (int i = 0; i < dp.length; i++) { |
| 118 | + if (k <= 1+dp[i][0] &&k>=1+dp[i][0]-dp[i][1]) { |
| 119 | + int dis= dp[i][1]-(1+dp[i][0]-k); |
| 120 | + res+=comnination(dp[i][1],dis); |
| 121 | + } |
| 122 | + } |
| 123 | + return res; |
| 124 | + } |
| 125 | + |
| 126 | + // m>=n |
| 127 | + public int comnination(int m, int n) { |
| 128 | + if (n == 0) return 1; |
| 129 | + long ans = 1; |
| 130 | + |
| 131 | + //都从小的开始 防止过早溢出 |
| 132 | + for (int x = m - n + 1, y = 1; y <= n; ++x, ++y) { |
| 133 | + ans = ans * x / y; |
| 134 | + } |
| 135 | + return (int) ans; |
| 136 | + } |
| 137 | + |
| 138 | + public static void main(String[] args) { |
| 139 | + wk398 w = new wk398(); |
| 140 | + w.waysToReachStair(1); |
| 141 | + } |
| 142 | +} |
0 commit comments