Skip to content

Commit 6d4f3ee

Browse files
committed
Added tasks 3674-3677
1 parent 8cdc57b commit 6d4f3ee

File tree

12 files changed

+344
-0
lines changed

12 files changed

+344
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package g3601_3700.s3674_minimum_operations_to_equalize_array;
2+
3+
// #Easy #Weekly_Contest_466 #2025_09_07_Time_1_ms_(100.00%)_Space_43.98_MB_(100.00%)
4+
5+
public class Solution {
6+
public int minOperations(int[] nums) {
7+
for (int num : nums) {
8+
if (num != nums[0]) return 1;
9+
}
10+
return 0;
11+
}
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3674\. Minimum Operations to Equalize Array
2+
3+
Easy
4+
5+
You are given an integer array `nums` of length `n`.
6+
7+
In one operation, choose any subarray `nums[l...r]` (`0 <= l <= r < n`) and **replace** each element in that subarray with the **bitwise AND** of all elements.
8+
9+
Return the **minimum** number of operations required to make all elements of `nums` equal.
10+
11+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2]
16+
17+
**Output:** 1
18+
19+
**Explanation:**
20+
21+
Choose `nums[0...1]`: `(1 AND 2) = 0`, so the array becomes `[0, 0]` and all elements are equal in 1 operation.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [5,5,5]
26+
27+
**Output:** 0
28+
29+
**Explanation:**
30+
31+
`nums` is `[5, 5, 5]` which already has all elements equal, so 0 operations are required.
32+
33+
**Constraints:**
34+
35+
* `1 <= n == nums.length <= 100`
36+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3601_3700.s3675_minimum_operations_to_transform_string;
2+
3+
// #Medium #Weekly_Contest_466 #2025_09_07_Time_137_ms_(100.00%)_Space_51.85_MB_(100.00%)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public int minOperations(String s) {
10+
Set<Character> set = new HashSet<>();
11+
for (char ch : s.toCharArray()) {
12+
set.add(ch);
13+
}
14+
if (set.size() == 1 && set.contains('a')) {
15+
return 0;
16+
}
17+
char minCh = 'z';
18+
for (char ch : s.toCharArray()) {
19+
if (ch != 'a' && ch < minCh) {
20+
minCh = ch;
21+
}
22+
}
23+
return ('z' - minCh) + 1;
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3675\. Minimum Operations to Transform String
2+
3+
Medium
4+
5+
You are given a string `s` consisting only of lowercase English letters.
6+
7+
You can perform the following operation any number of times (including zero):
8+
9+
* Choose any character `c` in the string and replace **every** occurrence of `c` with the **next** lowercase letter in the English alphabet.
10+
11+
12+
Return the **minimum** number of operations required to transform `s` into a string consisting of **only** `'a'` characters.
13+
14+
**Note:** Consider the alphabet as circular, thus `'a'` comes after `'z'`.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "yz"
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
* Change `'y'` to `'z'` to get `"zz"`.
25+
* Change `'z'` to `'a'` to get `"aa"`.
26+
* Thus, the answer is 2.
27+
28+
**Example 2:**
29+
30+
**Input:** s = "a"
31+
32+
**Output:** 0
33+
34+
**Explanation:**
35+
36+
* The string `"a"` only consists of `'a'` characters. Thus, the answer is 0.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= s.length <= 5 * 10<sup>5</sup></code>
41+
* `s` consists only of lowercase English letters.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3601_3700.s3676_count_bowl_subarrays;
2+
3+
// #Medium #Weekly_Contest_466 #2025_09_07_Time_19_ms_(100.00%)_Space_59.30_MB_(100.00%)
4+
5+
import java.util.ArrayDeque;
6+
import java.util.Deque;
7+
8+
public class Solution {
9+
public long bowlSubarrays(int[] nums) {
10+
int l = nums.length;
11+
int ans = 0;
12+
Deque<Integer> stack = new ArrayDeque<>();
13+
for (int i = 0; i < l; i++) {
14+
while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) {
15+
int mid = stack.pop();
16+
if (!stack.isEmpty()) {
17+
int left = stack.peek();
18+
if (Math.min(nums[left], nums[i]) > nums[mid]) ++ans;
19+
}
20+
}
21+
stack.push(i);
22+
}
23+
return ans;
24+
}
25+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3676\. Count Bowl Subarrays
2+
3+
Medium
4+
5+
You are given an integer array `nums` with **distinct** elements.
6+
7+
A subarray `nums[l...r]` of `nums` is called a **bowl** if:
8+
9+
* The subarray has length at least 3. That is, `r - l + 1 >= 3`.
10+
* The **minimum** of its two ends is **strictly greater** than the **maximum** of all elements in between. That is, `min(nums[l], nums[r]) > max(nums[l + 1], ..., nums[r - 1])`.
11+
12+
Return the number of **bowl** subarrays in `nums`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,5,3,1,4]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
The bowl subarrays are `[3, 1, 4]` and `[5, 3, 1, 4]`.
23+
24+
* `[3, 1, 4]` is a bowl because `min(3, 4) = 3 > max(1) = 1`.
25+
* `[5, 3, 1, 4]` is a bowl because `min(5, 4) = 4 > max(3, 1) = 3`.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [5,1,2,3,4]
30+
31+
**Output:** 3
32+
33+
**Explanation:**
34+
35+
The bowl subarrays are `[5, 1, 2]`, `[5, 1, 2, 3]` and `[5, 1, 2, 3, 4]`.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [1000000000,999999999,999999998]
40+
41+
**Output:** 0
42+
43+
**Explanation:**
44+
45+
No subarray is a bowl.
46+
47+
**Constraints:**
48+
49+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
51+
* `nums` consists of distinct elements.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3601_3700.s3677_count_binary_palindromic_numbers;
2+
3+
// #Hard #Weekly_Contest_466 #2025_09_07_Time_1_ms_(100.00%)_Space_40.86_MB_(100.00%)
4+
5+
public class Solution {
6+
private long makePalin(long left, boolean odd) {
7+
long ans = left;
8+
if (odd) left = left >> 1;
9+
while (left > 0) {
10+
ans = (ans << 1) | (left & 1);
11+
left = left >> 1;
12+
}
13+
return ans;
14+
}
15+
16+
public int countBinaryPalindromes(long n) {
17+
if (n == 0) return 1;
18+
int len = 64 - Long.numberOfLeadingZeros(n);
19+
long count = 1;
20+
for (int i = 1; i < len; i++) {
21+
int half = (i + 1) / 2;
22+
count += 1L << (half - 1);
23+
}
24+
int half = (len + 1) / 2;
25+
long prefix = n >> (len - half);
26+
long palin = makePalin(prefix, len % 2 == 1);
27+
count += (prefix - (1L << (half - 1)));
28+
if (palin <= n) ++count;
29+
return (int) count;
30+
}
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3677\. Count Binary Palindromic Numbers
2+
3+
Hard
4+
5+
You are given a **non-negative** integer `n`.
6+
7+
A **non-negative** integer is called **binary-palindromic** if its binary representation (written without leading zeros) reads the same forward and backward.
8+
9+
Return the number of integers `k` such that `0 <= k <= n` and the binary representation of `k` is a palindrome.
10+
11+
**Note:** The number 0 is considered binary-palindromic, and its representation is `"0"`.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 9
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
The integers `k` in the range `[0, 9]` whose binary representations are palindromes are:
22+
23+
* `0 → "0"`
24+
* `1 → "1"`
25+
* `3 → "11"`
26+
* `5 → "101"`
27+
* `7 → "111"`
28+
* `9 → "1001"`
29+
30+
All other values in `[0, 9]` have non-palindromic binary forms. Therefore, the count is 6.
31+
32+
**Example 2:**
33+
34+
**Input:** n = 0
35+
36+
**Output:** 1
37+
38+
**Explanation:**
39+
40+
Since `"0"` is a palindrome, the count is 1.
41+
42+
**Constraints:**
43+
44+
* <code>0 <= n <= 10<sup>15</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3601_3700.s3674_minimum_operations_to_equalize_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minOperations() {
11+
assertThat(new Solution().minOperations(new int[] {1, 2}), equalTo(1));
12+
}
13+
14+
@Test
15+
void minOperations2() {
16+
assertThat(new Solution().minOperations(new int[] {5, 5, 5}), equalTo(0));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3601_3700.s3675_minimum_operations_to_transform_string;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minOperations() {
11+
assertThat(new Solution().minOperations("yz"), equalTo(2));
12+
}
13+
14+
@Test
15+
void minOperations2() {
16+
assertThat(new Solution().minOperations("a"), equalTo(0));
17+
}
18+
}

0 commit comments

Comments
 (0)