|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.text.ParseException; |
| 4 | +import java.text.SimpleDateFormat; |
| 5 | +import java.time.LocalDate; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Date; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +public class wkb87 { |
| 12 | + //ranking: 712 / 4005 |
| 13 | + |
| 14 | + //这是做的啥啊? |
| 15 | + /* static public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) { |
| 16 | +
|
| 17 | + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
| 18 | + int res = 0; |
| 19 | + try { |
| 20 | + res = getRepeatDays(formatter.parse("2022-" + arriveAlice), formatter.parse("2022-" + leaveAlice), formatter.parse("2022-" + arriveBob), formatter.parse("2022-" + leaveBob)); |
| 21 | + } catch (ParseException e) { |
| 22 | + e.printStackTrace(); |
| 23 | + } |
| 24 | + return res; |
| 25 | + } |
| 26 | +
|
| 27 | + public static String getDayDifference(Date d1, Date d2) { |
| 28 | + StringBuffer ds = new StringBuffer(); |
| 29 | + long days = ((d1.getTime() - d2.getTime()) / 1000) / (3600 * 24); |
| 30 | + if (days >= 0) { |
| 31 | + return (days + 1) + ""; |
| 32 | + } |
| 33 | + return ds.toString(); |
| 34 | + } |
| 35 | +
|
| 36 | + public static int getRepeatDays(Date startDate1, Date endDate1, Date startDate2, Date endDate2) { |
| 37 | + long star1 = startDate1.getTime(); |
| 38 | + long end1 = endDate1.getTime(); |
| 39 | + long star2 = startDate2.getTime(); |
| 40 | + long end2 = endDate2.getTime(); |
| 41 | + String res; |
| 42 | +
|
| 43 | + if (star1 <= star2 && end1 >= end2) { |
| 44 | + res = getDayDifference(endDate2, startDate2); |
| 45 | + } else if (star1 >= star2 && end1 <= end2) { |
| 46 | +
|
| 47 | + res = getDayDifference(endDate1, startDate1); |
| 48 | + } else if (star1 >= star2 && star1 <= end2 && end2 <= end1) { |
| 49 | +
|
| 50 | + res = getDayDifference(endDate2, startDate1); |
| 51 | + } else if (star1 <= star2 && end1 <= end2 && end1 >= star2) { |
| 52 | +
|
| 53 | + res = getDayDifference(endDate1, startDate2); |
| 54 | + } else if (end1 <= star2 && star1 >= end2) { |
| 55 | + res = "0"; |
| 56 | + } else { |
| 57 | + res = "0"; |
| 58 | + } |
| 59 | + int numberDays = Integer.parseInt(res); |
| 60 | +
|
| 61 | + return numberDays; |
| 62 | + }*/ |
| 63 | + |
| 64 | + //直接调api做吧 |
| 65 | + private static final String YEAR = "2022-"; |
| 66 | + |
| 67 | + public int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) { |
| 68 | + LocalDate alice = LocalDate.parse(YEAR + arriveAlice), bob = LocalDate.parse(YEAR + arriveBob), arrive = alice.isAfter(bob) ? alice : bob; |
| 69 | + alice = LocalDate.parse(YEAR + leaveAlice); |
| 70 | + bob = LocalDate.parse(YEAR + leaveBob); |
| 71 | + return (int) Math.max(0L, (alice.isAfter(bob) ? bob : alice).toEpochDay() - arrive.toEpochDay() + 1L); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + //排序+双指针 |
| 76 | + public int matchPlayersAndTrainers(int[] players, int[] trainers) { |
| 77 | + Arrays.sort(players); |
| 78 | + Arrays.sort(trainers); |
| 79 | + int j = 0; |
| 80 | + int ans = 0; |
| 81 | + for (int i = 0; i < players.length; i++) { |
| 82 | + while (j < trainers.length && players[i] > trainers[j]) { |
| 83 | + j++; |
| 84 | + } |
| 85 | + if (j < trainers.length) { |
| 86 | + ans++; |
| 87 | + } |
| 88 | + j++; |
| 89 | + } |
| 90 | + return ans; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + //位运算 |
| 95 | + /* static public int[] smallestSubarrays(int[] nums) { |
| 96 | + List<Integer>[] dp = new List[32]; |
| 97 | + //考虑每个数字对每一位的贡献, |
| 98 | + // 比如 3的二进制是11,对第0和第1位有贡献 然后在每个位置讲坐标依次入队 |
| 99 | + for (int i = 0; i < dp.length; i++) { |
| 100 | + dp[i] = new ArrayList<>(); |
| 101 | + int a = 1 << i; |
| 102 | + for (int j = 0; j < nums.length; j++) { |
| 103 | + if ((a & nums[j]) != 0) { |
| 104 | + dp[i].add(j); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + int[] cur = new int[32]; |
| 109 | + for (int i = 0; i < nums.length; i++) { |
| 110 | + int max = i; |
| 111 | + for (int j = 0; j < cur.length; j++) { |
| 112 | + //判断队首的位置是不是超出i |
| 113 | + if (cur[j] < dp[j].size() && dp[j].get(cur[j]) < i) { |
| 114 | + cur[j]++; |
| 115 | + } |
| 116 | + // 求最大位置 |
| 117 | + if (cur[j] < dp[j].size()) { |
| 118 | + max = Math.max(max, dp[j].get(cur[j])); |
| 119 | + } |
| 120 | + } |
| 121 | + //只需要到max即可,max后面的对整体没有贡献 |
| 122 | + nums[i] = max - i + 1; |
| 123 | + } |
| 124 | + return nums; |
| 125 | + }*/ |
| 126 | + |
| 127 | + //从后往前遍历,减少遍历次数 |
| 128 | + static public int[] smallestSubarrays(int[] nums) { |
| 129 | + int[] ans = new int[nums.length]; |
| 130 | + int[] dp = new int[32]; |
| 131 | + for (int i = nums.length - 1; i >= 0; i--) { |
| 132 | + ans[i]=1; |
| 133 | + int num = nums[i]; |
| 134 | + for (int j = 0; j < 32; j++) { |
| 135 | + if ((num & (1 << j)) != 0) { |
| 136 | + dp[j] = i; |
| 137 | + } |
| 138 | + } |
| 139 | + int max=0; |
| 140 | + for (int j = 0; j < dp.length; j++) { |
| 141 | + max=Math.max(dp[j],max); |
| 142 | + } |
| 143 | + |
| 144 | + if(max==0){ |
| 145 | + continue; |
| 146 | + } |
| 147 | + ans[i]=max-i+1; |
| 148 | + } |
| 149 | + return ans; |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + //贪心 |
| 154 | + public long minimumMoney(int[][] transactions) { |
| 155 | + long money = 0; |
| 156 | + //记录所有 cost<cashback的赔钱情况,totalLose |
| 157 | + for (int i = 0; i < transactions.length; i++) { |
| 158 | + if (transactions[i][1] <= transactions[i][0]) { |
| 159 | + money += (transactions[i][0] - transactions[i][1]); |
| 160 | + } |
| 161 | + } |
| 162 | + long max = 0; |
| 163 | + |
| 164 | + //考虑所有的情况都可以完成交易 |
| 165 | + //对于cost<=cashback的交易,只要初始资金为 totalLose+max(cost),一定能保证这种交易完成。因为money一直在变多 |
| 166 | + //对于cost>cashback的交易, 只要初始资金为 totalLose+max(cashback),一定能完成交易。因为保证了一定大于任何一个cost |
| 167 | + //所以取两者最大值即可 |
| 168 | + for (int i = 0; i < transactions.length; i++) { |
| 169 | + if (transactions[i][1] <= transactions[i][0]) money -= (transactions[i][0] - transactions[i][1]); |
| 170 | + max = Math.max(max, money + transactions[i][0]); |
| 171 | + if (transactions[i][1] <= transactions[i][0]) money += (transactions[i][0] - transactions[i][1]); |
| 172 | + |
| 173 | + } |
| 174 | + return max; |
| 175 | + } |
| 176 | + |
| 177 | + public static void main(String[] args) { |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments