Skip to content

Commit 7cf0adb

Browse files
authored
Added tasks 2843-2848
1 parent 83b7c21 commit 7cf0adb

File tree

15 files changed

+607
-0
lines changed

15 files changed

+607
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2801_2900.s2843_count_symmetric_integers;
2+
3+
// #Easy #Math #Enumeration #2023_12_13_Time_26_ms_(80.87%)_Space_43.9_MB_(8.49%)
4+
5+
public class Solution {
6+
public int countSymmetricIntegers(int low, int high) {
7+
int count = 0;
8+
for (int i = low; i <= high; i++) {
9+
if (isSymmetric(i)) {
10+
count++;
11+
}
12+
}
13+
return count;
14+
}
15+
16+
private boolean isSymmetric(int num) {
17+
String str = String.valueOf(num);
18+
int n = str.length();
19+
if (n % 2 != 0) {
20+
return false;
21+
}
22+
int leftSum = 0;
23+
int rightSum = 0;
24+
for (int i = 0, j = n - 1; i < j; i++, j--) {
25+
leftSum += str.charAt(i) - '0';
26+
rightSum += str.charAt(j) - '0';
27+
}
28+
return leftSum == rightSum;
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2843\. Count Symmetric Integers
2+
3+
Easy
4+
5+
You are given two positive integers `low` and `high`.
6+
7+
An integer `x` consisting of `2 * n` digits is **symmetric** if the sum of the first `n` digits of `x` is equal to the sum of the last `n` digits of `x`. Numbers with an odd number of digits are never symmetric.
8+
9+
Return _the **number of symmetric** integers in the range_ `[low, high]`.
10+
11+
**Example 1:**
12+
13+
**Input:** low = 1, high = 100
14+
15+
**Output:** 9
16+
17+
**Explanation:** There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
18+
19+
**Example 2:**
20+
21+
**Input:** low = 1200, high = 1230
22+
23+
**Output:** 4
24+
25+
**Explanation:** There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= low <= high <= 10<sup>4</sup></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g2801_2900.s2844_minimum_operations_to_make_a_special_number;
2+
3+
// #Medium #String #Math #Greedy #Enumeration #2023_12_13_Time_1_ms_(100.00%)_Space_41_MB_(68.09%)
4+
5+
public class Solution {
6+
public int minimumOperations(String num) {
7+
char[] number = num.toCharArray();
8+
int n = number.length;
9+
int zero = 0;
10+
int five = 0;
11+
for (int i = n - 1; i >= 0; i--) {
12+
if (number[i] == '0') {
13+
if (zero == 1) {
14+
return n - i - 2;
15+
} else {
16+
zero++;
17+
}
18+
} else if (number[i] == '5') {
19+
if (zero == 1) {
20+
return n - i - 2;
21+
}
22+
if (five == 0) {
23+
five++;
24+
}
25+
} else if ((number[i] == '2' || number[i] == '7') && five == 1) {
26+
return n - i - 2;
27+
}
28+
}
29+
if (zero == 1) {
30+
return n - 1;
31+
}
32+
return n;
33+
}
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2844\. Minimum Operations to Make a Special Number
2+
3+
Medium
4+
5+
You are given a **0-indexed** string `num` representing a non-negative integer.
6+
7+
In one operation, you can pick any digit of `num` and delete it. Note that if you delete all the digits of `num`, `num` becomes `0`.
8+
9+
Return _the **minimum number of operations** required to make_ `num` _special_.
10+
11+
An integer `x` is considered **special** if it is divisible by `25`.
12+
13+
**Example 1:**
14+
15+
**Input:** num = "2245047"
16+
17+
**Output:** 2
18+
19+
**Explanation:** Delete digits num[5] and num[6]. The resulting number is "22450" which is special since it is divisible by 25. It can be shown that 2 is the minimum number of operations required to get a special number.
20+
21+
**Example 2:**
22+
23+
**Input:** num = "2908305"
24+
25+
**Output:** 3
26+
27+
**Explanation:** Delete digits num[3], num[4], and num[6]. The resulting number is "2900" which is special since it is divisible by 25. It can be shown that 3 is the minimum number of operations required to get a special number.
28+
29+
**Example 3:**
30+
31+
**Input:** num = "10"
32+
33+
**Output:** 1
34+
35+
**Explanation:** Delete digit num[0]. The resulting number is "0" which is special since it is divisible by 25. It can be shown that 1 is the minimum number of operations required to get a special number.
36+
37+
**Constraints:**
38+
39+
* `1 <= num.length <= 100`
40+
* `num` only consists of digits `'0'` through `'9'`.
41+
* `num` does not contain any leading zeros.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2801_2900.s2845_count_of_interesting_subarrays;
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #2023_12_13_Time_27_ms_(97.76%)_Space_62.7_MB_(26.12%)
4+
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class Solution {
10+
public long countInterestingSubarrays(List<Integer> nums, int modulo, int k) {
11+
int prefixCnt = 0;
12+
Map<Integer, Integer> freq = new HashMap<>();
13+
freq.put(0, 1);
14+
long interestingSubarrays = 0;
15+
for (Integer num : nums) {
16+
if (num % modulo == k) {
17+
prefixCnt++;
18+
}
19+
int expectedPrefix = (prefixCnt - k + modulo) % modulo;
20+
interestingSubarrays += freq.getOrDefault(expectedPrefix, 0);
21+
freq.put(prefixCnt % modulo, freq.getOrDefault(prefixCnt % modulo, 0) + 1);
22+
}
23+
return interestingSubarrays;
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2845\. Count of Interesting Subarrays
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums`, an integer `modulo`, and an integer `k`.
6+
7+
Your task is to find the count of subarrays that are **interesting**.
8+
9+
A **subarray** `nums[l..r]` is **interesting** if the following condition holds:
10+
11+
* Let `cnt` be the number of indices `i` in the range `[l, r]` such that `nums[i] % modulo == k`. Then, `cnt % modulo == k`.
12+
13+
Return _an integer denoting the count of interesting subarrays._
14+
15+
**Note:** A subarray is _a contiguous non-empty sequence of elements within an array_.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [3,2,4], modulo = 2, k = 1
20+
21+
**Output:** 3
22+
23+
**Explanation:** In this example the interesting subarrays are: The subarray nums[0..0] which is [3].
24+
- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
25+
- Hence, cnt = 1 and cnt % modulo == k. The subarray nums[0..1] which is [3,2].
26+
- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
27+
- Hence, cnt = 1 and cnt % modulo == k. The subarray nums[0..2] which is [3,2,4].
28+
- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
29+
- Hence, cnt = 1 and cnt % modulo == k.
30+
31+
It can be shown that there are no other interesting subarrays. So, the answer is 3.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [3,1,9,6], modulo = 3, k = 0
36+
37+
**Output:** 2
38+
39+
**Explanation:** In this example the interesting subarrays are: The subarray nums[0..3] which is [3,1,9,6].
40+
- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
41+
- Hence, cnt = 3 and cnt % modulo == k. The subarray nums[1..1] which is [1].
42+
- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
43+
- Hence, cnt = 0 and cnt % modulo == k.
44+
45+
It can be shown that there are no other interesting subarrays. So, the answer is 2.
46+
47+
**Constraints:**
48+
49+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
51+
* <code>1 <= modulo <= 10<sup>9</sup></code>
52+
* `0 <= k < modulo`
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package g2801_2900.s2846_minimum_edge_weight_equilibrium_queries_in_a_tree;
2+
3+
// #Hard #Array #Tree #Graph #Strongly_Connected_Component
4+
// #2023_12_13_Time_74_ms_(66.04%)_Space_57_MB_(50.94%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
@SuppressWarnings("java:S107")
11+
public class Solution {
12+
private static class Node {
13+
int v;
14+
int w;
15+
16+
Node(int v, int w) {
17+
this.v = v;
18+
this.w = w;
19+
}
20+
}
21+
22+
public int[] minOperationsQueries(int n, int[][] edges, int[][] queries) {
23+
List<List<Node>> graph = createGraph(edges, n);
24+
int queryCount = queries.length;
25+
int[] res = new int[queryCount];
26+
int[] parent = new int[n];
27+
int[] level = new int[n];
28+
int[][] weightFreq = new int[n][27];
29+
int[] freq = new int[27];
30+
int height = (int) (Math.log(n) / Math.log(2)) + 1;
31+
int[][] up = new int[n][height];
32+
for (int[] arr : up) {
33+
Arrays.fill(arr, -1);
34+
}
35+
dfs(graph, 0, 0, -1, parent, level, weightFreq, freq);
36+
for (int i = 0; i < n; i++) {
37+
up[i][0] = parent[i];
38+
}
39+
for (int i = 1; i < height; i++) {
40+
for (int j = 0; j < n; j++) {
41+
if (up[j][i - 1] == -1) {
42+
up[j][i] = -1;
43+
continue;
44+
}
45+
up[j][i] = up[up[j][i - 1]][i - 1];
46+
}
47+
}
48+
for (int i = 0; i < queryCount; i++) {
49+
int src = queries[i][0];
50+
int dest = queries[i][1];
51+
int lcaNode = lca(src, dest, up, height, level);
52+
res[i] = processResult(weightFreq[src], weightFreq[dest], weightFreq[lcaNode]);
53+
}
54+
return res;
55+
}
56+
57+
private int lca(int src, int dest, int[][] up, int height, int[] level) {
58+
int curr1 = src;
59+
int curr2 = dest;
60+
int minlevel;
61+
if (level[curr1] > level[curr2]) {
62+
minlevel = level[curr2];
63+
curr1 = getKthAncestor(curr1, level[curr1] - level[curr2], up, height);
64+
} else if (level[curr1] <= level[curr2]) {
65+
minlevel = level[curr1];
66+
curr2 = getKthAncestor(curr2, level[curr2] - level[curr1], up, height);
67+
} else {
68+
minlevel = level[curr1];
69+
}
70+
if (curr1 == curr2) {
71+
return curr1;
72+
}
73+
int l = 0;
74+
int h = level[curr2];
75+
while (l <= h) {
76+
int mid = l + (h - l) / 2;
77+
int p1 = getKthAncestor(curr1, minlevel - mid, up, height);
78+
int p2 = getKthAncestor(curr2, minlevel - mid, up, height);
79+
if (p1 == p2) {
80+
l = mid + 1;
81+
} else {
82+
h = mid - 1;
83+
}
84+
}
85+
return getKthAncestor(curr1, minlevel - l + 1, up, height);
86+
}
87+
88+
private int getKthAncestor(int node, int k, int[][] up, int height) {
89+
int curr = node;
90+
for (int i = 0; i < height && k >> i != 0; i++) {
91+
if (((1 << i) & k) != 0) {
92+
if (curr == -1) {
93+
return -1;
94+
}
95+
curr = up[curr][i];
96+
}
97+
}
98+
return curr;
99+
}
100+
101+
private int processResult(int[] freqSrc, int[] freqDest, int[] freqLCA) {
102+
int[] freqPath = new int[27];
103+
for (int i = 1; i < 27; i++) {
104+
freqPath[i] = freqSrc[i] + freqDest[i] - 2 * freqLCA[i];
105+
}
106+
int max = 0;
107+
int pathlen = 0;
108+
for (int i = 1; i < 27; i++) {
109+
max = Math.max(max, freqPath[i]);
110+
pathlen += freqPath[i];
111+
}
112+
return pathlen - max;
113+
}
114+
115+
private void dfs(
116+
List<List<Node>> graph,
117+
int src,
118+
int currlevel,
119+
int p,
120+
int[] parent,
121+
int[] level,
122+
int[][] weightFreq,
123+
int[] freq) {
124+
parent[src] = p;
125+
level[src] = currlevel;
126+
System.arraycopy(freq, 0, weightFreq[src], 0, freq.length);
127+
for (Node node : graph.get(src)) {
128+
int v = node.v;
129+
int w = node.w;
130+
if (v != p) {
131+
freq[w]++;
132+
dfs(graph, v, currlevel + 1, src, parent, level, weightFreq, freq);
133+
freq[w]--;
134+
}
135+
}
136+
}
137+
138+
private List<List<Node>> createGraph(int[][] edges, int n) {
139+
List<List<Node>> graph = new ArrayList<>();
140+
for (int i = 0; i < n; i++) {
141+
graph.add(new ArrayList<>());
142+
}
143+
for (int[] edge : edges) {
144+
int u = edge[0];
145+
int v = edge[1];
146+
int w = edge[2];
147+
graph.get(u).add(new Node(v, w));
148+
graph.get(v).add(new Node(u, w));
149+
}
150+
return graph;
151+
}
152+
}

0 commit comments

Comments
 (0)