-
Notifications
You must be signed in to change notification settings - Fork 0
/
wk373.java
130 lines (116 loc) · 4 KB
/
wk373.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
package weekly;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class wk373 {
//模拟
static public boolean areSimilar(int[][] mat, int k) {
k %= mat[0].length;
int[][] ans = new int[mat.length][mat[0].length];
for (int i = 0; i < mat.length; i++) {
if (i % 2 == 0) {
for (int j = 0; j < mat[0].length; j++) {
int nj = (j + k) % (mat[0].length);
ans[i][nj] = mat[i][j];
}
} else {
for (int j = 0; j < mat[0].length; j++) {
int nj = (j - k + mat[0].length) % (mat[0].length);
ans[i][nj] = mat[i][j];
}
}
}
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
if (mat[i][j] != ans[i][j]) {
return false;
}
}
}
return true;
}
/* static public int beautifulSubstrings(String s, int k) {
int[] count = new int[s.length() + 1];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
count[i + 1] += count[i];
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count[i + 1]++;
}
}
int ans = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = i; j < s.length(); j++) {
int vowels = count[j + 1] - count[i];
int consonants = (j - i) + 1 - vowels;
if (vowels == consonants && (vowels * consonants) % k == 0) {
ans++;
}
}
}
return ans;
}*/
//排序分组 +填充原位置
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
List<int[]> list = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
list.add(new int[]{nums[i], i});
}
Collections.sort(list, (a, b) -> a[0] - b[0]);
List<Integer> indexList = new ArrayList<>();
List<Integer> numList = new ArrayList<>();
indexList.add(list.get(0)[1]);
numList.add(list.get(0)[0]);
int pre = list.get(0)[0];
int[] ans = new int[nums.length];
for (int i = 1; i < list.size(); i++) {
if (list.get(i)[0] - pre <= limit) {
} else {
Collections.sort(indexList);
for (int j = 0; j < indexList.size(); j++) {
ans[indexList.get(j)] = numList.get(j);
}
indexList = new ArrayList<>();
numList = new ArrayList<>();
}
pre = list.get(i)[0];
indexList.add(list.get(i)[1]);
numList.add(list.get(i)[0]);
}
Collections.sort(indexList);
for (int j = 0; j < indexList.size(); j++) {
ans[indexList.get(j)] = numList.get(j);
}
return ans;
}
//枚举+前缀和
public long beautifulSubstrings(String s, int k) {
int[] count = new int[s.length() + 1];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
count[i + 1] += count[i];
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count[i + 1]++;
}
}
int n = s.length();
int max = s.length() / 2;
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= max; i++) {
if ((i * i) % k == 0) {
list.add(i * 2);
}
}
int ans = 0;
for (Integer len : list) {
for (int i = 0; i+len <= s.length(); i++) {
int vowels = count[i + len] - count[i];
int consonants = (i+len-i) - vowels;
if (vowels == consonants && (vowels * consonants) % k == 0) {
ans++;
}
}
}
return ans;
}
}