Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76b3609

Browse files
committed
第466场周赛T1~T4 (4)
1 parent f665919 commit 76b3609

File tree

8 files changed

+277
-0
lines changed

8 files changed

+277
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution3674 {
2+
public int minOperations(int[] nums) {
3+
for (int i = 1; i < nums.length; i++) {
4+
if (nums[i - 1] != nums[i]) return 1;
5+
}
6+
return 0;
7+
}
8+
}
9+
/*
10+
3674. 数组元素相等的最小操作次数
11+
https://leetcode.cn/problems/minimum-operations-to-equalize-array/description/
12+
13+
第 466 场周赛 T1。
14+
15+
给你一个长度为 n 的整数数组 nums。
16+
在一次操作中,可以选择任意子数组 nums[l...r] (0 <= l <= r < n),并将该子数组中的每个元素 替换 为所有元素的 按位与(bitwise AND)结果。
17+
返回使数组 nums 中所有元素相等所需的最小操作次数。
18+
子数组 是数组中连续的、非空的元素序列。
19+
提示:
20+
1 <= n == nums.length <= 100
21+
1 <= nums[i] <= 10^5
22+
23+
脑筋急转弯。
24+
如果 nums 所有元素都相等,无需操作,返回 0。否则返回 1。
25+
时间复杂度 O(n)。
26+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Solution3675 {
2+
public int minOperations(String s) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
cnt[c - 'a']++;
6+
}
7+
8+
for (int i = 1; i < 26; i++) {
9+
if (cnt[i] > 0) return 26 - i;
10+
}
11+
return 0;
12+
}
13+
}
14+
/*
15+
3675. 转换字符串的最小操作次数
16+
https://leetcode.cn/problems/minimum-operations-to-transform-string/description/
17+
18+
第 466 场周赛 T2。
19+
20+
给你一个仅由小写英文字母组成的字符串 s。
21+
你可以执行以下操作任意次(包括零次):
22+
- 选择字符串中出现的一个字符 c,并将 每个 出现的 c 替换为英文字母表中 下一个 小写字母。
23+
返回将 s 转换为仅由 'a' 组成的字符串所需的最小操作次数。
24+
注意:字母表是循环的,因此 'z' 的下一个字母是 'a'。
25+
提示:
26+
1 <= s.length <= 5 * 10^5
27+
s 仅由小写英文字母组成。
28+
29+
统计出现过的字符。答案就是最小的非 a 字母到 z+1 的距离。
30+
时间复杂度 O(n)。
31+
*/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.ArrayDeque;
2+
import java.util.Deque;
3+
4+
public class Solution3676 {
5+
public long bowlSubarrays(int[] nums) {
6+
int n = nums.length;
7+
int[] left = new int[n];
8+
int[] right = new int[n];
9+
Deque<Integer> st = new ArrayDeque<>();
10+
11+
for (int i = 0; i < n; i++) {
12+
while (!st.isEmpty() && nums[st.peek()] <= nums[i]) {
13+
st.pop();
14+
}
15+
left[i] = st.isEmpty() ? -1 : st.peek();
16+
st.push(i);
17+
}
18+
19+
st.clear();
20+
for (int i = n - 1; i >= 0; i--) {
21+
while (!st.isEmpty() && nums[st.peek()] <= nums[i]) {
22+
st.pop();
23+
}
24+
right[i] = st.isEmpty() ? n : st.peek();
25+
st.push(i);
26+
}
27+
28+
long ans = 0;
29+
for (int i = 0; i < n; i++) {
30+
if (left[i] != -1 && right[i] != n) {
31+
ans++;
32+
}
33+
}
34+
return ans;
35+
}
36+
}
37+
/*
38+
3676. 碗子数组的数目
39+
https://leetcode.cn/problems/count-bowl-subarrays/description/
40+
41+
第 466 场周赛 T3。
42+
43+
给你一个整数数组 nums,包含 互不相同 的元素。
44+
nums 的一个子数组 nums[l...r] 被称为 碗(bowl),如果它满足以下条件:
45+
- 子数组的长度至少为 3。也就是说,r - l + 1 >= 3。
46+
- 其两端元素的 最小值 严格大于 中间所有元素的 最大值。也就是说,min(nums[l], nums[r]) > max(nums[l + 1], ..., nums[r - 1])。
47+
返回 nums 中 碗 子数组的数量。
48+
子数组 是数组中连续的元素序列。
49+
提示:
50+
3 <= nums.length <= 10^5
51+
1 <= nums[i] <= 10^9
52+
nums 由不同的元素组成。
53+
54+
单调栈。统计合法下标。返回值 long 是迷惑性质。
55+
时间复杂度 O(n)。
56+
https://yuanbao.tencent.com/chat/naQivTmsDa/0f153432-97c4-4f12-972f-bae0f4f6d805
57+
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
public class Solution3677 {
2+
public int countBinaryPalindromes(long n) {
3+
if (n == 0) {
4+
return 1;
5+
}
6+
String binaryStr = Long.toBinaryString(n);
7+
int totalLen = binaryStr.length();
8+
long count = 1;
9+
10+
for (int L = 1; L < totalLen; L++) {
11+
int halfLen = (L + 1) / 2;
12+
count += (1L << (halfLen - 1));
13+
}
14+
15+
int halfLen = (totalLen + 1) / 2;
16+
long low = 1L << (halfLen - 1);
17+
long high = (1L << halfLen) - 1;
18+
19+
long left = low, right = high;
20+
long lastValid = low - 1;
21+
while (left <= right) {
22+
long mid = left + (right - left) / 2;
23+
long palin = constructPalindrome(mid, totalLen);
24+
if (palin <= n) {
25+
lastValid = mid;
26+
left = mid + 1;
27+
} else {
28+
right = mid - 1;
29+
}
30+
}
31+
if (lastValid >= low) {
32+
count += (lastValid - low + 1);
33+
}
34+
return (int) count;
35+
}
36+
37+
private long constructPalindrome(long p, int len) {
38+
int halfLen = (len + 1) / 2;
39+
if (len % 2 == 0) {
40+
long reversed = reverseBits(p, halfLen);
41+
return (p << halfLen) | reversed;
42+
} else {
43+
long reversed = reverseBits(p >> 1, halfLen - 1);
44+
return (p << (halfLen - 1)) | reversed;
45+
}
46+
}
47+
48+
private long reverseBits(long num, int bits) {
49+
long rev = 0;
50+
for (int i = 0; i < bits; i++) {
51+
rev = (rev << 1) | (num & 1);
52+
num >>= 1;
53+
}
54+
return rev;
55+
}
56+
}
57+
/*
58+
3677. 统计二进制回文数字的数目
59+
https://leetcode.cn/problems/count-binary-palindromic-numbers/description/
60+
61+
第 466 场周赛 T4。
62+
63+
给你一个 非负 整数 n。
64+
如果一个 非负 整数的二进制表示(不含前导零)正着读和倒着读都一样,则称该数为 二进制回文数。
65+
返回满足 0 <= k <= n 且 k 的二进制表示是回文数的整数 k 的数量。
66+
注意: 数字 0 被认为是二进制回文数,其表示为 "0"。
67+
提示:
68+
0 <= n <= 10^15
69+
70+
方法思路
71+
1.特殊情况处理:如果n为0,直接返回1,因为0是二进制回文数。
72+
2.计算二进制长度:将n转换为二进制字符串,并计算其长度len。
73+
3.统计较短长度的回文数:对于每个长度L从1到len-1,计算该长度的二进制回文数的数量。这些回文数的数量由前半部分的位数决定,即2^(halfLen-1),其中halfLen是(L+1)/2。
74+
4.统计相同长度的回文数:对于长度为len的二进制回文数,使用二分查找来高效地确定满足条件的回文数数量,而不是逐个生成和检查。
75+
https://chat.deepseek.com/a/chat/s/f16c1854-1de7-4750-b0c3-19b280f76ae2
76+
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3674Tests {
5+
private final Solution3674 solution3674 = new Solution3674();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {1, 2};
10+
int expected = 1;
11+
Assertions.assertEquals(expected, solution3674.minOperations(nums));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] nums = {5, 5, 5};
17+
int expected = 0;
18+
Assertions.assertEquals(expected, solution3674.minOperations(nums));
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3675Tests {
5+
private final Solution3675 solution3675 = new Solution3675();
6+
7+
@Test
8+
public void example1() {
9+
String s = "yz";
10+
int expected = 2;
11+
Assertions.assertEquals(expected, solution3675.minOperations(s));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
String s = "a";
17+
int expected = 0;
18+
Assertions.assertEquals(expected, solution3675.minOperations(s));
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3676Tests {
5+
private final Solution3676 solution3676 = new Solution3676();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {2, 5, 3, 1, 4};
10+
long expected = 2;
11+
Assertions.assertEquals(expected, solution3676.bowlSubarrays(nums));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] nums = {5, 1, 2, 3, 4};
17+
long expected = 3;
18+
Assertions.assertEquals(expected, solution3676.bowlSubarrays(nums));
19+
}
20+
21+
@Test
22+
public void example3() {
23+
int[] nums = {1000000000, 999999999, 999999998};
24+
long expected = 0;
25+
Assertions.assertEquals(expected, solution3676.bowlSubarrays(nums));
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3677Tests {
5+
private final Solution3677 solution3677 = new Solution3677();
6+
7+
@Test
8+
public void example1() {
9+
long n = 9;
10+
int expected = 6;
11+
Assertions.assertEquals(expected, solution3677.countBinaryPalindromes(n));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
long n = 0;
17+
int expected = 1;
18+
Assertions.assertEquals(expected, solution3677.countBinaryPalindromes(n));
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /