-
Notifications
You must be signed in to change notification settings - Fork 0
/
wkb86.java
145 lines (129 loc) · 4.55 KB
/
wkb86.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package weekly;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeMap;
public class wkb86 {
//简单题, set记录数组和
public boolean findSubarrays(int[] nums) {
Set<Integer> set = new HashSet<>();
int sum = nums[0];
for (int i = 1; i < nums.length; i++) {
sum += nums[i];
if (set.contains(sum)) return true;
set.add(sum);
sum -= nums[i - 1];
}
return false;
}
//中等题,进制转换+回文检测
//可以直接返回false,n>=5时,n的(n-2)进制是12,不是回文
public boolean isStrictlyPalindromic(int n) {
for (int i = 2; i < n; i++) {
if (!help(n, i)) return false;
}
return true;
}
boolean help(int n, int k) {
StringBuilder sb = new StringBuilder();
while (n >= k) {
sb.append(n % k);
n /= k;
}
if (n > 0) sb.append(n);
for (int i = 0; i < sb.length() / 2; i++) {
if (sb.charAt(i) != sb.charAt(sb.length() - i - 1)) return false;
}
return true;
}
//中等题,枚举子序列
static public int maximumRows(int[][] mat, int cols) {
//记录每一行的二进制表示
int[] dp = new int[mat.length];
for (int i = 0; i < mat.length; i++) {
int k = 0;
for (int j = 0; j < mat[i].length; j++) {
k = (k << 1) + mat[i][j];
}
dp[i] = k;
//System.out.println(Integer.toBinaryString(k));
}
//子序列枚举,没必要子序列,可以直接遍历
int state = (1 << mat[0].length) - 1;
int max = 0;
for (int i = state; i > 0; i = (i - 1) & state) {
if (Integer.bitCount(i) != cols) continue;
int count = 0;
//求是不是被覆盖的
for (int j = 0; j < dp.length; j++) {
if ((dp[j] | i) == i) count++;
}
// System.out.println(Integer.toBinaryString(i) + " " + count);
max = Math.max(max, count);
}
return max;
}
//困难题,注意题目是连续,所以可以滑动窗口+treemap记录最大的chargeTime
/* public int maximumRobots(int[] chargeTimes, int[] runningCosts, long budget) {
int left = 0;
long sumRunning = 0;
int k = 0;
TreeMap<Integer, Integer> counter = new TreeMap<>();
int ans = 0;
for (int i = 0; i < chargeTimes.length; i++) {
sumRunning += runningCosts[i];
k++;
counter.put(chargeTimes[i], counter.getOrDefault(chargeTimes[i], 0) + 1);
long c = counter.lastKey() + k * sumRunning;
while (left <= i && c > budget) {
Integer le = counter.get(chargeTimes[left]) - 1;
if (le == 0) {
counter.remove(chargeTimes[left]);
} else {
counter.put(chargeTimes[left], le);
}
sumRunning-=runningCosts[left];
left++;
k--;
if (counter.size() == 0) {
break;
}
c = counter.lastKey() + k * sumRunning;
}
ans = Math.max(ans, k);
}
return ans;
}*/
//单调队列写法
static public int maximumRobots(int[] chargeTimes, int[] runningCosts, long budget) {
int left = 0;
long sumRunning = 0;
Deque<Integer> deque = new ArrayDeque<>();
int ans = 0;
for (int i = 0; i < chargeTimes.length; i++) {
sumRunning += runningCosts[i];
while (!deque.isEmpty() && chargeTimes[deque.peekLast()] <= chargeTimes[i]) {
deque.pollLast();
}
deque.addLast(i);
long c = chargeTimes[deque.peekFirst()] + (i-left+1) * sumRunning;
while (left <= i && c > budget) {
sumRunning -= runningCosts[left];
left++;
while (!deque.isEmpty() && deque.peekFirst() < left) {
deque.pollFirst();
}
if (deque.isEmpty()) {
break;
}
c = chargeTimes[deque.peekFirst()] + (i-left+1) * sumRunning;
}
ans = Math.max(ans, (i-left+1));
}
return ans;
}
public static void main(String[] args) {
maximumRobots(new int[]{11,12,19},new int[]{10,8,7},19);
}
}