Skip to content

Commit 12c8fae

Browse files
committed
Added tasks 3627-3642
1 parent b988886 commit 12c8fae

File tree

15 files changed

+1429
-7
lines changed
  • src/main/kotlin
    • g0101_0200/s0133_clone_graph
    • g3601_3700
      • s3627_maximum_median_sum_of_subsequences_of_size_3
      • s3628_maximum_number_of_subsequences_after_one_inserting
      • s3629_minimum_jumps_to_reach_end_via_prime_teleportation
      • s3630_partition_array_for_maximum_xor_and_and
      • s3633_earliest_finish_time_for_land_and_water_rides_i
      • s3634_minimum_removals_to_balance_array
      • s3635_earliest_finish_time_for_land_and_water_rides_ii
      • s3636_threshold_majority_queries
      • s3637_trionic_array_i
      • s3638_maximum_balanced_shipments
      • s3639_minimum_time_to_activate_string
      • s3640_trionic_array_ii
      • s3642_find_books_with_polarized_opinions

15 files changed

+1429
-7
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@
769769
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
770770
|-|-|-|-|-|-
771771
| 0200 |[Number of Islands](src/main/kotlin/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 252 | 95.41
772-
| 0133 |[Clone Graph](src/main/kotlin/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 351 | 60.91
772+
| 0133 |[Clone Graph](src/main/kotlin/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 133 | 88.96
773773
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 319 | 100.00
774774

775775
#### Udemy Dynamic Programming
@@ -973,7 +973,7 @@
973973
|-|-|-|-|-|-
974974
| 0200 |[Number of Islands](src/main/kotlin/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 252 | 95.41
975975
| 0130 |[Surrounded Regions](src/main/kotlin/g0101_0200/s0130_surrounded_regions)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 355 | 84.42
976-
| 0133 |[Clone Graph](src/main/kotlin/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 351 | 60.91
976+
| 0133 |[Clone Graph](src/main/kotlin/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 133 | 88.96
977977
| 0399 |[Evaluate Division](src/main/kotlin/g0301_0400/s0399_evaluate_division)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path, LeetCode_75_Graphs/DFS | 183 | 91.49
978978
| 0207 |[Course Schedule](src/main/kotlin/g0201_0300/s0207_course_schedule)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 183 | 92.07
979979
| 0210 |[Course Schedule II](src/main/kotlin/g0201_0300/s0210_course_schedule_ii)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 266 | 96.32
@@ -2088,6 +2088,19 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3642 |[Find Books with Polarized Opinions](src/main/kotlin/g3601_3700/s3642_find_books_with_polarized_opinions)| Easy | Database | 490 | 100.00
2092+
| 3640 |[Trionic Array II](src/main/kotlin/g3601_3700/s3640_trionic_array_ii)| Hard | Weekly_Contest_461 | 7 | 100.00
2093+
| 3639 |[Minimum Time to Activate String](src/main/kotlin/g3601_3700/s3639_minimum_time_to_activate_string)| Medium | Weekly_Contest_461 | 160 | 100.00
2094+
| 3638 |[Maximum Balanced Shipments](src/main/kotlin/g3601_3700/s3638_maximum_balanced_shipments)| Medium | Weekly_Contest_461 | 5 | 100.00
2095+
| 3637 |[Trionic Array I](src/main/kotlin/g3601_3700/s3637_trionic_array_i)| Easy | Weekly_Contest_461 | 1 | 100.00
2096+
| 3636 |[Threshold Majority Queries](src/main/kotlin/g3601_3700/s3636_threshold_majority_queries)| Hard | Biweekly_Contest_162 | 848 | 100.00
2097+
| 3635 |[Earliest Finish Time for Land and Water Rides II](src/main/kotlin/g3601_3700/s3635_earliest_finish_time_for_land_and_water_rides_ii)| Medium | Biweekly_Contest_162 | 5 | 100.00
2098+
| 3634 |[Minimum Removals to Balance Array](src/main/kotlin/g3601_3700/s3634_minimum_removals_to_balance_array)| Medium | Biweekly_Contest_162 | 43 | 100.00
2099+
| 3633 |[Earliest Finish Time for Land and Water Rides I](src/main/kotlin/g3601_3700/s3633_earliest_finish_time_for_land_and_water_rides_i)| Easy | Biweekly_Contest_162 | 15 | 100.00
2100+
| 3630 |[Partition Array for Maximum XOR and AND](src/main/kotlin/g3601_3700/s3630_partition_array_for_maximum_xor_and_and)| Hard | Array, Math, Greedy, Enumeration, Weekly_Contest_460 | 57 | 100.00
2101+
| 3629 |[Minimum Jumps to Reach End via Prime Teleportation](src/main/kotlin/g3601_3700/s3629_minimum_jumps_to_reach_end_via_prime_teleportation)| Medium | Weekly_Contest_460 | 406 | 100.00
2102+
| 3628 |[Maximum Number of Subsequences After One Inserting](src/main/kotlin/g3601_3700/s3628_maximum_number_of_subsequences_after_one_inserting)| Medium | Weekly_Contest_460 | 13 | 100.00
2103+
| 3627 |[Maximum Median Sum of Subsequences of Size 3](src/main/kotlin/g3601_3700/s3627_maximum_median_sum_of_subsequences_of_size_3)| Medium | Weekly_Contest_460 | 46 | 91.67
20912104
| 3626 |[Find Stores with Inventory Imbalance](src/main/kotlin/g3601_3700/s3626_find_stores_with_inventory_imbalance)| Medium | Database | 516 | 100.00
20922105
| 3625 |[Count Number of Trapezoids II](src/main/kotlin/g3601_3700/s3625_count_number_of_trapezoids_ii)| Hard | Array, Hash_Table, Math, Geometry, Weekly_Contest_459 | 377 | 100.00
20932106
| 3624 |[Number of Integers With Popcount-Depth Equal to K II](src/main/kotlin/g3601_3700/s3624_number_of_integers_with_popcount_depth_equal_to_k_ii)| Hard | Array, Segment_Tree, Weekly_Contest_459 | 38 | 100.00
@@ -4856,7 +4869,7 @@
48564869
| 0136 |[Single Number](src/main/kotlin/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Top_Interview_150_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 344 | 83.63
48574870
| 0135 |[Candy](src/main/kotlin/g0101_0200/s0135_candy)| Hard | Array, Greedy, Top_Interview_150_Array/String | 466 | 58.33
48584871
| 0134 |[Gas Station](src/main/kotlin/g0101_0200/s0134_gas_station)| Medium | Top_Interview_Questions, Array, Greedy, Top_Interview_150_Array/String | 545 | 70.18
4859-
| 0133 |[Clone Graph](src/main/kotlin/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Udemy_Graph, Top_Interview_150_Graph_General | 351 | 60.91
4872+
| 0133 |[Clone Graph](src/main/kotlin/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Udemy_Graph, Top_Interview_150_Graph_General | 133 | 88.96
48604873
| 0132 |[Palindrome Partitioning II](src/main/kotlin/g0101_0200/s0132_palindrome_partitioning_ii)| Hard | String, Dynamic_Programming | 290 | 100.00
48614874
| 0131 |[Palindrome Partitioning](src/main/kotlin/g0101_0200/s0131_palindrome_partitioning)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 820 | 87.27
48624875
| 0130 |[Surrounded Regions](src/main/kotlin/g0101_0200/s0130_surrounded_regions)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search, Top_Interview_150_Graph_General | 355 | 84.42

src/main/kotlin/g0101_0200/s0133_clone_graph/readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ The given node will always be the first node with `val = 1`. You must return the
6868
## Solution
6969

7070
```kotlin
71-
import com_github_leetcode.neighbors.Node
71+
import com_github_leetcode.Node
7272

7373
/*
7474
* Definition for a Node.
@@ -89,11 +89,10 @@ class Solution {
8989
}
9090
val newNode = Node(node.`val`)
9191
processedNodes[node] = newNode
92-
newNode.neighbors = ArrayList()
9392
for (neighbor in node.neighbors) {
94-
val clonedNeighbor: Node? = cloneGraph(neighbor, processedNodes)
93+
val clonedNeighbor = cloneGraph(neighbor, processedNodes)
9594
if (clonedNeighbor != null) {
96-
newNode.neighbors.add(clonedNeighbor)
95+
(newNode.neighbors as ArrayList).add(clonedNeighbor)
9796
}
9897
}
9998
return newNode
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3627\. Maximum Median Sum of Subsequences of Size 3
5+
6+
Medium
7+
8+
You are given an integer array `nums` with a length divisible by 3.
9+
10+
You want to make the array empty in steps. In each step, you can select any three elements from the array, compute their **median**, and remove the selected elements from the array.
11+
12+
The **median** of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order.
13+
14+
Return the **maximum** possible sum of the medians computed from the selected elements.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,1,3,2,1,3]
19+
20+
**Output:** 5
21+
22+
**Explanation:**
23+
24+
* In the first step, select elements at indices 2, 4, and 5, which have a median 3. After removing these elements, `nums` becomes `[2, 1, 2]`.
25+
* In the second step, select elements at indices 0, 1, and 2, which have a median 2. After removing these elements, `nums` becomes empty.
26+
27+
Hence, the sum of the medians is `3 + 2 = 5`.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,1,10,10,10,10]
32+
33+
**Output:** 20
34+
35+
**Explanation:**
36+
37+
* In the first step, select elements at indices 0, 2, and 3, which have a median 10. After removing these elements, `nums` becomes `[1, 10, 10]`.
38+
* In the second step, select elements at indices 0, 1, and 2, which have a median 10. After removing these elements, `nums` becomes empty.
39+
40+
Hence, the sum of the medians is `10 + 10 = 20`.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 5 * 10<sup>5</sup></code>
45+
* `nums.length % 3 == 0`
46+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
47+
48+
## Solution
49+
50+
```kotlin
51+
class Solution {
52+
fun maximumMedianSum(nums: IntArray): Long {
53+
nums.sort()
54+
var i = 0
55+
var j = nums.size
56+
var sum = 0L
57+
while (i < j) {
58+
sum += nums[j - 2]
59+
j = j - 2
60+
i++
61+
}
62+
return sum
63+
}
64+
}
65+
```
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3628\. Maximum Number of Subsequences After One Inserting
5+
6+
Medium
7+
8+
You are given a string `s` consisting of uppercase English letters.
9+
10+
You are allowed to insert **at most one** uppercase English letter at **any** position (including the beginning or end) of the string.
11+
12+
Return the **maximum** number of `"LCT"` subsequences that can be formed in the resulting string after **at most one insertion**.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "LMCT"
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
We can insert a `"L"` at the beginning of the string s to make `"LLMCT"`, which has 2 subsequences, at indices [0, 3, 4] and [1, 3, 4].
23+
24+
**Example 2:**
25+
26+
**Input:** s = "LCCT"
27+
28+
**Output:** 4
29+
30+
**Explanation:**
31+
32+
We can insert a `"L"` at the beginning of the string s to make `"LLCCT"`, which has 4 subsequences, at indices [0, 2, 4], [0, 3, 4], [1, 2, 4] and [1, 3, 4].
33+
34+
**Example 3:**
35+
36+
**Input:** s = "L"
37+
38+
**Output:** 0
39+
40+
**Explanation:**
41+
42+
Since it is not possible to obtain the subsequence `"LCT"` by inserting a single letter, the result is 0.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= s.length <= 10<sup>5</sup></code>
47+
* `s` consists of uppercase English letters.
48+
49+
## Solution
50+
51+
```kotlin
52+
class Solution {
53+
fun numOfSubsequences(s: String): Long {
54+
var tc: Long = 0
55+
val chs = s.toCharArray()
56+
for (c in chs) {
57+
tc += (if (c == 'T') 1 else 0).toLong()
58+
}
59+
var ls: Long = 0
60+
var cs: Long = 0
61+
var lcf: Long = 0
62+
var ctf: Long = 0
63+
var lct: Long = 0
64+
var ocg: Long = 0
65+
var tp: Long = 0
66+
for (curr in chs) {
67+
val rt = tc - tp
68+
val cg = ls * rt
69+
ocg = if (cg > ocg) cg else ocg
70+
if (curr == 'L') {
71+
ls++
72+
} else {
73+
if (curr == 'C') {
74+
cs++
75+
lcf += ls
76+
} else {
77+
if (curr == 'T') {
78+
lct += lcf
79+
ctf += cs
80+
tp++
81+
}
82+
}
83+
}
84+
}
85+
val fcg = ls * (tc - tp)
86+
ocg = if (fcg > ocg) fcg else ocg
87+
var maxi: Long = 0
88+
val bo = longArrayOf(lcf, ctf, ocg)
89+
for (op in bo) {
90+
maxi = if (op > maxi) op else maxi
91+
}
92+
return lct + maxi
93+
}
94+
}
95+
```
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3629\. Minimum Jumps to Reach End via Prime Teleportation
5+
6+
Medium
7+
8+
You are given an integer array `nums` of length `n`.
9+
10+
You start at index 0, and your goal is to reach index `n - 1`.
11+
12+
From any index `i`, you may perform one of the following operations:
13+
14+
* **Adjacent Step**: Jump to index `i + 1` or `i - 1`, if the index is within bounds.
15+
* **Prime Teleportation**: If `nums[i]` is a prime number `p`, you may instantly jump to any index `j != i` such that `nums[j] % p == 0`.
16+
17+
Return the **minimum** number of jumps required to reach index `n - 1`.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [1,2,4,6]
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
One optimal sequence of jumps is:
28+
29+
* Start at index `i = 0`. Take an adjacent step to index 1.
30+
* At index `i = 1`, `nums[1] = 2` is a prime number. Therefore, we teleport to index `i = 3` as `nums[3] = 6` is divisible by 2.
31+
32+
Thus, the answer is 2.
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [2,3,4,7,9]
37+
38+
**Output:** 2
39+
40+
**Explanation:**
41+
42+
One optimal sequence of jumps is:
43+
44+
* Start at index `i = 0`. Take an adjacent step to index `i = 1`.
45+
* At index `i = 1`, `nums[1] = 3` is a prime number. Therefore, we teleport to index `i = 4` since `nums[4] = 9` is divisible by 3.
46+
47+
Thus, the answer is 2.
48+
49+
**Example 3:**
50+
51+
**Input:** nums = [4,6,5,8]
52+
53+
**Output:** 3
54+
55+
**Explanation:**
56+
57+
* Since no teleportation is possible, we move through `0 → 1 → 2 → 3`. Thus, the answer is 3.
58+
59+
**Constraints:**
60+
61+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
62+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
63+
64+
## Solution
65+
66+
```kotlin
67+
import java.util.ArrayDeque
68+
import kotlin.math.max
69+
70+
class Solution {
71+
fun minJumps(nums: IntArray): Int {
72+
val n = nums.size
73+
if (n == 1) {
74+
return 0
75+
}
76+
var maxVal = 0
77+
for (v in nums) {
78+
maxVal = max(maxVal, v)
79+
}
80+
val isPrime = sieve(maxVal)
81+
val posOfValue: Array<ArrayList<Int>> = Array<ArrayList<Int>>(maxVal + 1) { ArrayList<Int>() }
82+
for (i in 0..<n) {
83+
val v = nums[i]
84+
posOfValue[v].add(i)
85+
}
86+
val primeProcessed = BooleanArray(maxVal + 1)
87+
val dist = IntArray(n)
88+
dist.fill(-1)
89+
val q = ArrayDeque<Int>()
90+
q.add(0)
91+
dist[0] = 0
92+
while (q.isNotEmpty()) {
93+
val i: Int = q.poll()!!
94+
val d = dist[i]
95+
if (i == n - 1) {
96+
return d
97+
}
98+
if (i + 1 < n && dist[i + 1] == -1) {
99+
dist[i + 1] = d + 1
100+
q.add(i + 1)
101+
}
102+
if (i - 1 >= 0 && dist[i - 1] == -1) {
103+
dist[i - 1] = d + 1
104+
q.add(i - 1)
105+
}
106+
val v = nums[i]
107+
if (v <= maxVal && isPrime[v] && !primeProcessed[v]) {
108+
var mult = v
109+
while (mult <= maxVal) {
110+
val list = posOfValue[mult]
111+
for (idx in list) {
112+
if (dist[idx] == -1) {
113+
dist[idx] = d + 1
114+
q.add(idx)
115+
}
116+
}
117+
mult += v
118+
}
119+
primeProcessed[v] = true
120+
}
121+
}
122+
return -1
123+
}
124+
125+
private fun sieve(n: Int): BooleanArray {
126+
val prime = BooleanArray(n + 1)
127+
if (n >= 2) {
128+
prime.fill(true)
129+
}
130+
if (n >= 0) {
131+
prime[0] = false
132+
}
133+
if (n >= 1) {
134+
prime[1] = false
135+
}
136+
var i = 2
137+
while (i.toLong() * i <= n) {
138+
if (prime[i]) {
139+
var j = i * i
140+
while (j <= n) {
141+
prime[j] = false
142+
j += i
143+
}
144+
}
145+
i++
146+
}
147+
return prime
148+
}
149+
}
150+
```

0 commit comments

Comments
 (0)