From 18e933bb0c9550fb50546bc2c6bcf59d53ef62a7 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Fri, 19 Apr 2024 22:42:08 +0700 Subject: [PATCH 1/5] Added tasks 3096-3100 --- .../Solution.java | 30 +++++++++ .../readme.md | 63 +++++++++++++++++++ .../Solution.java | 29 +++++++++ .../readme.md | 45 +++++++++++++ .../Solution.java | 39 ++++++++++++ .../readme.md | 47 ++++++++++++++ .../s3099_harshad_number/Solution.java | 20 ++++++ .../g3001_3100/s3099_harshad_number/readme.md | 29 +++++++++ .../s3100_water_bottles_ii/Solution.java | 16 +++++ .../s3100_water_bottles_ii/readme.md | 39 ++++++++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 23 +++++++ .../SolutionTest.java | 23 +++++++ .../s3099_harshad_number/SolutionTest.java | 18 ++++++ .../s3100_water_bottles_ii/SolutionTest.java | 18 ++++++ 15 files changed, 462 insertions(+) create mode 100644 src/main/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/Solution.java create mode 100644 src/main/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/readme.md create mode 100644 src/main/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/Solution.java create mode 100644 src/main/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/readme.md create mode 100644 src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java create mode 100644 src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/readme.md create mode 100644 src/main/java/g3001_3100/s3099_harshad_number/Solution.java create mode 100644 src/main/java/g3001_3100/s3099_harshad_number/readme.md create mode 100644 src/main/java/g3001_3100/s3100_water_bottles_ii/Solution.java create mode 100644 src/main/java/g3001_3100/s3100_water_bottles_ii/readme.md create mode 100644 src/test/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3099_harshad_number/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3100_water_bottles_ii/SolutionTest.java diff --git a/src/main/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/Solution.java b/src/main/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/Solution.java new file mode 100644 index 000000000..a1710eb4f --- /dev/null +++ b/src/main/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/Solution.java @@ -0,0 +1,30 @@ +package g3001_3100.s3096_minimum_levels_to_gain_more_points; + +// #Medium #Array #Prefix_Sum #2024_04_19_Time_3_ms_(99.97%)_Space_59.6_MB_(49.99%) + +public class Solution { + public int minimumLevels(int[] possible) { + int n = possible.length; + int sum = 0; + for (int p : possible) { + sum += p; + } + if (sum == 0 && n == 2) { + return -1; + } + if (sum == 0 && n > 2) { + return 1; + } + int sumLeft = 0; + for (int i = 0; i < n - 1; i++) { + sumLeft += possible[i]; + int sumRight = sum - sumLeft; + int danScore = sumLeft - ((i + 1) - sumLeft); + int bobScore = sumRight - ((n - i - 1) - sumRight); + if (danScore > bobScore) { + return i + 1; + } + } + return -1; + } +} diff --git a/src/main/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/readme.md b/src/main/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/readme.md new file mode 100644 index 000000000..5bff6b556 --- /dev/null +++ b/src/main/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/readme.md @@ -0,0 +1,63 @@ +3096\. Minimum Levels to Gain More Points + +Medium + +You are given a binary array `possible` of length `n`. + +Alice and Bob are playing a game that consists of `n` levels. Some of the levels in the game are **impossible** to clear while others can **always** be cleared. In particular, if `possible[i] == 0`, then the ith level is **impossible** to clear for **both** the players. A player gains `1` point on clearing a level and loses `1` point if the player fails to clear it. + +At the start of the game, Alice will play some levels in the **given order** starting from the 0th level, after which Bob will play for the rest of the levels. + +Alice wants to know the **minimum** number of levels she should play to gain more points than Bob, if both players play optimally to **maximize** their points. + +Return _the **minimum** number of levels Alice should play to gain more points_. _If this is **not** possible, return_ `-1`. + +**Note** that each player must play at least `1` level. + +**Example 1:** + +**Input:** possible = [1,0,1,0] + +**Output:** 1 + +**Explanation:** + +Let's look at all the levels that Alice can play up to: + +* If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has -1 + 1 - 1 = -1 point. +* If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points. +* If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 1 - 1 + 1 = 1 point, while Bob has -1 point. + +Alice must play a minimum of 1 level to gain more points. + +**Example 2:** + +**Input:** possible = [1,1,1,1,1] + +**Output:** 3 + +**Explanation:** + +Let's look at all the levels that Alice can play up to: + +* If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points. +* If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points. +* If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points. +* If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point. + +Alice must play a minimum of 3 levels to gain more points. + +**Example 3:** + +**Input:** possible = [0,0] + +**Output:** \-1 + +**Explanation:** + +The only possible way is for both players to play 1 level each. Alice plays level 0 and loses 1 point. Bob plays level 1 and loses 1 point. As both players have equal points, Alice can't gain more points than Bob. + +**Constraints:** + +* 2 <= n == possible.length <= 105 +* `possible[i]` is either `0` or `1`. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/Solution.java b/src/main/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/Solution.java new file mode 100644 index 000000000..f92f2e05c --- /dev/null +++ b/src/main/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/Solution.java @@ -0,0 +1,29 @@ +package g3001_3100.s3097_shortest_subarray_with_or_at_least_k_ii; + +// #Medium #Array #Bit_Manipulation #Sliding_Window +// #2024_04_19_Time_7_ms_(98.43%)_Space_70.2_MB_(74.25%) + +public class Solution { + public int minimumSubarrayLength(int[] nums, int k) { + int n = nums.length; + if (nums[0] >= k) { + return 1; + } + int res = Integer.MAX_VALUE; + for (int i = 1; i < n; i++) { + if (nums[i] >= k) { + return 1; + } + for (int j = i - 1; j >= 0 && (nums[i] | nums[j]) != nums[j]; j--) { + nums[j] |= nums[i]; + if (nums[j] >= k) { + res = Math.min(res, i - j + 1); + } + } + } + if (res == Integer.MAX_VALUE) { + return -1; + } + return res; + } +} diff --git a/src/main/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/readme.md b/src/main/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/readme.md new file mode 100644 index 000000000..b49bcce81 --- /dev/null +++ b/src/main/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/readme.md @@ -0,0 +1,45 @@ +3097\. Shortest Subarray With OR at Least K II + +Medium + +You are given an array `nums` of **non-negative** integers and an integer `k`. + +An array is called **special** if the bitwise `OR` of all of its elements is **at least** `k`. + +Return _the length of the **shortest** **special** **non-empty** subarray of_ `nums`, _or return_ `-1` _if no special subarray exists_. + +**Example 1:** + +**Input:** nums = [1,2,3], k = 2 + +**Output:** 1 + +**Explanation:** + +The subarray `[3]` has `OR` value of `3`. Hence, we return `1`. + +**Example 2:** + +**Input:** nums = [2,1,8], k = 10 + +**Output:** 3 + +**Explanation:** + +The subarray `[2,1,8]` has `OR` value of `11`. Hence, we return `3`. + +**Example 3:** + +**Input:** nums = [1,2], k = 0 + +**Output:** 1 + +**Explanation:** + +The subarray `[1]` has `OR` value of `1`. Hence, we return `1`. + +**Constraints:** + +* 1 <= nums.length <= 2 * 105 +* 0 <= nums[i] <= 109 +* 0 <= k <= 109 \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java b/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java new file mode 100644 index 000000000..9905060c4 --- /dev/null +++ b/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java @@ -0,0 +1,39 @@ +package g3001_3100.s3098_find_the_sum_of_subsequence_powers; + +// #Hard #Array #Dynamic_Programming #Sorting #2024_04_19_Time_34_ms_(91.54%)_Space_47.9_MB_(65.64%) + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class Solution { + private final int MOD = 1_000_000_007; + private int len; + + private int dfs(int lastIdx, int k, int minDiff, Map dp, int[] nums) { + if (k == 0) { + return minDiff; + } + long key = (((long) minDiff) << 12) + ((long) lastIdx << 6) + k; + if (dp.containsKey(key)) { + return dp.get(key); + } + int res = 0; + for (int i = lastIdx + 1; i <= len - k; i++) { + res = (res + dfs(i, k - 1, Math.min(minDiff, nums[i] - nums[lastIdx]), dp, nums)) % MOD; + } + dp.put(key, res); + return res; + } + + public int sumOfPowers(int[] nums, int k) { + len = nums.length; + Arrays.sort(nums); + Map dp = new HashMap<>(); + int res = 0; + for (int i = 0; i <= len - k; i++) { + res = (res + dfs(i, k - 1, nums[len - 1] - nums[0], dp, nums)) % MOD; + } + return res; + } +} diff --git a/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/readme.md b/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/readme.md new file mode 100644 index 000000000..df513170e --- /dev/null +++ b/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/readme.md @@ -0,0 +1,47 @@ +3098\. Find the Sum of Subsequence Powers + +Hard + +You are given an integer array `nums` of length `n`, and a **positive** integer `k`. + +The **power** of a subsequence is defined as the **minimum** absolute difference between **any** two elements in the subsequence. + +Return _the **sum** of **powers** of **all** subsequences of_ `nums` _which have length_ **_equal to_** `k`. + +Since the answer may be large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** nums = [1,2,3,4], k = 3 + +**Output:** 4 + +**Explanation:** + +There are 4 subsequences in `nums` which have length 3: `[1,2,3]`, `[1,3,4]`, `[1,2,4]`, and `[2,3,4]`. The sum of powers is `|2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4`. + +**Example 2:** + +**Input:** nums = [2,2], k = 2 + +**Output:** 0 + +**Explanation:** + +The only subsequence in `nums` which has length 2 is `[2,2]`. The sum of powers is `|2 - 2| = 0`. + +**Example 3:** + +**Input:** nums = [4,3,-1], k = 2 + +**Output:** 10 + +**Explanation:** + +There are 3 subsequences in `nums` which have length 2: `[4,3]`, `[4,-1]`, and `[3,-1]`. The sum of powers is `|4 - 3| + |4 - (-1)| + |3 - (-1)| = 10`. + +**Constraints:** + +* `2 <= n == nums.length <= 50` +* -108 <= nums[i] <= 108 +* `2 <= k <= n` \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3099_harshad_number/Solution.java b/src/main/java/g3001_3100/s3099_harshad_number/Solution.java new file mode 100644 index 000000000..6898426be --- /dev/null +++ b/src/main/java/g3001_3100/s3099_harshad_number/Solution.java @@ -0,0 +1,20 @@ +package g3001_3100.s3099_harshad_number; + +// #Easy #Math #2024_04_19_Time_0_ms_(100.00%)_Space_40.9_MB_(7.00%) + +public class Solution { + public int sumOfTheDigitsOfHarshadNumber(int x) { + int sum = 0; + int digit; + int temp = x; + while (temp != 0) { + digit = temp % 10; + sum = sum + digit; + temp = temp / 10; + } + if (x % sum == 0) { + return sum; + } + return -1; + } +} diff --git a/src/main/java/g3001_3100/s3099_harshad_number/readme.md b/src/main/java/g3001_3100/s3099_harshad_number/readme.md new file mode 100644 index 000000000..a0b260849 --- /dev/null +++ b/src/main/java/g3001_3100/s3099_harshad_number/readme.md @@ -0,0 +1,29 @@ +3099\. Harshad Number + +Easy + +An integer divisible by the **sum** of its digits is said to be a **Harshad** number. You are given an integer `x`. Return _the sum of the digits_ of `x` if `x` is a **Harshad** number, otherwise, return `-1`_._ + +**Example 1:** + +**Input:** x = 18 + +**Output:** 9 + +**Explanation:** + +The sum of digits of `x` is `9`. `18` is divisible by `9`. So `18` is a Harshad number and the answer is `9`. + +**Example 2:** + +**Input:** x = 23 + +**Output:** \-1 + +**Explanation:** + +The sum of digits of `x` is `5`. `23` is not divisible by `5`. So `23` is not a Harshad number and the answer is `-1`. + +**Constraints:** + +* `1 <= x <= 100` \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3100_water_bottles_ii/Solution.java b/src/main/java/g3001_3100/s3100_water_bottles_ii/Solution.java new file mode 100644 index 000000000..8517d2f26 --- /dev/null +++ b/src/main/java/g3001_3100/s3100_water_bottles_ii/Solution.java @@ -0,0 +1,16 @@ +package g3001_3100.s3100_water_bottles_ii; + +// #Medium #Math #Simulation #2024_04_19_Time_0_ms_(100.00%)_Space_40.8_MB_(45.33%) + +public class Solution { + public int maxBottlesDrunk(int numBottles, int numExchange) { + int emptyBottles = numBottles; + int bottleDrinks = numBottles; + while (numExchange <= emptyBottles) { + bottleDrinks += 1; + emptyBottles = 1 + (emptyBottles - numExchange); + numExchange++; + } + return bottleDrinks; + } +} diff --git a/src/main/java/g3001_3100/s3100_water_bottles_ii/readme.md b/src/main/java/g3001_3100/s3100_water_bottles_ii/readme.md new file mode 100644 index 000000000..2c43d138d --- /dev/null +++ b/src/main/java/g3001_3100/s3100_water_bottles_ii/readme.md @@ -0,0 +1,39 @@ +3100\. Water Bottles II + +Medium + +You are given two integers `numBottles` and `numExchange`. + +`numBottles` represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations: + +* Drink any number of full water bottles turning them into empty bottles. +* Exchange `numExchange` empty bottles with one full water bottle. Then, increase `numExchange` by one. + +Note that you cannot exchange multiple batches of empty bottles for the same value of `numExchange`. For example, if `numBottles == 3` and `numExchange == 1`, you cannot exchange `3` empty water bottles for `3` full bottles. + +Return _the **maximum** number of water bottles you can drink_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2024/01/28/exampleone1.png) + +**Input:** numBottles = 13, numExchange = 6 + +**Output:** 15 + +**Explanation:** The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/01/28/example231.png) + +**Input:** numBottles = 10, numExchange = 3 + +**Output:** 13 + +**Explanation:** The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk. + +**Constraints:** + +* `1 <= numBottles <= 100` +* `1 <= numExchange <= 100` \ No newline at end of file diff --git a/src/test/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/SolutionTest.java b/src/test/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/SolutionTest.java new file mode 100644 index 000000000..3b64b3e63 --- /dev/null +++ b/src/test/java/g3001_3100/s3096_minimum_levels_to_gain_more_points/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3096_minimum_levels_to_gain_more_points; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumLevels() { + assertThat(new Solution().minimumLevels(new int[] {1, 0, 1, 0}), equalTo(1)); + } + + @Test + void minimumLevels2() { + assertThat(new Solution().minimumLevels(new int[] {1, 1, 1, 1, 1}), equalTo(3)); + } + + @Test + void minimumLevels3() { + assertThat(new Solution().minimumLevels(new int[] {0, 0}), equalTo(-1)); + } +} diff --git a/src/test/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/SolutionTest.java b/src/test/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/SolutionTest.java new file mode 100644 index 000000000..0f8e01753 --- /dev/null +++ b/src/test/java/g3001_3100/s3097_shortest_subarray_with_or_at_least_k_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3097_shortest_subarray_with_or_at_least_k_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumSubarrayLength() { + assertThat(new Solution().minimumSubarrayLength(new int[] {1, 2, 3}, 2), equalTo(1)); + } + + @Test + void minimumSubarrayLength2() { + assertThat(new Solution().minimumSubarrayLength(new int[] {2, 1, 8}, 10), equalTo(3)); + } + + @Test + void minimumSubarrayLength3() { + assertThat(new Solution().minimumSubarrayLength(new int[] {1, 2}, 0), equalTo(1)); + } +} diff --git a/src/test/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/SolutionTest.java b/src/test/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/SolutionTest.java new file mode 100644 index 000000000..aab12a10e --- /dev/null +++ b/src/test/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3098_find_the_sum_of_subsequence_powers; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sumOfPowers() { + assertThat(new Solution().sumOfPowers(new int[] {1, 2, 3, 4}, 3), equalTo(4)); + } + + @Test + void sumOfPowers2() { + assertThat(new Solution().sumOfPowers(new int[] {2, 2}, 2), equalTo(0)); + } + + @Test + void sumOfPowers3() { + assertThat(new Solution().sumOfPowers(new int[] {4, 3, -1}, 2), equalTo(10)); + } +} diff --git a/src/test/java/g3001_3100/s3099_harshad_number/SolutionTest.java b/src/test/java/g3001_3100/s3099_harshad_number/SolutionTest.java new file mode 100644 index 000000000..57263d686 --- /dev/null +++ b/src/test/java/g3001_3100/s3099_harshad_number/SolutionTest.java @@ -0,0 +1,18 @@ +package g3001_3100.s3099_harshad_number; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sumOfTheDigitsOfHarshadNumber() { + assertThat(new Solution().sumOfTheDigitsOfHarshadNumber(18), equalTo(9)); + } + + @Test + void sumOfTheDigitsOfHarshadNumber2() { + assertThat(new Solution().sumOfTheDigitsOfHarshadNumber(23), equalTo(-1)); + } +} diff --git a/src/test/java/g3001_3100/s3100_water_bottles_ii/SolutionTest.java b/src/test/java/g3001_3100/s3100_water_bottles_ii/SolutionTest.java new file mode 100644 index 000000000..cfa9df698 --- /dev/null +++ b/src/test/java/g3001_3100/s3100_water_bottles_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3001_3100.s3100_water_bottles_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxBottlesDrunk() { + assertThat(new Solution().maxBottlesDrunk(13, 6), equalTo(15)); + } + + @Test + void maxBottlesDrunk2() { + assertThat(new Solution().maxBottlesDrunk(10, 3), equalTo(13)); + } +} From ff3cb7874f3054155dc3e9e9d86aba17bbc0a638 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Fri, 19 Apr 2024 22:48:55 +0700 Subject: [PATCH 2/5] Update Solution.java --- .../s3098_find_the_sum_of_subsequence_powers/Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java b/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java index 9905060c4..2f2a480ed 100644 --- a/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java +++ b/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java @@ -7,7 +7,7 @@ import java.util.Map; public class Solution { - private final int MOD = 1_000_000_007; + private final int mod = 1_000_000_007; private int len; private int dfs(int lastIdx, int k, int minDiff, Map dp, int[] nums) { @@ -20,7 +20,7 @@ private int dfs(int lastIdx, int k, int minDiff, Map dp, int[] nu } int res = 0; for (int i = lastIdx + 1; i <= len - k; i++) { - res = (res + dfs(i, k - 1, Math.min(minDiff, nums[i] - nums[lastIdx]), dp, nums)) % MOD; + res = (res + dfs(i, k - 1, Math.min(minDiff, nums[i] - nums[lastIdx]), dp, nums)) % mod; } dp.put(key, res); return res; @@ -32,7 +32,7 @@ public int sumOfPowers(int[] nums, int k) { Map dp = new HashMap<>(); int res = 0; for (int i = 0; i <= len - k; i++) { - res = (res + dfs(i, k - 1, nums[len - 1] - nums[0], dp, nums)) % MOD; + res = (res + dfs(i, k - 1, nums[len - 1] - nums[0], dp, nums)) % mod; } return res; } From 4f9754e0c5f871b657df5eddee31c2f7a657fdcf Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Fri, 19 Apr 2024 22:51:27 +0700 Subject: [PATCH 3/5] Update Solution.java --- src/main/java/g3001_3100/s3099_harshad_number/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3001_3100/s3099_harshad_number/Solution.java b/src/main/java/g3001_3100/s3099_harshad_number/Solution.java index 6898426be..183f88adc 100644 --- a/src/main/java/g3001_3100/s3099_harshad_number/Solution.java +++ b/src/main/java/g3001_3100/s3099_harshad_number/Solution.java @@ -12,7 +12,7 @@ public int sumOfTheDigitsOfHarshadNumber(int x) { sum = sum + digit; temp = temp / 10; } - if (x % sum == 0) { + if (sum !=0 && x % sum == 0) { return sum; } return -1; From 87ea5400e1829938f38f89acf3b6a675372db809 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Fri, 19 Apr 2024 22:53:06 +0700 Subject: [PATCH 4/5] Update Solution.java --- src/main/java/g3001_3100/s3099_harshad_number/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3001_3100/s3099_harshad_number/Solution.java b/src/main/java/g3001_3100/s3099_harshad_number/Solution.java index 183f88adc..484597927 100644 --- a/src/main/java/g3001_3100/s3099_harshad_number/Solution.java +++ b/src/main/java/g3001_3100/s3099_harshad_number/Solution.java @@ -12,7 +12,7 @@ public int sumOfTheDigitsOfHarshadNumber(int x) { sum = sum + digit; temp = temp / 10; } - if (sum !=0 && x % sum == 0) { + if (sum != 0 && x % sum == 0) { return sum; } return -1; From 9e6dd39027a3b1ea7f29ce26f6d38eae44a7d3ea Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Fri, 19 Apr 2024 22:55:08 +0700 Subject: [PATCH 5/5] Update Solution.java --- .../s3098_find_the_sum_of_subsequence_powers/Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java b/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java index 2f2a480ed..460079502 100644 --- a/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java +++ b/src/main/java/g3001_3100/s3098_find_the_sum_of_subsequence_powers/Solution.java @@ -7,7 +7,7 @@ import java.util.Map; public class Solution { - private final int mod = 1_000_000_007; + private static final int MOD = 1_000_000_007; private int len; private int dfs(int lastIdx, int k, int minDiff, Map dp, int[] nums) { @@ -20,7 +20,7 @@ private int dfs(int lastIdx, int k, int minDiff, Map dp, int[] nu } int res = 0; for (int i = lastIdx + 1; i <= len - k; i++) { - res = (res + dfs(i, k - 1, Math.min(minDiff, nums[i] - nums[lastIdx]), dp, nums)) % mod; + res = (res + dfs(i, k - 1, Math.min(minDiff, nums[i] - nums[lastIdx]), dp, nums)) % MOD; } dp.put(key, res); return res; @@ -32,7 +32,7 @@ public int sumOfPowers(int[] nums, int k) { Map dp = new HashMap<>(); int res = 0; for (int i = 0; i <= len - k; i++) { - res = (res + dfs(i, k - 1, nums[len - 1] - nums[0], dp, nums)) % mod; + res = (res + dfs(i, k - 1, nums[len - 1] - nums[0], dp, nums)) % MOD; } return res; }