Skip to content

Commit 6c33c0a

Browse files
authored
Added tasks 3751, 3752, 3753
1 parent a682b9f commit 6c33c0a

9 files changed

Lines changed: 387 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3701_3800.s3751_total_waviness_of_numbers_in_range_i;
2+
3+
// #Medium #Dynamic_Programming #Math #Enumeration #Senior #Biweekly_Contest_170
4+
// #2026_04_26_Time_11_ms_(98.18%)_Space_42.55_MB_(97.88%)
5+
6+
public class Solution {
7+
private int countpeakValley(int num) {
8+
int lastdigit = num % 10;
9+
int waiviness = 0;
10+
int prevcurrent = -1;
11+
num = num / 10;
12+
while (num > 0) {
13+
if (prevcurrent == -1) {
14+
prevcurrent = num % 10;
15+
num = num / 10;
16+
continue;
17+
}
18+
int currvalue = num % 10;
19+
if ((prevcurrent > currvalue && prevcurrent > lastdigit)
20+
|| (prevcurrent < currvalue && prevcurrent < lastdigit)) {
21+
waiviness++;
22+
}
23+
lastdigit = prevcurrent;
24+
prevcurrent = num % 10;
25+
num = num / 10;
26+
}
27+
return waiviness;
28+
}
29+
30+
public int totalWaviness(int num1, int num2) {
31+
int ans = 0;
32+
for (int i = num1; i <= num2; i++) {
33+
int l = countpeakValley(i);
34+
ans = ans + l;
35+
}
36+
return ans;
37+
}
38+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3751\. Total Waviness of Numbers in Range I
2+
3+
Medium
4+
5+
You are given two integers `num1` and `num2` representing an **inclusive** range `[num1, num2]`.
6+
7+
The **waviness** of a number is defined as the total count of its **peaks** and **valleys**:
8+
9+
* A digit is a **peak** if it is **strictly greater** than both of its immediate neighbors.
10+
* A digit is a **valley** if it is **strictly less** than both of its immediate neighbors.
11+
* The first and last digits of a number **cannot** be peaks or valleys.
12+
* Any number with fewer than 3 digits has a waviness of 0.
13+
14+
Return the total sum of waviness for all numbers in the range `[num1, num2]`.
15+
16+
**Example 1:**
17+
18+
**Input:** num1 = 120, num2 = 130
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
In the range `[120, 130]`:
25+
26+
* `120`: middle digit 2 is a peak, waviness = 1.
27+
* `121`: middle digit 2 is a peak, waviness = 1.
28+
* `130`: middle digit 3 is a peak, waviness = 1.
29+
* All other numbers in the range have a waviness of 0.
30+
31+
Thus, total waviness is `1 + 1 + 1 = 3`.
32+
33+
**Example 2:**
34+
35+
**Input:** num1 = 198, num2 = 202
36+
37+
**Output:** 3
38+
39+
**Explanation:**
40+
41+
In the range `[198, 202]`:
42+
43+
* `198`: middle digit 9 is a peak, waviness = 1.
44+
* `201`: middle digit 0 is a valley, waviness = 1.
45+
* `202`: middle digit 0 is a valley, waviness = 1.
46+
* All other numbers in the range have a waviness of 0.
47+
48+
Thus, total waviness is `1 + 1 + 1 = 3`.
49+
50+
**Example 3:**
51+
52+
**Input:** num1 = 4848, num2 = 4848
53+
54+
**Output:** 2
55+
56+
**Explanation:**
57+
58+
Number `4848`: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.
59+
60+
**Constraints:**
61+
62+
* <code>1 <= num1 <= num2 <= 10<sup>5</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3701_3800.s3752_lexicographically_smallest_negated_permutation_that_sums_to_target;
2+
3+
// #Medium #Array #Math #Sorting #Greedy #Two_Pointers #Staff #Biweekly_Contest_170
4+
// #2026_04_26_Time_3_ms_(100.00%)_Space_162.22_MB_(86.17%)
5+
6+
public class Solution {
7+
public int[] lexSmallestNegatedPerm(int n, long target) {
8+
long drop = (long) n * (n + 1) / 2 - target;
9+
if (drop < 0 || drop % 2 != 0) {
10+
return new int[] {};
11+
}
12+
int[] ans = new int[n];
13+
int l = 0;
14+
int r = n - 1;
15+
for (int i = n; i > 0; i--) {
16+
int val = i;
17+
if (2 * val <= drop) {
18+
drop -= 2 * val;
19+
ans[l++] = -val;
20+
} else {
21+
ans[r--] = val;
22+
}
23+
}
24+
if (drop != 0) {
25+
return new int[] {};
26+
}
27+
return ans;
28+
}
29+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3752\. Lexicographically Smallest Negated Permutation that Sums to Target
2+
3+
Medium
4+
5+
You are given a positive integer `n` and an integer `target`.
6+
7+
Return the **lexicographically smallest** array of integers of size `n` such that:
8+
9+
* The **sum** of its elements equals `target`.
10+
* The **absolute values** of its elements form a **permutation** of size `n`.
11+
12+
If no such array exists, return an empty array.
13+
14+
A **permutation** of size `n` is a rearrangement of integers `1, 2, ..., n`.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 3, target = 0
19+
20+
**Output:** [-3,1,2]
21+
22+
**Explanation:**
23+
24+
The arrays that sum to 0 and whose absolute values form a permutation of size 3 are:
25+
26+
* `[-3, 1, 2]`
27+
* `[-3, 2, 1]`
28+
* `[-2, -1, 3]`
29+
* `[-2, 3, -1]`
30+
* `[-1, -2, 3]`
31+
* `[-1, 3, -2]`
32+
* `[1, -3, 2]`
33+
* `[1, 2, -3]`
34+
* `[2, -3, 1]`
35+
* `[2, 1, -3]`
36+
* `[3, -2, -1]`
37+
* `[3, -1, -2]`
38+
39+
The lexicographically smallest one is `[-3, 1, 2]`.
40+
41+
**Example 2:**
42+
43+
**Input:** n = 1, target = 10000000000
44+
45+
**Output:** []
46+
47+
**Explanation:**
48+
49+
There are no arrays that sum to 10000000000 and whose absolute values form a permutation of size 1. Therefore, the answer is `[]`.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= n <= 10<sup>5</sup></code>
54+
* <code>-10<sup>10</sup> <= target <= 10<sup>10</sup></code>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package g3701_3800.s3753_total_waviness_of_numbers_in_range_ii;
2+
3+
// #Hard #Dynamic_Programming #Math #Senior_Staff #Biweekly_Contest_170
4+
// #2026_04_26_Time_52_ms_(96.83%)_Space_46.46_MB_(98.41%)
5+
6+
public class Solution {
7+
private static class Pair {
8+
long count;
9+
long sum;
10+
11+
Pair(long count, long sum) {
12+
this.count = count;
13+
this.sum = sum;
14+
}
15+
}
16+
17+
private char[] digits;
18+
private Pair[][][][][] memo;
19+
private boolean[][][][][] seen;
20+
21+
public long totalWaviness(long num1, long num2) {
22+
return solve(num2) - solve(num1 - 1);
23+
}
24+
25+
private long solve(long x) {
26+
if (x <= 0) {
27+
return 0;
28+
}
29+
digits = Long.toString(x).toCharArray();
30+
int n = digits.length;
31+
memo = new Pair[n][2][2][11][11];
32+
seen = new boolean[n][2][2][11][11];
33+
return dfs(0, 1, 0, 10, 10).sum;
34+
}
35+
36+
private Pair dfs(int pos, int tight, int started, int prev2, int prev1) {
37+
if (pos == digits.length) {
38+
return new Pair(1, 0);
39+
}
40+
if (seen[pos][tight][started][prev2][prev1]) {
41+
return memo[pos][tight][started][prev2][prev1];
42+
}
43+
seen[pos][tight][started][prev2][prev1] = true;
44+
int limit = tight == 1 ? digits[pos] - '0' : 9;
45+
long totalCount = 0;
46+
long totalSum = 0;
47+
for (int d = 0; d <= limit; d++) {
48+
int nextTight = (tight == 1 && d == limit) ? 1 : 0;
49+
if (started == 0 && d == 0) {
50+
// still leading zeros, number not started
51+
Pair nxt = dfs(pos + 1, nextTight, 0, 10, 10);
52+
totalCount += nxt.count;
53+
totalSum += nxt.sum;
54+
} else if (started == 0) {
55+
// first real digit
56+
Pair nxt = dfs(pos + 1, nextTight, 1, 10, d);
57+
totalCount += nxt.count;
58+
totalSum += nxt.sum;
59+
} else if (prev2 == 10) {
60+
// second real digit
61+
Pair nxt = dfs(pos + 1, nextTight, 1, prev1, d);
62+
totalCount += nxt.count;
63+
totalSum += nxt.sum;
64+
} else {
65+
// now prev1 is an interior candidate, decide if it is peak/valley
66+
int add = 0;
67+
if ((prev1 > prev2 && prev1 > d) || (prev1 < prev2 && prev1 < d)) {
68+
add = 1;
69+
}
70+
Pair nxt = dfs(pos + 1, nextTight, 1, prev1, d);
71+
totalCount += nxt.count;
72+
totalSum += nxt.sum + add * nxt.count;
73+
}
74+
}
75+
memo[pos][tight][started][prev2][prev1] = new Pair(totalCount, totalSum);
76+
return memo[pos][tight][started][prev2][prev1];
77+
}
78+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3753\. Total Waviness of Numbers in Range II
2+
3+
Hard
4+
5+
You are given two integers `num1` and `num2` representing an **inclusive** range `[num1, num2]`.
6+
7+
The **waviness** of a number is defined as the total count of its **peaks** and **valleys**:
8+
9+
* A digit is a **peak** if it is **strictly greater** than both of its immediate neighbors.
10+
* A digit is a **valley** if it is **strictly less** than both of its immediate neighbors.
11+
* The first and last digits of a number **cannot** be peaks or valleys.
12+
* Any number with fewer than 3 digits has a waviness of 0.
13+
14+
Return the total sum of waviness for all numbers in the range `[num1, num2]`.
15+
16+
**Example 1:**
17+
18+
**Input:** num1 = 120, num2 = 130
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
In the range `[120, 130]`:
25+
26+
* `120`: middle digit 2 is a peak, waviness = 1.
27+
* `121`: middle digit 2 is a peak, waviness = 1.
28+
* `130`: middle digit 3 is a peak, waviness = 1.
29+
* All other numbers in the range have a waviness of 0.
30+
31+
Thus, total waviness is `1 + 1 + 1 = 3`.
32+
33+
**Example 2:**
34+
35+
**Input:** num1 = 198, num2 = 202
36+
37+
**Output:** 3
38+
39+
**Explanation:**
40+
41+
In the range `[198, 202]`:
42+
43+
* `198`: middle digit 9 is a peak, waviness = 1.
44+
* `201`: middle digit 0 is a valley, waviness = 1.
45+
* `202`: middle digit 0 is a valley, waviness = 1.
46+
* All other numbers in the range have a waviness of 0.
47+
48+
Thus, total waviness is `1 + 1 + 1 = 3`.
49+
50+
**Example 3:**
51+
52+
**Input:** num1 = 4848, num2 = 4848
53+
54+
**Output:** 2
55+
56+
**Explanation:**
57+
58+
Number `4848`: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.
59+
60+
**Constraints:**
61+
62+
* <code>1 <= num1 <= num2 <= 10<sup>15</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3701_3800.s3751_total_waviness_of_numbers_in_range_i;
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 totalWaviness() {
11+
assertThat(new Solution().totalWaviness(120, 130), equalTo(3));
12+
}
13+
14+
@Test
15+
void totalWaviness2() {
16+
assertThat(new Solution().totalWaviness(198, 202), equalTo(3));
17+
}
18+
19+
@Test
20+
void totalWaviness3() {
21+
assertThat(new Solution().totalWaviness(4848, 4848), equalTo(2));
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3701_3800.s3752_lexicographically_smallest_negated_permutation_that_sums_to_target;
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 lexSmallestNegatedPerm() {
11+
assertThat(new Solution().lexSmallestNegatedPerm(3, 0L), equalTo(new int[] {-3, 1, 2}));
12+
}
13+
14+
@Test
15+
void lexSmallestNegatedPerm2() {
16+
assertThat(new Solution().lexSmallestNegatedPerm(3, 10000000000L), equalTo(new int[] {}));
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3701_3800.s3753_total_waviness_of_numbers_in_range_ii;
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 totalWaviness() {
11+
assertThat(new Solution().totalWaviness(120L, 130L), equalTo(3L));
12+
}
13+
14+
@Test
15+
void totalWaviness2() {
16+
assertThat(new Solution().totalWaviness(198L, 202L), equalTo(3L));
17+
}
18+
19+
@Test
20+
void totalWaviness3() {
21+
assertThat(new Solution().totalWaviness(4848L, 4848L), equalTo(2L));
22+
}
23+
}

0 commit comments

Comments
 (0)