Skip to content

Commit 8fc39e1

Browse files
authored
Added tasks 3184-3213
1 parent 644643d commit 8fc39e1

File tree

27 files changed

+2256
-112
lines changed
  • src/main/java
    • g0101_0200
    • g3101_3200
      • s3184_count_pairs_that_form_a_complete_day_i
      • s3185_count_pairs_that_form_a_complete_day_ii
      • s3186_maximum_total_damage_with_spell_casting
      • s3187_peaks_in_array
      • s3190_find_minimum_operations_to_make_all_elements_divisible_by_three
      • s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i
      • s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii
      • s3193_count_the_number_of_inversions
      • s3194_minimum_average_of_smallest_and_largest_elements
      • s3195_find_the_minimum_area_to_cover_all_ones_i
      • s3196_maximize_total_cost_of_alternating_subarrays
      • s3197_find_the_minimum_area_to_cover_all_ones_ii
      • s3200_maximum_height_of_a_triangle
    • g3201_3300
      • s3201_find_the_maximum_length_of_valid_subsequence_i
      • s3202_find_the_maximum_length_of_valid_subsequence_ii
      • s3203_find_minimum_diameter_after_merging_two_trees
      • s3206_alternating_groups_i
      • s3207_maximum_points_after_enemy_battles
      • s3208_alternating_groups_ii
      • s3209_number_of_subarrays_with_and_value_of_k
      • s3210_find_the_encrypted_string
      • s3211_generate_binary_strings_without_adjacent_zeros
      • s3212_count_submatrices_with_equal_frequency_of_x_and_y
      • s3213_construct_string_with_minimum_cost

27 files changed

+2256
-112
lines changed

README.md

Lines changed: 115 additions & 91 deletions
Large diffs are not rendered by default.

src/main/java/g0101_0200/s0152_maximum_product_subarray/readme.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,21 @@ A **subarray** is a contiguous subsequence of the array.
3737

3838
```java
3939
public class Solution {
40-
public int maxProduct(int[] arr) {
41-
int ans = Integer.MIN_VALUE;
42-
int cprod = 1;
43-
for (int j : arr) {
44-
cprod = cprod * j;
45-
ans = Math.max(ans, cprod);
46-
if (cprod == 0) {
47-
cprod = 1;
40+
public int maxProduct(int[] nums) {
41+
int currentMaxProd = nums[0];
42+
int currentMinProd = nums[0];
43+
int overAllMaxProd = nums[0];
44+
for (int i = 1; i < nums.length; i++) {
45+
if (nums[i] < 0) {
46+
int temp = currentMaxProd;
47+
currentMaxProd = currentMinProd;
48+
currentMinProd = temp;
4849
}
50+
currentMaxProd = Math.max(nums[i], nums[i] * currentMaxProd);
51+
currentMinProd = Math.min(nums[i], nums[i] * currentMinProd);
52+
overAllMaxProd = Math.max(overAllMaxProd, currentMaxProd);
4953
}
50-
cprod = 1;
51-
for (int i = arr.length - 1; i >= 0; i--) {
52-
cprod = cprod * arr[i];
53-
ans = Math.max(ans, cprod);
54-
if (cprod == 0) {
55-
cprod = 1;
56-
}
57-
}
58-
return ans;
54+
return overAllMaxProd;
5955
}
6056
}
6157
```

src/main/java/g0101_0200/s0180_consecutive_numbers/readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ The query result format is in the following example.
5454

