-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk307.java
186 lines (162 loc) · 5.26 KB
/
wk307.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
public class wk307 {
//ranking: 659 / 7064
//简单题,注意比赛顺序是从后往前的..
public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {
int ans = 0;
for (int i = 0; i < energy.length; i++) {
if (energy[i] >= initialEnergy) {
//严格大于
ans += energy[i] - initialEnergy + 1;
initialEnergy += energy[i] - initialEnergy + 1;
}
//精力扣除
initialEnergy -= energy[i];
if (experience[i] >= initialExperience) {
//严格大于
ans += experience[i] - initialExperience + 1;
initialExperience += experience[i] - initialExperience + 1;
}
//经验上涨
initialExperience += experience[i];
}
return ans;
}
//中等题,注意边界条件,只需计算前半部分即可
public String largestPalindromic(String num) {
int[] count = new int[10];
for (char c : num.toCharArray()) {
count[c - '0']++;
}
StringBuilder sb = new StringBuilder();
for (int i = count.length - 1; i >= 0; i--) {
while (count[i] >= 2) {
sb.append(i);
count[i] -= 2;
}
}
boolean add = false;
int c = -1;
for (int i = count.length - 1; i >= 0; i--) {
if (count[i] > 0) {
add = true;
c = i;
break;
}
}
//
while (sb.length() > 0 && sb.charAt(0) == '0') {
sb.delete(0, 1);
}
//没有单独的>0的数字
if (c <= 0 && sb.length() == 0) return "0";
StringBuilder reverse = new StringBuilder(sb).reverse();
if (!add) {
return sb.append(reverse).toString();
} else {
return sb.append(c).append(reverse).toString();
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
//中等题,先计算图,然后bfs
public int amountOfTime(TreeNode root, int start) {
Map<Integer, List<Integer>> map = new HashMap<>();
help(root, map, -1);
Queue<Integer> queue = new LinkedList<>();
queue.add(start);
Set<Integer> set = new HashSet<>();
int time = 0;
set.add(start);
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
Integer poll = queue.poll();
if (!map.containsKey(poll)) continue;
for (Integer next : map.get(poll)) {
if (set.contains(next)) continue;
set.add(next);
queue.add(next);
}
}
time++;
}
return time - 1;
}
void help(TreeNode root, Map<Integer, List<Integer>> map, int up) {
if (!map.containsKey(root)) {
map.put(root.val, new ArrayList<>());
}
if (up != -1) {
map.get(root.val).add(up);
}
if (root.left != null) {
map.get(root.val).add(root.left.val);
help(root.left, map, root.val);
}
if (root.right != null) {
map.get(root.val).add(root.right.val);
help(root.right, map, root.val);
}
}
class Pair {
long sum = 0;
int index = 0;
Pair(long sum, int index) {
this.sum = sum;
this.index = index;
}
}
//困难题,当时没想出来,看了题解恍然大悟
public long kSum(int[] nums, int k) {
//最大的数一定是所有正数的和
long sum = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
sum += nums[i];
} else {
//变成正数,一律按减法处理
nums[i] =-nums[i];
}
}
if (k == 1) return sum;
Arrays.sort(nums);
PriorityQueue<Pair> priorityQueue = new PriorityQueue<>((a, b) -> (int) (b.sum - a.sum));
priorityQueue.add(new Pair(sum - nums[0], 0));
//依次对每个位置取或者不取
for(int i=2;i<k;i++){
Pair poll = priorityQueue.poll();
//System.out.println(poll.sum);
if (poll.index < nums.length-1) {
priorityQueue.add(new Pair(poll.sum - nums[poll.index + 1], poll.index + 1));
priorityQueue.add(new Pair(poll.sum + nums[poll.index]- nums[poll.index + 1], poll.index + 1));
}
}
return priorityQueue.poll().sum;
}
public static void main(String[] args) {
}
}