Skip to content

Added tasks 3184-3187 #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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`
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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:**

* <code>1 <= hours.length <= 5 * 10<sup>5</sup></code>
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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:**

* <code>1 <= power.length <= 10<sup>5</sup></code>
* <code>1 <= power[i] <= 10<sup>9</sup></code>
72 changes: 72 additions & 0 deletions src/main/java/g3101_3200/s3187_peaks_in_array/Solution.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> 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;
}
}
}
54 changes: 54 additions & 0 deletions src/main/java/g3101_3200/s3187_peaks_in_array/readme.md
Original file line number Diff line number Diff line change
@@ -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:

* <code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of **peak** elements in the subarray <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.
* <code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code>val<sub>i</sub></code>.

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:**

* <code>3 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `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`, <code>1 <= queries[i][2] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading
Loading