Skip to content

Commit a24f7bc

Browse files
ThanhNITjavadev
authored andcommitted
Added tasks 920, 921, 922, 923
1 parent ca0c571 commit a24f7bc

File tree

13 files changed

+348
-0
lines changed

13 files changed

+348
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17301730
|------|----------------|-------------|-------------|----------|---------
17311731
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17321732
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
1733+
| 0923 |[3Sum With Multiplicity](src/main/kotlin/g0901_1000/s0923_3sum_with_multiplicity/Solution.kt)| Medium | Array, Hash_Table, Sorting, Two_Pointers, Counting | 190 | 100.00
1734+
| 0922 |[Sort Array By Parity II](src/main/kotlin/g0901_1000/s0922_sort_array_by_parity_ii/Solution.kt)| Easy | Array, Sorting, Two_Pointers | 257 | 87.50
1735+
| 0921 |[Minimum Add to Make Parentheses Valid](src/main/kotlin/g0901_1000/s0921_minimum_add_to_make_parentheses_valid/Solution.kt)| Medium | String, Greedy, Stack | 131 | 92.59
1736+
| 0920 |[Number of Music Playlists](src/main/kotlin/g0901_1000/s0920_number_of_music_playlists/Solution.kt)| Hard | Dynamic_Programming, Math, Combinatorics | 136 | 100.00
17331737
| 0919 |[Complete Binary Tree Inserter](src/main/kotlin/g0901_1000/s0919_complete_binary_tree_inserter/CBTInserter.kt)| Medium | Breadth_First_Search, Tree, Binary_Tree, Design | 225 | 100.00
17341738
| 0918 |[Maximum Sum Circular Subarray](src/main/kotlin/g0901_1000/s0918_maximum_sum_circular_subarray/Solution.kt)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue, Dynamic_Programming_I_Day_5 | 339 | 86.96
17351739
| 0917 |[Reverse Only Letters](src/main/kotlin/g0901_1000/s0917_reverse_only_letters/Solution.kt)| Easy | String, Two_Pointers | 126 | 100.00
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0901_1000.s0920_number_of_music_playlists
2+
3+
// #Hard #Dynamic_Programming #Math #Combinatorics
4+
// #2023_04_17_Time_136_ms_(100.00%)_Space_35.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun numMusicPlaylists(n: Int, l: Int, k: Int): Int {
8+
val dp = Array(l) { LongArray(n + 1) }
9+
for (i in 0 until l) {
10+
dp[i].fill(-1)
11+
}
12+
return helper(0, l, 0, n, k, dp).toInt()
13+
}
14+
15+
private fun helper(songNumber: Int, l: Int, usedSong: Int, n: Int, k: Int, dp: Array<LongArray>): Long {
16+
if (songNumber == l) {
17+
return if (usedSong == n) 1 else 0
18+
}
19+
if (dp[songNumber][usedSong] != -1L) {
20+
return dp[songNumber][usedSong]
21+
}
22+
val ans: Long = if (songNumber < k) {
23+
(n - usedSong) * helper(songNumber + 1, l, usedSong + 1, n, k, dp)
24+
} else if (usedSong == n) {
25+
(usedSong - k) * helper(songNumber + 1, l, usedSong, n, k, dp)
26+
} else {
27+
(
28+
(n - usedSong) * helper(songNumber + 1, l, usedSong + 1, n, k, dp) +
29+
(usedSong - k) * helper(songNumber + 1, l, usedSong, n, k, dp)
30+
)
31+
}
32+
val mod = 1e9.toInt() + 7
33+
dp[songNumber][usedSong] = ans % mod
34+
return ans % mod
35+
}
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
920\. Number of Music Playlists
2+
3+
Hard
4+
5+
Your music player contains `n` different songs. You want to listen to `goal` songs (not necessarily different) during your trip. To avoid boredom, you will create a playlist so that:
6+
7+
* Every song is played **at least once**.
8+
* A song can only be played again only if `k` other songs have been played.
9+
10+
Given `n`, `goal`, and `k`, return _the number of possible playlists that you can create_. Since the answer can be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 3, goal = 3, k = 1
15+
16+
**Output:** 6
17+
18+
**Explanation:** There are 6 possible playlists: [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], and [3, 2, 1].
19+
20+
**Example 2:**
21+
22+
**Input:** n = 2, goal = 3, k = 0
23+
24+
**Output:** 6
25+
26+
**Explanation:** There are 6 possible playlists: [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], and [1, 2, 2].
27+
28+
**Example 3:**
29+
30+
**Input:** n = 2, goal = 3, k = 1
31+
32+
**Output:** 2
33+
34+
**Explanation:** There are 2 possible playlists: [1, 2, 1] and [2, 1, 2].
35+
36+
**Constraints:**
37+
38+
* `0 <= k < n <= goal <= 100`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0901_1000.s0921_minimum_add_to_make_parentheses_valid
2+
3+
// #Medium #String #Greedy #Stack #2023_04_17_Time_131_ms_(92.59%)_Space_34.3_MB_(14.81%)
4+
5+
import java.util.Deque
6+
import java.util.LinkedList
7+
8+
class Solution {
9+
fun minAddToMakeValid(s: String): Int {
10+
val stack: Deque<Char> = LinkedList()
11+
for (c in s.toCharArray()) {
12+
if (c == ')') {
13+
if (!stack.isEmpty() && stack.peek() == '(') {
14+
stack.pop()
15+
} else {
16+
stack.push(c)
17+
}
18+
} else {
19+
stack.push(c)
20+
}
21+
}
22+
return stack.size
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
921\. Minimum Add to Make Parentheses Valid
2+
3+
Medium
4+
5+
A parentheses string is valid if and only if:
6+
7+
* It is the empty string,
8+
* It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are valid strings, or
9+
* It can be written as `(A)`, where `A` is a valid string.
10+
11+
You are given a parentheses string `s`. In one move, you can insert a parenthesis at any position of the string.
12+
13+
* For example, if `s = "()))"`, you can insert an opening parenthesis to be <code>"(**(**)))"</code> or a closing parenthesis to be <code>"())**)**)"</code>.
14+
15+
Return _the minimum number of moves required to make_ `s` _valid_.
16+
17+
**Example 1:**
18+
19+
**Input:** s = "())"
20+
21+
**Output:** 1
22+
23+
**Example 2:**
24+
25+
**Input:** s = "((("
26+
27+
**Output:** 3
28+
29+
**Constraints:**
30+
31+
* `1 <= s.length <= 1000`
32+
* `s[i]` is either `'('` or `')'`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0901_1000.s0922_sort_array_by_parity_ii
2+
3+
// #Easy #Array #Sorting #Two_Pointers #2023_04_17_Time_257_ms_(87.50%)_Space_39_MB_(87.50%)
4+
5+
class Solution {
6+
fun sortArrayByParityII(nums: IntArray): IntArray {
7+
var i = 0
8+
var j = 1
9+
while (i < nums.size - 1 && j < nums.size) {
10+
if (nums[i] % 2 != 0 && nums[j] % 2 == 0) {
11+
val tmp = nums[i]
12+
nums[i] = nums[j]
13+
nums[j] = tmp
14+
i += 2
15+
j += 2
16+
}
17+
while (i < nums.size - 1 && nums[i] % 2 == 0) {
18+
i += 2
19+
}
20+
while (j < nums.size && nums[j] % 2 != 0) {
21+
j += 2
22+
}
23+
}
24+
return nums
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
922\. Sort Array By Parity II
2+
3+
Easy
4+
5+
Given an array of integers `nums`, half of the integers in `nums` are **odd**, and the other half are **even**.
6+
7+
Sort the array so that whenever `nums[i]` is odd, `i` is **odd**, and whenever `nums[i]` is even, `i` is **even**.
8+
9+
Return _any answer array that satisfies this condition_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [4,2,5,7]
14+
15+
**Output:** [4,5,2,7]
16+
17+
**Explanation:** [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [2,3]
22+
23+
**Output:** [2,3]
24+
25+
**Constraints:**
26+
27+
* <code>2 <= nums.length <= 2 * 10<sup>4</sup></code>
28+
* `nums.length` is even.
29+
* Half of the integers in `nums` are even.
30+
* `0 <= nums[i] <= 1000`
31+
32+
**Follow Up:** Could you solve it in-place?
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0901_1000.s0923_3sum_with_multiplicity
2+
3+
// #Medium #Array #Hash_Table #Sorting #Two_Pointers #Counting
4+
// #2023_04_17_Time_190_ms_(100.00%)_Space_36.4_MB_(100.00%)
5+
6+
class Solution {
7+
fun threeSumMulti(arr: IntArray, target: Int): Int {
8+
var answer = 0
9+
val countRight = IntArray(MAX + 1)
10+
for (num in arr) {
11+
++countRight[num]
12+
}
13+
val countLeft = IntArray(MAX + 1)
14+
for (j in 0 until arr.size - 1) {
15+
--countRight[arr[j]]
16+
val remains = target - arr[j]
17+
if (remains <= 2 * MAX) {
18+
for (v in 0..remains.coerceAtMost(MAX)) {
19+
if (remains - v <= MAX) {
20+
val count = countRight[v] * countLeft[remains - v]
21+
if (count > 0) {
22+
answer = (answer + count) % MOD
23+
}
24+
}
25+
}
26+
}
27+
++countLeft[arr[j]]
28+
}
29+
return answer
30+
}
31+
32+
companion object {
33+
private const val MOD = 1e9.toInt() + 7
34+
private const val MAX = 100
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
923\. 3Sum With Multiplicity
2+
3+
Medium
4+
5+
Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`.
6+
7+
As the answer can be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
**Example 1:**
10+
11+
**Input:** arr = [1,1,2,2,3,3,4,4,5,5], target = 8
12+
13+
**Output:** 20
14+
15+
**Explanation:**
16+
17+
Enumerating by the values (arr[i], arr[j], arr[k]):
18+
(1, 2, 5) occurs 8 times;
19+
(1, 3, 4) occurs 8 times;
20+
(2, 2, 4) occurs 2 times;
21+
(2, 3, 3) occurs 2 times.
22+
23+
**Example 2:**
24+
25+
**Input:** arr = [1,1,2,2,2,2], target = 5
26+
27+
**Output:** 12
28+
29+
**Explanation:**
30+
31+
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
32+
33+
We choose one 1 from [1,1] in 2 ways,
34+
35+
and two 2s from [2,2,2,2] in 6 ways.
36+
37+
**Constraints:**
38+
39+
* `3 <= arr.length <= 3000`
40+
* `0 <= arr[i] <= 100`
41+
* `0 <= target <= 300`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0901_1000.s0920_number_of_music_playlists
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun numMusicPlaylists() {
10+
assertThat(Solution().numMusicPlaylists(3, 3, 1), equalTo(6))
11+
}
12+
13+
@Test
14+
fun numMusicPlaylists2() {
15+
assertThat(Solution().numMusicPlaylists(2, 3, 0), equalTo(6))
16+
}
17+
18+
@Test
19+
fun numMusicPlaylists3() {
20+
assertThat(Solution().numMusicPlaylists(2, 3, 1), equalTo(2))
21+
}
22+
}

0 commit comments

Comments
 (0)