Skip to content

Commit c6b09d6

Browse files
committed
weekly contest 337
1 parent d7d93bc commit c6b09d6

File tree

3 files changed

+232
-1
lines changed

3 files changed

+232
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Record leetcode contest and ideas every week, and encourage yourself to think mo
55
leetcode url: <https://leetcode.cn/u/cctest/>
66

77

8-
8+
* ☀️ [weekly contest 337](src/main/java/weekly/wk337.java) 模拟 | 模拟 | 回溯 | 同余分组
99
* 🐎️ [weekly contest 336](src/main/java/weekly/wk336.java) 模拟 | 排序 | 异或前缀和 | 贪心
1010
* 🐎️ [weekly contest 335](src/main/java/weekly/wk335.java) 模拟 | 层序遍历 | 质数分解 | 多重背包
1111
* 🐎️ [weekly contest 334](src/main/java/weekly/wk334.java) 前缀和 | 取模 | 贪心 | Dijkstra
@@ -51,6 +51,7 @@ leetcode url: <https://leetcode.cn/u/cctest/>
5151
* 🐶 [weekly contest 290](src/main/java/weekly/wk290.java)
5252
* 🐭 [weekly contest 280](src/main/java/weekly/wk289.java)
5353

54+
* 🐸 [biweekly contest 100](src/main/java/weekly/wkb100.java) 四维 | 贪心 | 堆 | 二分
5455
* 🐸 [biweekly contest 99](src/main/java/weekly/wkb99.java) 贪心 | 递推 | 区间合并 | 换根DP
5556
* 🐸 [biweekly contest 98](src/main/java/weekly/wkb98.java)
5657
* 🐸 [biweekly contest 97](src/main/java/weekly/wkb97.java) 模拟 | 哈希 | 滑动窗口 | DFS

src/main/java/weekly/wk337.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package weekly;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.Map;
7+
import java.util.Set;
8+
import java.util.TreeMap;
9+
10+
public class wk337 {
11+
12+
13+
//模拟
14+
public int[] evenOddBit(int n) {
15+
int even = 0, odd = 0;
16+
String s = Integer.toBinaryString(n);
17+
18+
for (int i = s.length() - 1; i >= 0; i--) {
19+
if (s.charAt(i) == '1') {
20+
if ((s.length() - i - 1) % 2 == 0) {
21+
even++;
22+
} else {
23+
odd++;
24+
}
25+
}
26+
}
27+
return new int[]{even, odd};
28+
}
29+
30+
//模拟
31+
//可以打散成一维
32+
public boolean checkValidGrid(int[][] grid) {
33+
int[][] moves = new int[][]{
34+
{1, 2}, {2, 1}, {-2, 1}, {1, -2},
35+
{-1, 2}, {2, -1}, {-2, -1}, {-1, -2},
36+
};
37+
38+
int x = 0, y = 0;
39+
int cur = 0;
40+
for (int i = 0; i < grid.length * grid[0].length - 1; i++) {
41+
boolean flag = false;
42+
for (int[] move : moves) {
43+
int nx = move[0] + x;
44+
int ny = move[1] + y;
45+
if (nx >= 0 && ny >= 0 && nx < grid.length && ny < grid[0].length) {
46+
if (grid[nx][ny] == cur + 1) {
47+
x = nx;
48+
y = ny;
49+
cur++;
50+
flag = true;
51+
break;
52+
}
53+
}
54+
55+
}
56+
if (!flag) {
57+
return false;
58+
}
59+
}
60+
return true;
61+
}
62+
63+
//回溯
64+
public int beautifulSubsets(int[] nums, int k) {
65+
Arrays.sort(nums);
66+
return help(new HashSet<>(),0,nums,k);
67+
}
68+
69+
int help(Set<Integer> set, int index, int[] nums, int k) {
70+
71+
if (index >= nums.length) {
72+
return 1;
73+
}
74+
int count = help(set,index+1,nums,k);
75+
if (!set.contains(nums[index] - k)) {
76+
set.add(nums[index]);
77+
count += help(set, index + 1, nums, k);
78+
set.remove(nums[index]);
79+
}
80+
return count;
81+
}
82+
83+
84+
//同余分组
85+
public int findSmallestInteger(int[] nums, int value) {
86+
int[] counter = new int[value];
87+
for (int num : nums) {
88+
int index = num % value;
89+
if (index < 0) index += value;
90+
counter[index]++;
91+
}
92+
int step = 0;
93+
while (true) {
94+
for (int i = 0; i < counter.length; i++) {
95+
if (counter[i] == 0) {
96+
return value * step + i;
97+
}
98+
counter[i]--;
99+
}
100+
step++;
101+
}
102+
}
103+
104+
public static void main(String[] args) {
105+
106+
}
107+
}

