第 373 场力扣周赛

循环移位后的矩阵相似检查

模拟。有个性质,如果左移 \(k\) 位之后相等,则右移 \(k\) 位也必定相等。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public boolean areSimilar(int[][] mat, int k) {
int m = mat.length, n = mat[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] != mat[i][((j + (i % 2 == 0 ? 1 : -1) * k) % n + n) % n]) {
return false;
}
}
}
return true;
}
}

统计美丽子字符串 I

将元音字母看作 \(1\),非元音字母看作 \(-1\),使用前缀和 + 哈希表的技巧,可以得到若干个分组,每组中任意两个下标构成的子数组都满足条件一。然后我们可以暴力判断所有满足条件一的子数组的长度是否满足条件二,时间复杂度为 \(O(n^{2})\)。(补充:可以纯暴力做,不需要分组。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int beautifulSubstrings(String s, int k) {
int n = s.length(), sum = 0;
Set<Character> set = Set.of('a', 'e', 'i', 'o', 'u');
Map<Integer, List<Integer>> map = new HashMap<>();
map.computeIfAbsent(0, t -> new ArrayList<>()).add(-1);
for (int i = 0; i < n; i++) {
sum += set.contains(s.charAt(i)) ? 1 : -1;
map.computeIfAbsent(sum, t -> new ArrayList<>()).add(i);
}
int ans = 0;
for (var list : map.values()) {
for (int j = 0; j < list.size() ; j++) {
for (int i = 0; i < j; i++) {
int len = (list.get(j) - list.get(i)) / 2;
if (len * len % k == 0) {
ans++;
}
}
}
}
return ans;
}
}

交换得到字典序最小的数组

如果 \(|nums[i]-nums[j]|<=limit\),那么就可以交换 \(nums[i]\) 和 \(nums[j]\),该交换的性质具有传递性,所以我们可以对原数组进行排序,只要相邻元素的差值小于等于 \(limit\),它们就在同一个可交换集合中。这样可以将原数组划分为若干可交换集合,然后对每个集合排序,从小到大排列即可。

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
class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length;
var aux = new Integer[n];
for (int i = 0; i < n; i++) {
aux[i] = i;
}
Arrays.sort(aux, (i, j) -> nums[i] - nums[j]);

int pre = -limit;
List<List<Integer>> buckets = new ArrayList<>();
for (int i : aux) {
if (nums[i] - pre > limit) {
buckets.add(new ArrayList<>());
}
buckets.get(buckets.size() - 1).add(i);
pre = nums[i];
}

int[] ans = new int[n];
for (var bucket : buckets) {
List<Integer> pos = new ArrayList<>();
pos.addAll(bucket);
Collections.sort(pos);
for (int i = 0; i < pos.size(); i++) {
ans[pos.get(i)] = nums[bucket.get(i)];
}
}
return ans;
}
}

统计美丽子字符串 II

朴素做法的瓶颈在 \((\frac{L}{2})^{2}\bmod{k}=0\) 的判断上,可以通过将条件二变换为 \(L\bmod{k^{\prime}}=0\),然后使用前缀和以及下标模 \(k^{\prime}\) 的值来分组,这样同组内的下标两两组合得到的必定是满足两个条件的子数组。灵神题解,时间复杂度 \(O(n+\sqrt{k})\)。还有另一种枚举的做法,题解

第 118 场力扣夜喵双周赛

查找包含给定字符的单词

模拟。

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public List<Integer> findWordsContaining(String[] words, char x) {
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
if (words[i].contains(x + "")) {
ans.add(i);
}
}
return ans;
}
}

最大化网格图中正方形空洞的面积

分别求出行和列的最长连续线段,然后最大正方形面积就是两者最小值加一的平方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int maximizeSquareHoleArea(int n, int m, int[] hBars, int[] vBars) {
Arrays.sort(hBars);
Arrays.sort(vBars);
int maxH = 0, maxV = 0;
for (int i = 0, j = 0; j < hBars.length; j++) {
if (hBars[j] - hBars[i] == j - i) {
maxH = Math.max(maxH, j - i + 1);
} else {
i = j;
}
}
for (int i = 0, j = 0; j < vBars.length; j++) {
if (vBars[j] - vBars[i] == j - i) {
maxV = Math.max(maxV, j - i + 1);
} else {
i = j;
}
}
int len = Math.min(maxH, maxV) + 1;
return len * len;
}
}

购买水果需要的最少金币数

