|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +public class wk350 { |
| 6 | + //模拟 |
| 7 | + public int distanceTraveled(int mainTank, int additionalTank) { |
| 8 | + int ans = 0; |
| 9 | + while (mainTank > 0) { |
| 10 | + if (mainTank >= 5) { |
| 11 | + if (additionalTank > 0) { |
| 12 | + ans += 10 * 5; |
| 13 | + mainTank -= 5; |
| 14 | + mainTank += 1; |
| 15 | + additionalTank--; |
| 16 | + } else { |
| 17 | + ans += 10 * 5; |
| 18 | + mainTank -= 5; |
| 19 | + } |
| 20 | + } else { |
| 21 | + ans += mainTank * 10; |
| 22 | + mainTank = 0; |
| 23 | + } |
| 24 | + } |
| 25 | + return ans; |
| 26 | + } |
| 27 | + |
| 28 | + //排序 |
| 29 | + public int findValueOfPartition(int[] nums) { |
| 30 | + Arrays.sort(nums); |
| 31 | + int min = Integer.MAX_VALUE; |
| 32 | + for (int i = 1; i < nums.length; i++) { |
| 33 | + min = Math.min(min, nums[i] - nums[i - 1]); |
| 34 | + } |
| 35 | + return min; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + private int[][] memo; |
| 40 | + int mod = (int) 1e9 + 7; |
| 41 | + |
| 42 | + int dfs(int[] nums, int index, int pre, int state) { |
| 43 | + if (index >= nums.length) { |
| 44 | + return 1; |
| 45 | + } |
| 46 | + if (memo[pre + 1][state] != -1) { |
| 47 | + return memo[pre + 1][state]; |
| 48 | + } |
| 49 | + |
| 50 | + int count = 0; |
| 51 | + |
| 52 | + for (int i = 0; i < nums.length; i++) { |
| 53 | + if (((state >> i) & 1) == 0 && (pre == -1 || (nums[i] % nums[pre] == 0) || (nums[pre] % nums[i] == 0))) { |
| 54 | + count = (count + dfs(nums, index + 1, i, state | (1 << i))) % mod; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + memo[pre + 1][state] = count; |
| 59 | + return count; |
| 60 | + } |
| 61 | + |
| 62 | + //记忆化搜索 |
| 63 | + public int specialPerm(int[] nums) { |
| 64 | + memo = new int[nums.length * (nums.length + 1)][1 << nums.length]; |
| 65 | + for (int[] ints : memo) { |
| 66 | + Arrays.fill(ints, -1); |
| 67 | + } |
| 68 | + |
| 69 | + return dfs(nums, 0, -1, 0); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + /* public int paintWalls(int[] cost, int[] time) { |
| 74 | + int csum = 0; |
| 75 | + for (int c : time) { |
| 76 | + csum += c; |
| 77 | + } |
| 78 | + int[][] dp = new int[csum + 1][cost.length + 1]; |
| 79 | + int[][] count = new int[csum + 1][cost.length + 1]; |
| 80 | + for (int[] ints : dp) { |
| 81 | + Arrays.fill(ints, (int) 1e9 + 7); |
| 82 | + } |
| 83 | + for (int i = 0; i < dp[0].length; i++) { |
| 84 | + dp[0][i] = 0; |
| 85 | + } |
| 86 | + int ans = Integer.MAX_VALUE; |
| 87 | + for (int i = 0; i <= time.length; i++) { |
| 88 | + for (int j = 1; j <= cost.length; j++) { |
| 89 | + int t = time[j - 1]; |
| 90 | + int c = cost[j - 1]; |
| 91 | + if (i - t >= 0) { |
| 92 | + dp[i][j] = dp[i - t][j - 1] + c; |
| 93 | + count[i][j] = count[i - t][j - 1] + 1; |
| 94 | + } |
| 95 | + if (dp[i][j - 1] < dp[i][j]) { |
| 96 | + dp[i][j] = dp[i][j - 1]; |
| 97 | + count[i][j] = count[i][j - 1]; |
| 98 | + }else if(dp[i][j - 1] == dp[i][j]) { |
| 99 | + count[i][j] = Math.max(count[i][j], count[i][j - 1]); |
| 100 | + } |
| 101 | + dp[i][j] = Math.min(dp[i][j], dp[i][j - 1]); |
| 102 | + if ((i + count[i][j]) >= (time.length)) { |
| 103 | + ans = Math.min(dp[i][j], ans); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return ans; |
| 108 | + }*/ |
| 109 | + |
| 110 | + |
| 111 | + //要先化简题目+记忆化 |
| 112 | + /* public int paintWalls(int[] cost, int[] time) { |
| 113 | + int ts = 0; |
| 114 | + for (int i : time) { |
| 115 | + ts += i; |
| 116 | + } |
| 117 | + memos = new int[cost.length][cost.length * 2 + 1]; |
| 118 | + for (int[] ints : memos) { |
| 119 | + Arrays.fill(ints, -1); |
| 120 | + } |
| 121 | + return dfs(0, 0, cost, time); |
| 122 | + } |
| 123 | +
|
| 124 | + int inf = (int) 1e9 + 7; |
| 125 | + int[][] memos; |
| 126 | +
|
| 127 | + int dfs(int i, int j, int[] cost, int[] time) { |
| 128 | + if (j >= cost.length - i) { |
| 129 | + return 0; |
| 130 | + } |
| 131 | + if (i >= cost.length) return inf; |
| 132 | +
|
| 133 | + if (memos[i][cost.length + j] != -1) { |
| 134 | + return memos[i][cost.length + j]; |
| 135 | + } |
| 136 | + int res = Math.min(dfs(i + 1, j + time[i], cost, time) + cost[i], dfs(i + 1, j - 1, cost, time)); |
| 137 | + memos[i][cost.length + j] = res; |
| 138 | + return res; |
| 139 | + }*/ |
| 140 | + |
| 141 | + int inf = (int) 1e9 + 7; |
| 142 | + |
| 143 | + //递推 |
| 144 | + public int paintWalls(int[] cost, int[] time) { |
| 145 | + int[] dp = new int[cost.length + 1]; |
| 146 | + Arrays.fill(dp, inf); |
| 147 | + dp[0] = 0; |
| 148 | + for (int i = 0; i < cost.length; i++) { |
| 149 | + for (int j = cost.length; j >= 0; j--) { |
| 150 | + dp[j] = Math.min(dp[j], dp[Math.max(j - time[i] - 1, 0)]+cost[i]); |
| 151 | + } |
| 152 | + } |
| 153 | + return dp[cost.length]; |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments