|
| 1 | +package weekly; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +public class wk400 { |
| 11 | + |
| 12 | + //遍历 |
| 13 | + public int minimumChairs(String s) { |
| 14 | + int count = 0; |
| 15 | + int ans = 0; |
| 16 | + for (char c : s.toCharArray()) { |
| 17 | + if (c == 'E') { |
| 18 | + count++; |
| 19 | + ans = Math.max(count, ans); |
| 20 | + } else { |
| 21 | + count--; |
| 22 | + } |
| 23 | + } |
| 24 | + return ans; |
| 25 | + } |
| 26 | + |
| 27 | +// public int countDays(int days, int[][] meetings) { |
| 28 | +// int[] count = new int[days + 1]; |
| 29 | +// for (int[] meeting : meetings) { |
| 30 | +// count[meeting[0] - 1]++; |
| 31 | +// count[meeting[1]]--; |
| 32 | +// } |
| 33 | +// int ans = 0; |
| 34 | +// for (int i = 0; i < count.length - 1; i++) { |
| 35 | +// if (i > 0) { |
| 36 | +// count[i] += count[i - 1]; |
| 37 | +// } |
| 38 | +// if (count[i] == 0) { |
| 39 | +// ans++; |
| 40 | +// } |
| 41 | +// |
| 42 | +// } |
| 43 | +// return ans; |
| 44 | +// } |
| 45 | + |
| 46 | + |
| 47 | + //排序 |
| 48 | + static public int countDays(int days, int[][] meetings) { |
| 49 | + Arrays.sort(meetings, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]); |
| 50 | + int start = 0, end = 0; |
| 51 | + int ans = 0; |
| 52 | + for (int[] meeting : meetings) { |
| 53 | + if (meeting[0] <= end) { |
| 54 | + end = Math.max(meeting[1], end); |
| 55 | + } else { |
| 56 | + ans += meeting[0] - end - 1; |
| 57 | + start = meeting[0]; |
| 58 | + end = Math.max(meeting[1], end); |
| 59 | + } |
| 60 | + } |
| 61 | + ans += days - end; |
| 62 | + return ans; |
| 63 | + } |
| 64 | + |
| 65 | + //模拟 |
| 66 | + public String clearStars(String s) { |
| 67 | + ArrayList<Integer>[] count = new ArrayList[26]; |
| 68 | + for (int i = 0; i < count.length; i++) { |
| 69 | + count[i] = new ArrayList<>(); |
| 70 | + } |
| 71 | + Set<Integer> delete = new HashSet<>(); |
| 72 | + for (int i = 0; i < s.length(); i++) { |
| 73 | + char c = s.charAt(i); |
| 74 | + if (c == '*') { |
| 75 | + for (int j = 0; j < count.length; j++) { |
| 76 | + if (count[j].size() > 0) { |
| 77 | + delete.add(count[j].get(count[j].size() - 1)); |
| 78 | + count[j].remove(count[j].size() - 1); |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } else { |
| 83 | + count[c - 'a'].add(i); |
| 84 | + } |
| 85 | + } |
| 86 | + StringBuilder sb = new StringBuilder(); |
| 87 | + for (int i = 0; i < s.length(); i++) { |
| 88 | + char c = s.charAt(i); |
| 89 | + if (c != '*' && !delete.contains(i)) { |
| 90 | + sb.append(c); |
| 91 | + } |
| 92 | + } |
| 93 | + return sb.toString(); |
| 94 | + } |
| 95 | + |
| 96 | +/* |
| 97 | + //超时 |
| 98 | + static public int minimumDifference(int[] nums, int k) { |
| 99 | + int ans = Integer.MAX_VALUE; |
| 100 | + for (int i = 0; i < nums.length; i++) { |
| 101 | + if (nums[i] <= k) { |
| 102 | + ans = Math.min(ans, k - nums[i]); |
| 103 | + } else { |
| 104 | + int temp = nums[i]; |
| 105 | + ans = Math.min(ans, Math.abs(temp-k)); |
| 106 | + for (int j = i + 1; j < nums.length; j++) { |
| 107 | + temp&=nums[j]; |
| 108 | + ans = Math.min(ans, Math.abs(temp-k)); |
| 109 | + if(temp<=k) break; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return ans; |
| 114 | + }*/ |
| 115 | + |
| 116 | + |
| 117 | + // and |
| 118 | + public int minimumDifference(int[] nums, int k) { |
| 119 | + int ans = Integer.MAX_VALUE; |
| 120 | + for (int i = 0; i < nums.length; i++) { |
| 121 | + int x = nums[i]; |
| 122 | + ans = Math.min(ans, Math.abs(x - k)); |
| 123 | + for (int j = i - 1; j >= 0 && (nums[j] & x) != nums[j]; j--) { |
| 124 | + nums[j] &= x; |
| 125 | + ans = Math.min(ans, Math.abs(nums[j] - k)); |
| 126 | + } |
| 127 | + } |
| 128 | + return ans; |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + public static void main(String[] args) { |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | +} |
0 commit comments