Skip to content

Commit 85b6f0e

Browse files
committed
Improved tasks 3675, 3676
1 parent 51caed7 commit 85b6f0e

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package g3601_3700.s3675_minimum_operations_to_transform_string;
22

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;
3+
// #Medium #Weekly_Contest_466 #2025_09_14_Time_5_ms_(100.00%)_Space_47.93_MB_(95.06%)
74

85
public class Solution {
96
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;
7+
int n = s.length();
8+
int ans = 0;
9+
for (int i = 0; i < n; i++) {
10+
final char c = s.charAt(i);
11+
if (c != 'a') {
12+
int ops = 'z' - c + 1;
13+
if (ops > ans) {
14+
ans = ops;
15+
}
16+
if (ops == 25) {
17+
break;
18+
}
2119
}
2220
}
23-
return ('z' - minCh) + 1;
21+
return ans;
2422
}
2523
}
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package g3601_3700.s3676_count_bowl_subarrays;
22

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;
3+
// #Medium #Weekly_Contest_466 #2025_09_14_Time_2_ms_(100.00%)_Space_58.91_MB_(69.85%)
74

85
public class Solution {
96
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]) {
19-
++ans;
20-
}
21-
}
7+
int n = nums.length;
8+
int res = n;
9+
int pre = 0;
10+
for (int a : nums) {
11+
if (a > pre) {
12+
res--;
13+
pre = a;
14+
}
15+
}
16+
pre = 0;
17+
for (int i = n - 1; i >= 0; i--) {
18+
if (nums[i] > pre) {
19+
res--;
20+
pre = nums[i];
2221
}
23-
stack.push(i);
2422
}
25-
return ans;
23+
return res + 1;
2624
}
2725
}

0 commit comments

Comments
 (0)