Skip to content

Commit 6706464

Browse files
committed
Added tasks 3586-3594
1 parent c94f639 commit 6706464

File tree

11 files changed

+1227
-3
lines changed
  • src/main/java
    • g3201_3300/s3248_snake_in_matrix
    • g3501_3600
      • s3586_find_covid_recovery_patients
      • s3587_minimum_adjacent_swaps_to_alternate_parity
      • s3588_find_maximum_area_of_a_triangle
      • s3589_count_prime_gap_balanced_subarrays
      • s3590_kth_smallest_path_xor_sum
      • s3591_check_if_any_element_has_prime_frequency
      • s3592_inverse_coin_change
      • s3593_minimum_increments_to_equalize_leaf_paths
      • s3594_minimum_time_to_transport_all_individuals

11 files changed

+1227
-3
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,16 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091-
| 3585 |[Find Weighted Median Node in Tree](src/main/java/g3501_3600/s3585_find_weighted_median_node_in_tree)| Hard | Array, Dynamic_Programming, Tree, Binary_Search, Depth_First_Search | 66 | 94.96
2091+
| 3594 |[Minimum Time to Transport All Individuals](src/main/java/g3501_3600/s3594_minimum_time_to_transport_all_individuals)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Heap_Priority_Queue, Graph, Bitmask, Shortest_Path | 261 | 100.00
2092+
| 3593 |[Minimum Increments to Equalize Leaf Paths](src/main/java/g3501_3600/s3593_minimum_increments_to_equalize_leaf_paths)| Medium | Array, Dynamic_Programming, Depth_First_Search, Tree | 18 | 100.00
2093+
| 3592 |[Inverse Coin Change](src/main/java/g3501_3600/s3592_inverse_coin_change)| Easy | Array, Dynamic_Programming | 1 | 100.00
2094+
| 3591 |[Check if Any Element Has Prime Frequency](src/main/java/g3501_3600/s3591_check_if_any_element_has_prime_frequency)| Easy | Array, Hash_Table, Math, Counting, Number_Theory | 1 | 100.00
2095+
| 3590 |[Kth Smallest Path XOR Sum](src/main/java/g3501_3600/s3590_kth_smallest_path_xor_sum)| Hard | Array, Depth_First_Search, Tree, Ordered_Set | 311 | 100.00
2096+
| 3589 |[Count Prime-Gap Balanced Subarrays](src/main/java/g3501_3600/s3589_count_prime_gap_balanced_subarrays)| Medium | Array, Math, Sliding_Window, Queue, Number_Theory, Monotonic_Queue | 407 | 100.00
2097+
| 3588 |[Find Maximum Area of a Triangle](src/main/java/g3501_3600/s3588_find_maximum_area_of_a_triangle)| Medium | Array, Hash_Table, Math, Greedy, Enumeration, Geometry | 410 | 100.00
2098+
| 3587 |[Minimum Adjacent Swaps to Alternate Parity](src/main/java/g3501_3600/s3587_minimum_adjacent_swaps_to_alternate_parity)| Medium | Array, Greedy | 20 | 100.00
2099+
| 3586 |[Find COVID Recovery Patients](src/main/java/g3501_3600/s3586_find_covid_recovery_patients)| Medium | Database | 471 | 97.17
2100+
| 3585 |[Find Weighted Median Node in Tree](src/main/java/g3501_3600/s3585_find_weighted_median_node_in_tree)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree, Binary_Search | 66 | 94.96
20922101
| 3584 |[Maximum Product of First and Last Elements of a Subsequence](src/main/java/g3501_3600/s3584_maximum_product_of_first_and_last_elements_of_a_subsequence)| Medium | Array, Two_Pointers | 4 | 86.42
20932102
| 3583 |[Count Special Triplets](src/main/java/g3501_3600/s3583_count_special_triplets)| Medium | Array, Hash_Table, Counting | 30 | 99.81
20942103
| 3582 |[Generate Tag for Video Caption](src/main/java/g3501_3600/s3582_generate_tag_for_video_caption)| Easy | String, Simulation | 2 | 99.93

src/main/java/g3201_3300/s3248_snake_in_matrix/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Return the position of the final cell where the snake ends up after executing `c
2121

2222
**Explanation:**
2323

24-
![image](https://leetcode-in-java.github.io/src/main/java/g3201_3300/s3248_snake_in_matrix/image01.png)
24+
![image](https://leetcode-images.github.io/g3201_3300/s3248_snake_in_matrix/image01.png)
2525

2626
**Example 2:**
2727

@@ -31,7 +31,7 @@ Return the position of the final cell where the snake ends up after executing `c
3131

3232
**Explanation:**
3333