动态规划,\(dp[i]\) 表示获取 \([i,n]\) 范围内所有水果所需的最少金币数,有 \(dp[i]=prices[i]+\min_{j=i+1}^{2i+1}{dp[j]}\),时间复杂度 \(O(n^{2})\)。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int minimumCoins(int[] prices) {
int n = prices.length;
for (int i = (n + 1) / 2 - 1; i > 0; i--) {
int min = Integer.MAX_VALUE;
for (int j = i + 1; j <= 2 * i + 1; j++) {
min = Math.min(min, prices[j - 1]);
}
prices[i - 1] += min;
}
return prices[0];
}
}

单调队列优化,时间复杂度 \(O(n)\)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int minimumCoins(int[] prices) {
int n = prices.length;
Deque<Integer> q = new ArrayDeque<>();
for (int i = n; i > 0; i--) {
while (!q.isEmpty() && q.peekFirst() > 2 * i + 1) {
q.pollFirst();
}
if (i <= (n + 1) / 2 - 1) {
prices[i - 1] += prices[q.peekFirst() - 1];
}
while (!q.isEmpty() && prices[q.peekLast() - 1] >= prices[i - 1]) {
q.pollLast();
}
q.offerLast(i);
}
return prices[q.peekLast() - 1];
}
}

找到最大非递减数组的长度

单调队列优化 DP,随缘补题。

AtCoder Beginner Contest 330

Counting Passes

模拟。

1
2
3
4
5
6
7
8
9
10
public static void solve() {
int n = io.nextInt(), l = io.nextInt();
int ans = 0;
for (int i = 0; i < n; i++) {
if (io.nextInt() >= l) {
ans++;
}
}
io.println(ans);
}

Minimize Abs 1

等价于求 \(y=|x-a_{i}|\) 在区间 \([L,R]\) 内的最小值对应的 \(x\)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void solve() {
int n = io.nextInt(), l = io.nextInt(), r = io.nextInt();
for (int i = 0; i < n; i++) {
int a = io.nextInt();
if (l <= a && a <= r) {
io.print(a + " ");
} else if (a < l) {
io.print(l + " ");
} else {
io.print(r + " ");
}
}
io.println();
}

Minimize Abs 2

对于每个固定的 \(x\),可以在 \(O(1)\) 时间内求出 \(|y^{2}+(x^{2}-D)|\) 的最小值(也可以二分),我们枚举 \([0,\lceil\sqrt{D}\rceil]\) 范围内的所有 \(x\)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void solve() {
long d = io.nextLong();
long ans = Long.MAX_VALUE;
long up = (long) Math.sqrt(d) + 1;
for (long x = 0; x <= up; x++) {
long t = x * x - d;
if (t >= 0) {
ans = Math.min(ans, t);
} else {
long y = (long) Math.sqrt(-t);
ans = Math.min(ans, Math.abs(y * y + x * x - d));
ans = Math.min(ans, Math.abs((y + 1) * (y + 1) + x * x - d));
}
}
io.println(ans);
}

Counting Ls

对行列计数,然后枚举交叉点。

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
public static void solve() {
int n = io.nextInt();
char[][] s = new char[n][];
for (int i = 0; i < n; i++) {
s[i] = io.next().toCharArray();
}

int[] row = new int[n];
int[] col = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'o') {
row[i]++;
col[j]++;
}
}
}

long ans = 0L;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'o' && col[j] > 1) {
ans += (long) (col[j] - 1) * (row[i] - 1);
}
}
}
io.println(ans);
}

Mex and Update

因为只有 \(n\) 个数,所以只需要考虑 \([0,n]\) 范围的数的增删,这样集合就可以存储单个数。比赛时没注意,使用的是区间,然后删除区间中的数,需要进行分裂,会麻烦很多,还需要排序以及考虑最左和最右的特殊区间。

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
public static void solve() {
int n = io.nextInt(), q = io.nextInt();
int[] a = new int[n];
int[] cnt = new int[n + 1];
for (int i = 0; i < n; i++) {
a[i] = io.nextInt();
if (a[i] <= n) {
cnt[a[i]]++;
}
}

TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i <= n; i++) {
if (cnt[i] == 0) {
set.add(i);
}
}

while (q-- != 0) {
int i = io.nextInt() - 1, x = io.nextInt();
if (a[i] <= n && --cnt[a[i]] == 0) {
set.add(a[i]);
}
a[i] = x;
if (a[i] <= n && cnt[a[i]]++ == 0) {
set.remove(a[i]);
}
io.println(set.first());
}
}