-
Notifications
You must be signed in to change notification settings - Fork 0
/
wkb108.java
123 lines (106 loc) · 3.52 KB
/
wkb108.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
package weekly;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
public class wkb108 {
//模拟
public int alternatingSubarray(int[] nums) {
int ans = 0;
for (int i = 0; i < nums.length; i++) {
int flag = 1;
int j = i + 1;
for (; j < nums.length; j++) {
if (nums[j] - nums[j - 1] != flag) {
break;
}
flag *= -1;
}
ans = Math.max(ans, j - i);
}
return ans == 1 ? -1 : ans;
}
//哈希
public List<Integer> relocateMarbles(int[] nums, int[] moveFrom, int[] moveTo) {
Set<Integer> set = new TreeSet<>();
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
}
for (int i = 0; i < moveFrom.length; i++) {
set.remove(moveFrom[i]);
set.add(moveTo[i]);
}
List<Integer> res = new ArrayList<>(set);
return res;
}
//dp
public int minimumBeautifulSubstrings(String s) {
int[] dp = new int[s.length() + 1];
Arrays.fill(dp, (int) 1e9 + 7);
dp[0] = 0;
Set<String> set = new HashSet<>();
int ans = 1;
//先求出5的幂的2进制字符串
for (int i = 0; i < 12; i++) {
set.add(Integer.toBinaryString(ans));
ans *= 5;
}
//dp枚举
for (int i = 0; i < s.length(); i++) {
for (int start = 0; start <= i; start++) {
String sub = s.substring(start, i + 1);
if (sub.startsWith("0")) continue;
if (!set.contains(sub)) continue;
dp[i + 1] = Math.min(dp[start] + 1, dp[i + 1]);
}
}
return dp[s.length()] == (int) 1e7 ? -1 : dp[s.length()];
}
//枚举黑格子,每个格子只对四个有贡献,一次求对他们的贡献
public long[] countBlackBlocks(int m, int n, int[][] coordinates) {
Map<Long, Integer> map = new HashMap<>();
for (int[] coordinate : coordinates) {
//左上角
int x = coordinate[0] - 1, y = coordinate[1] - 1;
if (x >= 0 && y >= 0 && x < m && y < n) {
long ans = (long) x * n + y;
map.put(ans, map.getOrDefault(ans, 0) + 1);
}
//右上角
x = coordinate[0] - 1;
y = coordinate[1] + 1;
if (x >= 0 && y >= 0 && x < m && y < n) {
long ans = (long) x * n + y - 1;
map.put(ans, map.getOrDefault(ans, 0) + 1);
}
//左下角
x = coordinate[0] + 1;
y = coordinate[1] - 1;
if (x >= 0 && y >= 0 && x < m && y < n) {
long ans = (long) (x - 1) * n + y;
map.put(ans, map.getOrDefault(ans, 0) + 1);
}
//右下角
x = coordinate[0] + 1;
y = coordinate[1] + 1;
if (x >= 0 && y >= 0 && x < m && y < n) {
long ans = (long) (x - 1) * n + y - 1;
map.put(ans, map.getOrDefault(ans, 0) + 1);
}
}
long[] res = new long[5];
long c = 0;
for (int value : map.values()) {
res[value]++;
c++;
}
res[0] = (long) (m - 1) * (n - 1) - c;
return res;
}
public static void main(String[] args) {
}
}