34-
![image](https://leetcode-in-java.github.io/src/main/java/g3201_3300/s3248_snake_in_matrix/image02.png)
34+
![image](https://leetcode-images.github.io/g3201_3300/s3248_snake_in_matrix/image02.png)
3535

3636
**Constraints:**
3737

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
## 3586\. Find COVID Recovery Patients
5+
6+
Medium
7+
8+
Table: `patients`
9+
10+
+-------------+---------+
11+
| Column Name | Type |
12+
+-------------+---------+
13+
| patient_id | int |
14+
| patient_name| varchar |
15+
| age | int |
16+
+-------------+---------+
17+
patient_id is the unique identifier for this table.
18+
Each row contains information about a patient.
19+
20+
Table: `covid_tests`
21+
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| test_id | int |
26+
| patient_id | int |
27+
| test_date | date |
28+
| result | varchar |
29+
+-------------+---------+
30+
test_id is the unique identifier for this table.
31+
Each row represents a COVID test result. The result can be Positive, Negative, or Inconclusive.
32+
33+
Write a solution to find patients who have **recovered from COVID** - patients who tested positive but later tested negative.
34+
35+
* A patient is considered recovered if they have **at least one** **Positive** test followed by at least one **Negative** test on a **later date**
36+
* Calculate the **recovery time** in days as the **difference** between the **first positive test** and the **first negative test** after that **positive test**
37+
* **Only include** patients who have both positive and negative test results
38+
39+
Return _the result table ordered by_ `recovery_time` _in **ascending** order, then by_ `patient_name` _in **ascending** order_.
40+
41+
The result format is in the following example.
42+
43+
**Example:**
44+
45+
**Input:**
46+
47+
patients table:
48+
49+
+------------+--------------+-----+
50+
| patient_id | patient_name | age |
51+
+------------+--------------+-----+
52+
| 1 | Alice Smith | 28 |
53+
| 2 | Bob Johnson | 35 |
54+
| 3 | Carol Davis | 42 |
55+
| 4 | David Wilson | 31 |
56+
| 5 | Emma Brown | 29 |
57+
+------------+--------------+-----+
58+
59+
covid\_tests table:
60+
61+
+---------+------------+------------+--------------+
62+
| test_id | patient_id | test_date | result |
63+
|---------|------------|------------|--------------|
64+
| 1 | 1 | 2023-01-15 | Positive |
65+
| 2 | 1 | 2023-01-25 | Negative |
66+
| 3 | 2 | 2023-02-01 | Positive |
67+
| 4 | 2 | 2023-02-05 | Inconclusive |
68+
| 5 | 2 | 2023-02-12 | Negative |
69+
| 6 | 3 | 2023-01-20 | Negative |
70+
| 7 | 3 | 2023-02-10 | Positive |
71+
| 8 | 3 | 2023-02-20 | Negative |
72+
| 9 | 4 | 2023-01-10 | Positive |
73+
| 10 | 4 | 2023-01-18 | Positive |
74+
| 11 | 5 | 2023-02-15 | Negative |
75+
| 12 | 5 | 2023-02-20 | Negative |
76+
+---------+------------+------------+--------------+
77+
78+
**Output:**
79+
80+
+------------+--------------+-----+---------------+
81+
| patient_id | patient_name | age | recovery_time |
82+
|------------|--------------|-----|---------------|
83+
| 1 | Alice Smith | 28 | 10 |
84+
| 3 | Carol Davis | 42 | 10 |
85+
| 2 | Bob Johnson | 35 | 11 |
86+
+------------+--------------+-----+---------------+
87+
88+
**Explanation:**
89+
90+
* **Alice Smith (patient\_id = 1):**
91+
* First positive test: 2023-01-15
92+
* First negative test after positive: 2023-01-25
93+
* Recovery time: 25 - 15 = 10 days
94+
* **Bob Johnson (patient\_id = 2):**
95+
* First positive test: 2023-02-01
96+
* Inconclusive test on 2023-02-05 (ignored for recovery calculation)
97+
* First negative test after positive: 2023-02-12
98+
* Recovery time: 12 - 1 = 11 days
99+
* **Carol Davis (patient\_id = 3):**
100+
* Had negative test on 2023-01-20 (before positive test)
101+
* First positive test: 2023-02-10
102+
* First negative test after positive: 2023-02-20
103+
* Recovery time: 20 - 10 = 10 days
104+
* **Patients not included:**
105+
* David Wilson (patient\_id = 4): Only has positive tests, no negative test after positive
106+
* Emma Brown (patient\_id = 5): Only has negative tests, never tested positive
107+
108+
Output table is ordered by recovery\_time in ascending order, and then by patient\_name in ascending order.
109+
110+
## Solution
111+
112+
```sql
113+
# Write your MySQL query statement below
114+
-- mysql
115+
-- SELECT
116+
-- p.patient_id,
117+
-- p.patient_name,
118+
-- p.age,
119+
-- DATEDIFF(
120+
-- min(neg.test_date),
121+
-- min(pos.test_date)
122+
-- ) AS recovery_time
123+
-- FROM
124+
-- patients p
125+
-- JOIN covid_tests pos ON
126+
-- p.patient_id = pos.patient_id AND pos.result = 'Positive'
127+
-- JOIN covid_tests neg ON
128+
-- p.patient_id = neg.patient_id AND neg.result = 'Negative'
129+
-- WHERE
130+
-- neg.test_date > pos.test_date
131+
-- GROUP BY
132+
-- p.patient_id, p.patient_name, p.age
133+
-- ORDER BY
134+
-- recovery_time, p.patient_name;
135+
select
136+
p.patient_id,
137+
p.patient_name,
138+
p.age,
139+
datediff(
140+
day,
141+
pos.first_pos_date,
142+
neg.first_neg_date
143+
) as recovery_time
144+
from
145+
patients p
146+
join (
147+
select patient_id, min(test_date) as first_pos_date
148+
from covid_tests
149+
where result = 'Positive'
150+
group by patient_id
151+
) pos on p.patient_id = pos.patient_id
152+
join (
153+
select
154+
c1.patient_id,
155+
min(c1.test_date) as first_neg_date
156+
from
157+
covid_tests c1
158+
join (
159+
select patient_id, min(test_date) as first_pos_date
160+
from covid_tests
161+
where result = 'Positive'
162+
group by patient_id
163+
) p2 on c1.patient_id = p2.patient_id
164+
where
165+
c1.result = 'Negative'
166+
and c1.test_date > p2.first_pos_date
167+
group by c1.patient_id
168+
) neg on p.patient_id = neg.patient_id
169+
order by
170+
recovery_time ASC, p.patient_name ASC;
171+
```
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
## 3587\. Minimum Adjacent Swaps to Alternate Parity
5+
6+
Medium
7+
8+
You are given an array `nums` of **distinct** integers.
9+
10+
In one operation, you can swap any two **adjacent** elements in the array.
11+
12+
An arrangement of the array is considered **valid** if the parity of adjacent elements **alternates**, meaning every pair of neighboring elements consists of one even and one odd number.
13+
14+
Return the **minimum** number of adjacent swaps required to transform `nums` into any valid arrangement.
15+
16+
If it is impossible to rearrange `nums` such that no two adjacent elements have the same parity, return `-1`.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [2,4,6,5,7]
21+
22+
**Output:** 3
23+
24+
**Explanation:**
25+
26+
Swapping 5 and 6, the array becomes `[2,4,5,6,7]`
27+
28+
Swapping 5 and 4, the array becomes `[2,5,4,6,7]`
29+
30+
Swapping 6 and 7, the array becomes `[2,5,4,7,6]`. The array is now a valid arrangement. Thus, the answer is 3.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [2,4,5,7]
35+
36+
**Output:** 1
37+
38+
**Explanation:**
39+
40+
By swapping 4 and 5, the array becomes `[2,5,4,7]`, which is a valid arrangement. Thus, the answer is 1.
41+
42+
**Example 3:**
43+
44+
**Input:** nums = [1,2,3]
45+
46+
**Output:** 0
47+
48+
**Explanation:**
49+
50+
The array is already a valid arrangement. Thus, no operations are needed.
51+
52+
**Example 4:**
53+
54+
**Input:** nums = [4,5,6,8]
55+
56+
**Output:** \-1
57+
58+
**Explanation:**
59+
60+
No valid arrangement is possible. Thus, the answer is -1.
61+
62+
**Constraints:**
63+
64+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
65+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
66+
* All elements in `nums` are **distinct**.
67+
68+
## Solution
69+
70+
```java
71+
import java.util.ArrayList;
72+
import java.util.List;
73+
74+
public class Solution {
75+
private static int helper(List<Integer> indices) {
76+
int swaps = 0;
77+
for (int i = 0; i < indices.size(); i++) {
78+
swaps += Math.abs(indices.get(i) - 2 * i);
79+
}
80+
return swaps;
81+
}
82+
83+
public int minSwaps(int[] nums) {
84+
List<Integer> evenIndices = new ArrayList<>();
85+
List<Integer> oddIndices = new ArrayList<>();
86+
for (int i = 0; i < nums.length; i++) {
87+
if (nums[i] % 2 == 0) {
88+
evenIndices.add(i);
89+
} else {
90+
oddIndices.add(i);
91+
}
92+
}
93+
int evenCount = evenIndices.size();
94+
int oddCount = oddIndices.size();
95+
if (Math.abs(evenCount - oddCount) > 1) {
96+
return -1;
97+
}
98+
int ans = Integer.MAX_VALUE;
99+
if (evenCount >= oddCount) {
100+
ans = Math.min(ans, helper(evenIndices));
101+
}
102+
if (oddCount >= evenCount) {
103+
ans = Math.min(ans, helper(oddIndices));
104+
}
105+
return ans;
106+
}
107+
}
108+
```

0 commit comments

Comments
 (0)