src/main/java/weekly/wkb100.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package weekly;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.PriorityQueue;
6+
import java.util.Set;
7+
8+
public class wkb100 {
9+
10+
//思维题?
11+
public int distMoney(int money, int children) {
12+
if (money < children) {
13+
return -1;
14+
}
15+
int ans = 0;
16+
for (int i = 0; i < children; i++) {
17+
if (money >= 8) {
18+
money -= 8;
19+
ans++;
20+
} else {
21+
if (money == 4 && children - i == 1) {
22+
ans--;
23+
} else if (money >= (children - i)) {
24+
25+
} else {
26+
int left = children - i - money;
27+
if (left != 0) {
28+
if (left % 7 == 0) {
29+
ans -= (left / 7);
30+
} else {
31+
ans -= (left / 7 + 1);
32+
}
33+
}
34+
}
35+
money = 0;
36+
break;
37+
}
38+
}
39+
if (money > 0) ans--;
40+
return ans;
41+
}
42+
43+
44+
//贪心+排序
45+
public int maximizeGreatness(int[] nums) {
46+
Arrays.sort(nums);
47+
int right = nums.length - 1;
48+
int ans = 0;
49+
for (int i = nums.length - 1; i >= 0; i--) {
50+
if (nums[right] > nums[i]) {
51+
right--;
52+
ans++;
53+
}
54+
}
55+
return ans;
56+
}
57+
58+
59+
//优先队列
60+
public long findScore(int[] nums) {
61+
PriorityQueue<int[]> priorityQueue = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
62+
for (int i = 0; i < nums.length; i++) {
63+
priorityQueue.add(new int[]{nums[i], i});
64+
}
65+
66+
long ans = 0;
67+
Set<Integer> set = new HashSet<>();
68+
while (!priorityQueue.isEmpty()) {
69+
int[] poll = priorityQueue.poll();
70+
if (set.contains(poll[1])) {
71+
continue;
72+
}
73+
set.add(poll[1] - 1);
74+
set.add(poll[1] + 1);
75+
ans += poll[0];
76+
}
77+
78+
return ans;
79+
}
80+
81+
82+
// 优先队列
83+
/*public long repairCars(int[] ranks, int cars) {
84+
PriorityQueue<long[]> priorityQueue = new PriorityQueue<>((a, b) -> Long.compare(a[0], b[0]));
85+
for (int rank : ranks) {
86+
priorityQueue.add(new long[]{rank, 1, rank});
87+
}
88+
long ans = 0;
89+
for (int i = 0; i < cars - 1; i++) {
90+
long[] poll = priorityQueue.poll();
91+
poll[1]++;
92+
poll[0] = poll[2] * poll[1] * poll[1];
93+
priorityQueue.add(poll);
94+
}
95+
return priorityQueue.poll()[0];
96+
}*/
97+
98+
// 二分
99+
public long repairCars(int[] ranks, int cars) {
100+
101+
long max = ranks[0];
102+
for (int i : ranks) {
103+
max = Math.min(max, i);
104+
}
105+
long left = 0;
106+
long right = (long)cars * cars * max;
107+
System.out.println(left + " " + right);
108+
while (left < right) {
109+
long mid = (left + right) / 2;
110+
System.out.println(mid);
111+
int s = 0;
112+
for (int rank : ranks) {
113+
s += Math.sqrt(mid / rank);
114+
}
115+
if (s >= cars) {
116+
right = mid;
117+
} else {
118+
left = mid + 1;
119+
}
120+
}
121+
return left;
122+
}
123+
}

0 commit comments

Comments
 (0)