5555
```sql
5656
# Write your MySQL query statement below
57-
select distinct num as ConsecutiveNums from
58-
(select num, lag(num,1) over(order by id) as l1, lag(num,2) over(order by id) as l2
59-
from Logs) con_thr
60-
where num = l1 and num = l2
57+
SELECT DISTINCT l1.num AS ConsecutiveNums
58+
FROM Logs l1
59+
JOIN Logs l2 ON l1.id = l2.id - 1
60+
JOIN Logs l3 ON l1.id = l3.id - 2
61+
WHERE l1.num = l2.num AND l2.num = l3.num;
6162
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3184\. Count Pairs That Form a Complete Day I
5+
6+
Easy
7+
8+
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**.
9+
10+
A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours.
11+
12+
For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.
13+
14+
**Example 1:**
15+
16+
**Input:** hours = [12,12,30,24,24]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.
23+
24+
**Example 2:**
25+
26+
**Input:** hours = [72,48,24,3]
27+
28+
**Output:** 3
29+
30+
**Explanation:**
31+
32+
The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.
33+
34+
**Constraints:**
35+
36+
* `1 <= hours.length <= 100`
37+
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
38+
39+
## Solution
40+
41+
```java
42+
public class Solution {
43+
public int countCompleteDayPairs(int[] hours) {
44+
int[] modular = new int[26];
45+
int ans = 0;
46+
for (int hour : hours) {
47+
int mod = hour % 24;
48+
ans += modular[24 - mod];
49+
if (mod == 0) {
50+
modular[24]++;
51+
} else {
52+
modular[mod]++;
53+
}
54+
}
55+
return ans;
56+
}
57+
}
58+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3185\. Count Pairs That Form a Complete Day II
5+
6+
Medium
7+
8+
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**.
9+
10+
A **complete day** is defined as a time duration that is an **exact** **multiple** of 24 hours.
11+
12+
For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.
13+
14+
**Example 1:**
15+
16+
**Input:** hours = [12,12,30,24,24]
17+
18+
**Output:** 2
19+
20+
**Explanation:** The pairs of indices that form a complete day are `(0, 1)` and `(3, 4)`.
21+
22+
**Example 2:**
23+
24+
**Input:** hours = [72,48,24,3]
25+
26+
**Output:** 3
27+
28+
**Explanation:** The pairs of indices that form a complete day are `(0, 1)`, `(0, 2)`, and `(1, 2)`.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= hours.length <= 5 * 10<sup>5</sup></code>
33+
* <code>1 <= hours[i] <= 10<sup>9</sup></code>
34+
35+
## Solution
36+
37+
```java
38+
public class Solution {
39+
public long countCompleteDayPairs(int[] hours) {
40+
long[] hour = new long[24];
41+
for (int j : hours) {
42+
hour[j % 24]++;
43+
}
44+
long counter = hour[0] * (hour[0] - 1) / 2;
45+
counter += hour[12] * (hour[12] - 1) / 2;
46+
for (int i = 1; i < 12; i++) {
47+
counter += hour[i] * hour[24 - i];
48+
}
49+
return counter;
50+
}
51+
}
52+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3186\. Maximum Total Damage With Spell Casting
5+
6+
Medium
7+
8+
A magician has various spells.
9+
10+
You are given an array `power`, where each element represents the damage of a spell. Multiple spells can have the same damage value.
11+
12+
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`.
13+
14+
Each spell can be cast **only once**.
15+
16+
Return the **maximum** possible _total damage_ that a magician can cast.
17+
18+
**Example 1:**
19+
20+
**Input:** power = [1,1,3,4]
21+
22+
**Output:** 6
23+
24+
**Explanation:**
25+
26+
The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.
27+
28+
**Example 2:**
29+
30+
**Input:** power = [7,1,6,6]
31+
32+
**Output:** 13
33+
34+
**Explanation:**
35+
36+
The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= power.length <= 10<sup>5</sup></code>
41+
* <code>1 <= power[i] <= 10<sup>9</sup></code>
42+
43+
## Solution
44+
45+
```java
46+
import java.util.Arrays;
47+
48+
public class Solution {
49+
public long maximumTotalDamage(int[] power) {
50+
int maxPower = 0;
51+
for (int p : power) {
52+
if (p > maxPower) {
53+
maxPower = p;
54+
}
55+
}
56+
return (maxPower <= 1_000_000) ? smallPower(power, maxPower) : bigPower(power);
57+
}
58+
59+
private long smallPower(int[] power, int maxPower) {
60+
int[] counts = new int[maxPower + 6];
61+
for (int p : power) {
62+
counts[p]++;
63+
}
64+
long[] dp = new long[maxPower + 6];
65+
dp[1] = counts[1];
66+
dp[2] = Math.max(counts[2] * 2L, dp[1]);
67+
for (int i = 3; i <= maxPower; i++) {
68+
dp[i] = Math.max(counts[i] * i + dp[i - 3], Math.max(dp[i - 1], dp[i - 2]));
69+
}
70+
return dp[maxPower];
71+
}
72+
73+
private long bigPower(int[] power) {
74+
Arrays.sort(power);
75+
int n = power.length;
76+
long[] prevs = new long[4];
77+
int curPower = power[0];
78+
int count = 1;
79+
long result = 0;
80+
for (int i = 1; i <= n; i++) {
81+
int p = (i == n) ? 1_000_000_009 : power[i];
82+
if (p == curPower) {
83+
count++;
84+
} else {
85+
long curVal =
86+
Math.max((long) curPower * count + prevs[3], Math.max(prevs[1], prevs[2]));
87+
int diff = Math.min(p - curPower, prevs.length - 1);
88+
long nextCurVal = (diff == 1) ? 0 : Math.max(prevs[3], Math.max(curVal, prevs[2]));
89+
// Shift the values in prevs[].
90+
int k = prevs.length - 1;
91+
if (diff < prevs.length - 1) {
92+
while (k > diff) {
93+
prevs[k] = prevs[k-- - diff];
94+
}
95+
prevs[k--] = curVal;
96+
}
97+
while (k > 0) {
98+
prevs[k--] = nextCurVal;
99+
}
100+
curPower = p;
101+
count = 1;
102+
}
103+
}
104+
for (long v : prevs) {
105+
if (v > result) {
106+
result = v;
107+
}
108+
}
109+
return result;
110+
}
111+
}
112+
```

0 commit comments

Comments
 (0)