diff --git a/src/main/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/Solution.java b/src/main/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/Solution.java new file mode 100644 index 000000000..8ecb80476 --- /dev/null +++ b/src/main/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/Solution.java @@ -0,0 +1,20 @@ +package g3101_3200.s3184_count_pairs_that_form_a_complete_day_i; + +// #Easy #Array #Hash_Table #Counting #2024_06_21_Time_1_ms_(98.20%)_Space_42_MB_(83.72%) + +public class Solution { + public int countCompleteDayPairs(int[] hours) { + int[] modular = new int[26]; + int ans = 0; + for (int hour : hours) { + int mod = hour % 24; + ans += modular[24 - mod]; + if (mod == 0) { + modular[24]++; + } else { + modular[mod]++; + } + } + return ans; + } +} diff --git a/src/main/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/readme.md b/src/main/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/readme.md new file mode 100644 index 000000000..178dab189 --- /dev/null +++ b/src/main/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/readme.md @@ -0,0 +1,34 @@ +3184\. Count Pairs That Form a Complete Day I + +Easy + +Given an integer array `hours` representing times in **hours**, return an integer denoting the number of pairs `i`, `j` where `i < j` and `hours[i] + hours[j]` forms a **complete day**. + +A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours. + +For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on. + +**Example 1:** + +**Input:** hours = [12,12,30,24,24] + +**Output:** 2 + +**Explanation:** + +The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`. + +**Example 2:** + +**Input:** hours = [72,48,24,3] + +**Output:** 3 + +**Explanation:** + +The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`. + +**Constraints:** + +* `1 <= hours.length <= 100` +* 1 <= hours[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/Solution.java b/src/main/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/Solution.java new file mode 100644 index 000000000..c574f392a --- /dev/null +++ b/src/main/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/Solution.java @@ -0,0 +1,18 @@ +package g3101_3200.s3185_count_pairs_that_form_a_complete_day_ii; + +// #Medium #Array #Hash_Table #Counting #2024_06_21_Time_3_ms_(97.60%)_Space_97.1_MB_(14.69%) + +public class Solution { + public long countCompleteDayPairs(int[] hours) { + long[] hour = new long[24]; + for (int j : hours) { + hour[j % 24]++; + } + long counter = hour[0] * (hour[0] - 1) / 2; + counter += hour[12] * (hour[12] - 1) / 2; + for (int i = 1; i < 12; i++) { + counter += hour[i] * hour[24 - i]; + } + return counter; + } +} diff --git a/src/main/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/readme.md b/src/main/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/readme.md new file mode 100644 index 000000000..187c79898 --- /dev/null +++ b/src/main/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/readme.md @@ -0,0 +1,30 @@ +3185\. Count Pairs That Form a Complete Day II + +Medium + +Given an integer array `hours` representing times in **hours**, return an integer denoting the number of pairs `i`, `j` where `i < j` and `hours[i] + hours[j]` forms a **complete day**. + +A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours. + +For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on. + +**Example 1:** + +**Input:** hours = [12,12,30,24,24] + +**Output:** 2 + +**Explanation:** The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`. + +**Example 2:** + +**Input:** hours = [72,48,24,3] + +**Output:** 3 + +**Explanation:** The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`. + +**Constraints:** + +* 1 <= hours.length <= 5 * 105 +* 1 <= hours[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/Solution.java b/src/main/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/Solution.java new file mode 100644 index 000000000..04b17eb65 --- /dev/null +++ b/src/main/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/Solution.java @@ -0,0 +1,71 @@ +package g3101_3200.s3186_maximum_total_damage_with_spell_casting; + +// #Medium #Array #Hash_Table #Dynamic_Programming #Sorting #Binary_Search #Two_Pointers #Counting +// #2024_06_21_Time_51_ms_(99.29%)_Space_60.8_MB_(78.34%) + +import java.util.Arrays; + +public class Solution { + public long maximumTotalDamage(int[] power) { + int maxPower = 0; + for (int p : power) { + if (p > maxPower) { + maxPower = p; + } + } + return (maxPower <= 1_000_000) ? smallPower(power, maxPower) : bigPower(power); + } + + private long smallPower(int[] power, int maxPower) { + int[] counts = new int[maxPower + 6]; + for (int p : power) { + counts[p]++; + } + long[] dp = new long[maxPower + 6]; + dp[1] = counts[1]; + dp[2] = Math.max(counts[2] * 2L, dp[1]); + for (int i = 3; i <= maxPower; i++) { + dp[i] = Math.max(counts[i] * i + dp[i - 3], Math.max(dp[i - 1], dp[i - 2])); + } + return dp[maxPower]; + } + + private long bigPower(int[] power) { + Arrays.sort(power); + int n = power.length; + long[] prevs = new long[4]; + int curPower = power[0]; + int count = 1; + long result = 0; + for (int i = 1; i <= n; i++) { + int p = (i == n) ? 1_000_000_009 : power[i]; + if (p == curPower) { + count++; + } else { + long curVal = + Math.max((long) curPower * count + prevs[3], Math.max(prevs[1], prevs[2])); + int diff = Math.min(p - curPower, prevs.length - 1); + long nextCurVal = (diff == 1) ? 0 : Math.max(prevs[3], Math.max(curVal, prevs[2])); + // Shift the values in prevs[]. + int k = prevs.length - 1; + if (diff < prevs.length - 1) { + while (k > diff) { + prevs[k] = prevs[k-- - diff]; + } + prevs[k--] = curVal; + } + while (k > 0) { + prevs[k--] = nextCurVal; + } + curPower = p; + count = 1; + } + } + for (long v : prevs) { + if (v > result) { + result = v; + } + } + return result; + } +} diff --git a/src/main/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/readme.md b/src/main/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/readme.md new file mode 100644 index 000000000..c841f31de --- /dev/null +++ b/src/main/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/readme.md @@ -0,0 +1,38 @@ +3186\. Maximum Total Damage With Spell Casting + +Medium + +A magician has various spells. + +You are given an array `power`, where each element represents the damage of a spell. Multiple spells can have the same damage value. + +It is a known fact that if a magician decides to cast a spell with a damage of `power[i]`, they **cannot** cast any spell with a damage of `power[i] - 2`, `power[i] - 1`, `power[i] + 1`, or `power[i] + 2`. + +Each spell can be cast **only once**. + +Return the **maximum** possible _total damage_ that a magician can cast. + +**Example 1:** + +**Input:** power = [1,1,3,4] + +**Output:** 6 + +**Explanation:** + +The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4. + +**Example 2:** + +**Input:** power = [7,1,6,6] + +**Output:** 13 + +**Explanation:** + +The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6. + +**Constraints:** + +* 1 <= power.length <= 105 +* 1 <= power[i] <= 109 \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3187_peaks_in_array/Solution.java b/src/main/java/g3101_3200/s3187_peaks_in_array/Solution.java new file mode 100644 index 000000000..a73836a31 --- /dev/null +++ b/src/main/java/g3101_3200/s3187_peaks_in_array/Solution.java @@ -0,0 +1,72 @@ +package g3101_3200.s3187_peaks_in_array; + +// #Hard #Array #Segment_Tree #Binary_Indexed_Tree +// #2024_06_21_Time_18_ms_(100.00%)_Space_137.7_MB_(31.34%) + +import java.util.ArrayList; +import java.util.List; + +public class Solution { + public List countOfPeaks(int[] nums, int[][] queries) { + boolean[] peaks = new boolean[nums.length]; + int[] binaryIndexedTree = new int[Integer.highestOneBit(peaks.length) * 2 + 1]; + for (int i = 1; i < peaks.length - 1; ++i) { + if (nums[i] > Math.max(nums[i - 1], nums[i + 1])) { + peaks[i] = true; + update(binaryIndexedTree, i + 1, 1); + } + } + + List result = new ArrayList<>(); + for (int[] query : queries) { + if (query[0] == 1) { + int leftIndex = query[1]; + int rightIndex = query[2]; + result.add(computeRangeSum(binaryIndexedTree, leftIndex + 2, rightIndex)); + } else { + int index = query[1]; + int value = query[2]; + nums[index] = value; + for (int i = -1; i <= 1; ++i) { + int affected = index + i; + if (affected >= 1 && affected <= nums.length - 2) { + boolean peak = + nums[affected] > Math.max(nums[affected - 1], nums[affected + 1]); + if (peak != peaks[affected]) { + if (peak) { + update(binaryIndexedTree, affected + 1, 1); + } else { + update(binaryIndexedTree, affected + 1, -1); + } + peaks[affected] = peak; + } + } + } + } + } + return result; + } + + private int computeRangeSum(int[] binaryIndexedTree, int beginIndex, int endIndex) { + return (beginIndex <= endIndex) + ? (query(binaryIndexedTree, endIndex) - query(binaryIndexedTree, beginIndex - 1)) + : 0; + } + + private int query(int[] binaryIndexedTree, int index) { + int result = 0; + while (index != 0) { + result += binaryIndexedTree[index]; + index -= index & -index; + } + + return result; + } + + private void update(int[] binaryIndexedTree, int index, int delta) { + while (index < binaryIndexedTree.length) { + binaryIndexedTree[index] += delta; + index += index & -index; + } + } +} diff --git a/src/main/java/g3101_3200/s3187_peaks_in_array/readme.md b/src/main/java/g3101_3200/s3187_peaks_in_array/readme.md new file mode 100644 index 000000000..4b9bdd7db --- /dev/null +++ b/src/main/java/g3101_3200/s3187_peaks_in_array/readme.md @@ -0,0 +1,54 @@ +3187\. Peaks in Array + +Hard + +A **peak** in an array `arr` is an element that is **greater** than its previous and next element in `arr`. + +You are given an integer array `nums` and a 2D integer array `queries`. + +You have to process queries of two types: + +* queries[i] = [1, li, ri], determine the count of **peak** elements in the subarray nums[li..ri]. +* queries[i] = [2, indexi, vali], change nums[indexi] to vali. + +Return an array `answer` containing the results of the queries of the first type in order. + +**Notes:** + +* The **first** and the **last** element of an array or a subarray **cannot** be a peak. + +**Example 1:** + +**Input:** nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]] + +**Output:** [0] + +**Explanation:** + +First query: We change `nums[3]` to 4 and `nums` becomes `[3,1,4,4,5]`. + +Second query: The number of peaks in the `[3,1,4,4,5]` is 0. + +**Example 2:** + +**Input:** nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]] + +**Output:** [0,1] + +**Explanation:** + +First query: `nums[2]` should become 4, but it is already set to 4. + +Second query: The number of peaks in the `[4,1,4]` is 0. + +Third query: The second 4 is a peak in the `[4,1,4,2,1]`. + +**Constraints:** + +* 3 <= nums.length <= 105 +* 1 <= nums[i] <= 105 +* 1 <= queries.length <= 105 +* `queries[i][0] == 1` or `queries[i][0] == 2` +* For all `i` that: + * `queries[i][0] == 1`: `0 <= queries[i][1] <= queries[i][2] <= nums.length - 1` + * `queries[i][0] == 2`: `0 <= queries[i][1] <= nums.length - 1`, 1 <= queries[i][2] <= 105 \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/SolutionTest.java b/src/test/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/SolutionTest.java new file mode 100644 index 000000000..0982445d7 --- /dev/null +++ b/src/test/java/g3101_3200/s3184_count_pairs_that_form_a_complete_day_i/SolutionTest.java @@ -0,0 +1,19 @@ +package g3101_3200.s3184_count_pairs_that_form_a_complete_day_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countCompleteDayPairs() { + assertThat( + new Solution().countCompleteDayPairs(new int[] {12, 12, 30, 24, 24}), equalTo(2)); + } + + @Test + void countCompleteDayPairs2() { + assertThat(new Solution().countCompleteDayPairs(new int[] {72, 48, 24, 3}), equalTo(3)); + } +} diff --git a/src/test/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/SolutionTest.java b/src/test/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/SolutionTest.java new file mode 100644 index 000000000..084c27553 --- /dev/null +++ b/src/test/java/g3101_3200/s3185_count_pairs_that_form_a_complete_day_ii/SolutionTest.java @@ -0,0 +1,19 @@ +package g3101_3200.s3185_count_pairs_that_form_a_complete_day_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countCompleteDayPairs() { + assertThat( + new Solution().countCompleteDayPairs(new int[] {12, 12, 30, 24, 24}), equalTo(2L)); + } + + @Test + void countCompleteDayPairs2() { + assertThat(new Solution().countCompleteDayPairs(new int[] {72, 48, 24, 3}), equalTo(3L)); + } +} diff --git a/src/test/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/SolutionTest.java b/src/test/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/SolutionTest.java new file mode 100644 index 000000000..709497e4e --- /dev/null +++ b/src/test/java/g3101_3200/s3186_maximum_total_damage_with_spell_casting/SolutionTest.java @@ -0,0 +1,25 @@ +package g3101_3200.s3186_maximum_total_damage_with_spell_casting; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximumTotalDamage() { + assertThat(new Solution().maximumTotalDamage(new int[] {1, 1, 3, 4}), equalTo(6L)); + } + + @Test + void maximumTotalDamage2() { + assertThat(new Solution().maximumTotalDamage(new int[] {7, 1, 6, 6}), equalTo(13L)); + } + + @Test + void maximumTotalDamage3() { + assertThat( + new Solution().maximumTotalDamage(new int[] {1_000_001, 1, 6, 6}), + equalTo(1000014L)); + } +} diff --git a/src/test/java/g3101_3200/s3187_peaks_in_array/SolutionTest.java b/src/test/java/g3101_3200/s3187_peaks_in_array/SolutionTest.java new file mode 100644 index 000000000..0561b4c88 --- /dev/null +++ b/src/test/java/g3101_3200/s3187_peaks_in_array/SolutionTest.java @@ -0,0 +1,28 @@ +package g3101_3200.s3187_peaks_in_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countOfPeaks() { + assertThat( + new Solution() + .countOfPeaks( + new int[] {3, 1, 4, 2, 5}, new int[][] {{2, 3, 4}, {1, 0, 4}}), + equalTo(List.of(0))); + } + + @Test + void countOfPeaks2() { + assertThat( + new Solution() + .countOfPeaks( + new int[] {4, 1, 4, 2, 1, 5}, + new int[][] {{2, 2, 4}, {1, 0, 2}, {1, 0, 4}}), + equalTo(List.of(0, 1))); + } +}