diff --git a/src/main/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/Solution.java b/src/main/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/Solution.java new file mode 100644 index 000000000..0771e9db9 --- /dev/null +++ b/src/main/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/Solution.java @@ -0,0 +1,38 @@ +package g3701_3800.s3751_total_waviness_of_numbers_in_range_i; + +// #Medium #Dynamic_Programming #Math #Enumeration #Senior #Biweekly_Contest_170 +// #2026_04_26_Time_11_ms_(98.18%)_Space_42.55_MB_(97.88%) + +public class Solution { + private int countpeakValley(int num) { + int lastdigit = num % 10; + int waiviness = 0; + int prevcurrent = -1; + num = num / 10; + while (num > 0) { + if (prevcurrent == -1) { + prevcurrent = num % 10; + num = num / 10; + continue; + } + int currvalue = num % 10; + if ((prevcurrent > currvalue && prevcurrent > lastdigit) + || (prevcurrent < currvalue && prevcurrent < lastdigit)) { + waiviness++; + } + lastdigit = prevcurrent; + prevcurrent = num % 10; + num = num / 10; + } + return waiviness; + } + + public int totalWaviness(int num1, int num2) { + int ans = 0; + for (int i = num1; i <= num2; i++) { + int l = countpeakValley(i); + ans = ans + l; + } + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/readme.md b/src/main/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/readme.md new file mode 100644 index 000000000..df45379fa --- /dev/null +++ b/src/main/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/readme.md @@ -0,0 +1,62 @@ +3751\. Total Waviness of Numbers in Range I + +Medium + +You are given two integers `num1` and `num2` representing an **inclusive** range `[num1, num2]`. + +The **waviness** of a number is defined as the total count of its **peaks** and **valleys**: + +* A digit is a **peak** if it is **strictly greater** than both of its immediate neighbors. +* A digit is a **valley** if it is **strictly less** than both of its immediate neighbors. +* The first and last digits of a number **cannot** be peaks or valleys. +* Any number with fewer than 3 digits has a waviness of 0. + +Return the total sum of waviness for all numbers in the range `[num1, num2]`. + +**Example 1:** + +**Input:** num1 = 120, num2 = 130 + +**Output:** 3 + +**Explanation:** + +In the range `[120, 130]`: + +* `120`: middle digit 2 is a peak, waviness = 1. +* `121`: middle digit 2 is a peak, waviness = 1. +* `130`: middle digit 3 is a peak, waviness = 1. +* All other numbers in the range have a waviness of 0. + +Thus, total waviness is `1 + 1 + 1 = 3`. + +**Example 2:** + +**Input:** num1 = 198, num2 = 202 + +**Output:** 3 + +**Explanation:** + +In the range `[198, 202]`: + +* `198`: middle digit 9 is a peak, waviness = 1. +* `201`: middle digit 0 is a valley, waviness = 1. +* `202`: middle digit 0 is a valley, waviness = 1. +* All other numbers in the range have a waviness of 0. + +Thus, total waviness is `1 + 1 + 1 = 3`. + +**Example 3:** + +**Input:** num1 = 4848, num2 = 4848 + +**Output:** 2 + +**Explanation:** + +Number `4848`: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2. + +**Constraints:** + +* 1 <= num1 <= num2 <= 105 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/Solution.java b/src/main/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/Solution.java new file mode 100644 index 000000000..6c9f7afe1 --- /dev/null +++ b/src/main/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/Solution.java @@ -0,0 +1,29 @@ +package g3701_3800.s3752_lexicographically_smallest_negated_permutation_that_sums_to_target; + +// #Medium #Array #Math #Sorting #Greedy #Two_Pointers #Staff #Biweekly_Contest_170 +// #2026_04_26_Time_3_ms_(100.00%)_Space_162.22_MB_(86.17%) + +public class Solution { + public int[] lexSmallestNegatedPerm(int n, long target) { + long drop = (long) n * (n + 1) / 2 - target; + if (drop < 0 || drop % 2 != 0) { + return new int[] {}; + } + int[] ans = new int[n]; + int l = 0; + int r = n - 1; + for (int i = n; i > 0; i--) { + int val = i; + if (2 * val <= drop) { + drop -= 2 * val; + ans[l++] = -val; + } else { + ans[r--] = val; + } + } + if (drop != 0) { + return new int[] {}; + } + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/readme.md b/src/main/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/readme.md new file mode 100644 index 000000000..7c381da51 --- /dev/null +++ b/src/main/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/readme.md @@ -0,0 +1,54 @@ +3752\. Lexicographically Smallest Negated Permutation that Sums to Target + +Medium + +You are given a positive integer `n` and an integer `target`. + +Return the **lexicographically smallest** array of integers of size `n` such that: + +* The **sum** of its elements equals `target`. +* The **absolute values** of its elements form a **permutation** of size `n`. + +If no such array exists, return an empty array. + +A **permutation** of size `n` is a rearrangement of integers `1, 2, ..., n`. + +**Example 1:** + +**Input:** n = 3, target = 0 + +**Output:** [-3,1,2] + +**Explanation:** + +The arrays that sum to 0 and whose absolute values form a permutation of size 3 are: + +* `[-3, 1, 2]` +* `[-3, 2, 1]` +* `[-2, -1, 3]` +* `[-2, 3, -1]` +* `[-1, -2, 3]` +* `[-1, 3, -2]` +* `[1, -3, 2]` +* `[1, 2, -3]` +* `[2, -3, 1]` +* `[2, 1, -3]` +* `[3, -2, -1]` +* `[3, -1, -2]` + +The lexicographically smallest one is `[-3, 1, 2]`. + +**Example 2:** + +**Input:** n = 1, target = 10000000000 + +**Output:** [] + +**Explanation:** + +There are no arrays that sum to 10000000000 and whose absolute values form a permutation of size 1. Therefore, the answer is `[]`. + +**Constraints:** + +* 1 <= n <= 105 +* -1010 <= target <= 1010 \ No newline at end of file diff --git a/src/main/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/Solution.java b/src/main/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/Solution.java new file mode 100644 index 000000000..4f09ec544 --- /dev/null +++ b/src/main/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/Solution.java @@ -0,0 +1,78 @@ +package g3701_3800.s3753_total_waviness_of_numbers_in_range_ii; + +// #Hard #Dynamic_Programming #Math #Senior_Staff #Biweekly_Contest_170 +// #2026_04_26_Time_52_ms_(96.83%)_Space_46.46_MB_(98.41%) + +public class Solution { + private static class Pair { + long count; + long sum; + + Pair(long count, long sum) { + this.count = count; + this.sum = sum; + } + } + + private char[] digits; + private Pair[][][][][] memo; + private boolean[][][][][] seen; + + public long totalWaviness(long num1, long num2) { + return solve(num2) - solve(num1 - 1); + } + + private long solve(long x) { + if (x <= 0) { + return 0; + } + digits = Long.toString(x).toCharArray(); + int n = digits.length; + memo = new Pair[n][2][2][11][11]; + seen = new boolean[n][2][2][11][11]; + return dfs(0, 1, 0, 10, 10).sum; + } + + private Pair dfs(int pos, int tight, int started, int prev2, int prev1) { + if (pos == digits.length) { + return new Pair(1, 0); + } + if (seen[pos][tight][started][prev2][prev1]) { + return memo[pos][tight][started][prev2][prev1]; + } + seen[pos][tight][started][prev2][prev1] = true; + int limit = tight == 1 ? digits[pos] - '0' : 9; + long totalCount = 0; + long totalSum = 0; + for (int d = 0; d <= limit; d++) { + int nextTight = (tight == 1 && d == limit) ? 1 : 0; + if (started == 0 && d == 0) { + // still leading zeros, number not started + Pair nxt = dfs(pos + 1, nextTight, 0, 10, 10); + totalCount += nxt.count; + totalSum += nxt.sum; + } else if (started == 0) { + // first real digit + Pair nxt = dfs(pos + 1, nextTight, 1, 10, d); + totalCount += nxt.count; + totalSum += nxt.sum; + } else if (prev2 == 10) { + // second real digit + Pair nxt = dfs(pos + 1, nextTight, 1, prev1, d); + totalCount += nxt.count; + totalSum += nxt.sum; + } else { + // now prev1 is an interior candidate, decide if it is peak/valley + int add = 0; + if ((prev1 > prev2 && prev1 > d) || (prev1 < prev2 && prev1 < d)) { + add = 1; + } + Pair nxt = dfs(pos + 1, nextTight, 1, prev1, d); + totalCount += nxt.count; + totalSum += nxt.sum + add * nxt.count; + } + } + memo[pos][tight][started][prev2][prev1] = new Pair(totalCount, totalSum); + return memo[pos][tight][started][prev2][prev1]; + } +} diff --git a/src/main/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/readme.md b/src/main/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/readme.md new file mode 100644 index 000000000..ece80ce70 --- /dev/null +++ b/src/main/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/readme.md @@ -0,0 +1,62 @@ +3753\. Total Waviness of Numbers in Range II + +Hard + +You are given two integers `num1` and `num2` representing an **inclusive** range `[num1, num2]`. + +The **waviness** of a number is defined as the total count of its **peaks** and **valleys**: + +* A digit is a **peak** if it is **strictly greater** than both of its immediate neighbors. +* A digit is a **valley** if it is **strictly less** than both of its immediate neighbors. +* The first and last digits of a number **cannot** be peaks or valleys. +* Any number with fewer than 3 digits has a waviness of 0. + +Return the total sum of waviness for all numbers in the range `[num1, num2]`. + +**Example 1:** + +**Input:** num1 = 120, num2 = 130 + +**Output:** 3 + +**Explanation:** + +In the range `[120, 130]`: + +* `120`: middle digit 2 is a peak, waviness = 1. +* `121`: middle digit 2 is a peak, waviness = 1. +* `130`: middle digit 3 is a peak, waviness = 1. +* All other numbers in the range have a waviness of 0. + +Thus, total waviness is `1 + 1 + 1 = 3`. + +**Example 2:** + +**Input:** num1 = 198, num2 = 202 + +**Output:** 3 + +**Explanation:** + +In the range `[198, 202]`: + +* `198`: middle digit 9 is a peak, waviness = 1. +* `201`: middle digit 0 is a valley, waviness = 1. +* `202`: middle digit 0 is a valley, waviness = 1. +* All other numbers in the range have a waviness of 0. + +Thus, total waviness is `1 + 1 + 1 = 3`. + +**Example 3:** + +**Input:** num1 = 4848, num2 = 4848 + +**Output:** 2 + +**Explanation:** + +Number `4848`: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2. + +**Constraints:** + +* 1 <= num1 <= num2 <= 1015 \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/SolutionTest.java b/src/test/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/SolutionTest.java new file mode 100644 index 000000000..d7d8b8df4 --- /dev/null +++ b/src/test/java/g3701_3800/s3751_total_waviness_of_numbers_in_range_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3751_total_waviness_of_numbers_in_range_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void totalWaviness() { + assertThat(new Solution().totalWaviness(120, 130), equalTo(3)); + } + + @Test + void totalWaviness2() { + assertThat(new Solution().totalWaviness(198, 202), equalTo(3)); + } + + @Test + void totalWaviness3() { + assertThat(new Solution().totalWaviness(4848, 4848), equalTo(2)); + } +} diff --git a/src/test/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/SolutionTest.java b/src/test/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/SolutionTest.java new file mode 100644 index 000000000..ce5a0370b --- /dev/null +++ b/src/test/java/g3701_3800/s3752_lexicographically_smallest_negated_permutation_that_sums_to_target/SolutionTest.java @@ -0,0 +1,18 @@ +package g3701_3800.s3752_lexicographically_smallest_negated_permutation_that_sums_to_target; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void lexSmallestNegatedPerm() { + assertThat(new Solution().lexSmallestNegatedPerm(3, 0L), equalTo(new int[] {-3, 1, 2})); + } + + @Test + void lexSmallestNegatedPerm2() { + assertThat(new Solution().lexSmallestNegatedPerm(3, 10000000000L), equalTo(new int[] {})); + } +} diff --git a/src/test/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/SolutionTest.java b/src/test/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/SolutionTest.java new file mode 100644 index 000000000..895b95607 --- /dev/null +++ b/src/test/java/g3701_3800/s3753_total_waviness_of_numbers_in_range_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3701_3800.s3753_total_waviness_of_numbers_in_range_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void totalWaviness() { + assertThat(new Solution().totalWaviness(120L, 130L), equalTo(3L)); + } + + @Test + void totalWaviness2() { + assertThat(new Solution().totalWaviness(198L, 202L), equalTo(3L)); + } + + @Test + void totalWaviness3() { + assertThat(new Solution().totalWaviness(4848L, 4848L), equalTo(2L)); + } +}