Skip to content

Commit 39c628e

Browse files
authored
Added tasks 375, 376, 377, 380.
1 parent b83206e commit 39c628e

File tree

13 files changed

+449
-0
lines changed

13 files changed

+449
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
276276
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
277277
|-|-|-|-|-|-
278278
| 0300 |[Longest Increasing Subsequence](src.save/main/kotlin/g0201_0300/s0300_longest_increasing_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 318 | 82.28
279+
| 0376 |[Wiggle Subsequence](src/main/kotlin/g0301_0400/s0376_wiggle_subsequence/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy | 162 | 88.89
279280

280281
#### Day 19
281282

@@ -294,6 +295,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
294295

295296
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
296297
|-|-|-|-|-|-
298+
| 0377 |[Combination Sum IV](src/main/kotlin/g0301_0400/s0377_combination_sum_iv/Solution.kt)| Medium | Array, Dynamic_Programming | 217 | 72.41
297299
| 0343 |[Integer Break](src/main/kotlin/g0301_0400/s0343_integer_break/Solution.kt)| Medium | Dynamic_Programming, Math | 218 | 63.89
298300
| 0279 |[Perfect Squares](src.save/main/kotlin/g0201_0300/s0279_perfect_squares/Solution.kt)| Medium | Top_Interview_Questions, Dynamic_Programming, Math, Breadth_First_Search | 176 | 98.80
299301

@@ -487,6 +489,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
487489

488490
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
489491
|-|-|-|-|-|-
492+
| 0380 |[Insert Delete GetRandom O(1)](src/main/kotlin/g0301_0400/s0380_insert_delete_getrandom_o1/RandomizedSet.kt)| Medium | Top_Interview_Questions, Array, Hash_Table, Math, Design, Randomized | 1326 | 68.23
490493

491494
### Graph Theory I
492495

@@ -1607,7 +1610,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
16071610
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
16081611
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
16091612
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 224 | 64.86
1613+
| 0380 |[Insert Delete GetRandom O(1)](src/main/kotlin/g0301_0400/s0380_insert_delete_getrandom_o1/RandomizedSet.kt)| Medium | Top_Interview_Questions, Array, Hash_Table, Math, Design, Randomized, Programming_Skills_II_Day_20 | 1326 | 68.23
16101614
| 0378 |[Kth Smallest Element in a Sorted Matrix](src/main/kotlin/g0301_0400/s0378_kth_smallest_element_in_a_sorted_matrix/Solution.kt)| Medium | Top_Interview_Questions, Array, Sorting, Binary_Search, Matrix, Heap_Priority_Queue | 522 | 59.78
1615+
| 0377 |[Combination Sum IV](src/main/kotlin/g0301_0400/s0377_combination_sum_iv/Solution.kt)| Medium | Array, Dynamic_Programming, Dynamic_Programming_I_Day_21 | 217 | 72.41
1616+
| 0376 |[Wiggle Subsequence](src/main/kotlin/g0301_0400/s0376_wiggle_subsequence/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy, Dynamic_Programming_I_Day_18 | 162 | 88.89
1617+
| 0375 |[Guess Number Higher or Lower II](src/main/kotlin/g0301_0400/s0375_guess_number_higher_or_lower_ii/Solution.kt)| Medium | Dynamic_Programming, Math, Game_Theory | 235 | 75.00
16111618
| 0374 |[Guess Number Higher or Lower](src/main/kotlin/g0301_0400/s0374_guess_number_higher_or_lower/Solution.kt)| Easy | Binary_Search, Interactive, Binary_Search_I_Day_1 | 134 | 94.19
16121619
| 0373 |[Find K Pairs with Smallest Sums](src/main/kotlin/g0301_0400/s0373_find_k_pairs_with_smallest_sums/Solution.kt)| Medium | Array, Heap_Priority_Queue | 1809 | 80.95
16131620
| 0372 |[Super Pow](src/main/kotlin/g0301_0400/s0372_super_pow/Solution.kt)| Medium | Math, Divide_and_Conquer | 196 | 100.00
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g0301_0400.s0375_guess_number_higher_or_lower_ii
2+
3+
// #Medium #Dynamic_Programming #Math #Game_Theory
4+
// #2022_11_22_Time_235_ms_(75.00%)_Space_35.1_MB_(50.00%)
5+
6+
class Solution {
7+
lateinit var matrix: Array<IntArray>
8+
fun getMoneyAmount(n: Int): Int {
9+
matrix = Array(n + 1) { IntArray(n + 1) }
10+
return get(1, n)
11+
}
12+
13+
private operator fun get(min: Int, max: Int): Int {
14+
if (max - min < 3) {
15+
return if (max - min <= 0) 0 else max - 1
16+
}
17+
if (matrix[min][max] != 0) {
18+
return matrix[min][max]
19+
}
20+
var select = max - 3
21+
var minRes = Int.MAX_VALUE
22+
var res: Int
23+
val end = min + (max - min shr 1) - 1
24+
var cnt = 0
25+
while (true) {
26+
res = select + Math.max(get(min, select - 1), get(select + 1, max))
27+
if (res > minRes) {
28+
cnt++
29+
if (cnt >= 3) {
30+
break
31+
}
32+
}
33+
if (res < minRes) {
34+
minRes = res
35+
}
36+
select--
37+
if (select <= end) {
38+
break
39+
}
40+
}
41+
matrix[min][max] = minRes
42+
return minRes
43+
}
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
375\. Guess Number Higher or Lower II
2+
3+
Medium
4+
5+
We are playing the Guessing Game. The game will work as follows:
6+
7+
1. I pick a number between `1` and `n`.
8+
2. You guess a number.
9+
3. If you guess the right number, **you win the game**.
10+
4. If you guess the wrong number, then I will tell you whether the number I picked is **higher or lower**, and you will continue guessing.
11+
5. Every time you guess a wrong number `x`, you will pay `x` dollars. If you run out of money, **you lose the game**.
12+
13+
Given a particular `n`, return _the minimum amount of money you need to **guarantee a win regardless of what number I pick**_.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/09/10/graph.png)
18+
19+
**Input:** n = 10
20+
21+
**Output:** 16
22+
23+
**Explanation:**
24+
25+
The winning strategy is as follows:
26+
- The range is [1,10]. Guess 7.
27+
- If this is my number, your total is $0. Otherwise, you pay $7.
28+
- If my number is higher, the range is [8,10]. Guess 9.
29+
- If this is my number, your total is $7. Otherwise, you pay $9.
30+
- If my number is higher, it must be 10. Guess 10. Your total is $7 + $9 = $16.
31+
- If my number is lower, it must be 8. Guess 8. Your total is $7 + $9 = $16.
32+
- If my number is lower, the range is [1,6]. Guess 3.
33+
- If this is my number, your total is $7. Otherwise, you pay $3.
34+
- If my number is higher, the range is [4,6]. Guess 5.
35+
- If this is my number, your total is $7 + $3 = $10. Otherwise, you pay $5.
36+
- If my number is higher, it must be 6. Guess 6. Your total is $7 + $3 + $5 = $15.
37+
- If my number is lower, it must be 4. Guess 4. Your total is $7 + $3 + $5 = $15.
38+
- If my number is lower, the range is [1,2]. Guess 1.
39+
- If this is my number, your total is $7 + $3 = $10. Otherwise, you pay $1.
40+
- If my number is higher, it must be 2. Guess 2. Your total is $7 + $3 + $1 = $11.
41+
The worst case in all these scenarios is that you pay $16. Hence, you only need $16 to guarantee a win.
42+
43+
**Example 2:**
44+
45+
**Input:** n = 1
46+
47+
**Output:** 0
48+
49+
**Explanation:** There is only one possible number, so you can guess 1 and not have to pay anything.
50+
51+
**Example 3:**
52+
53+
**Input:** n = 2
54+
55+
**Output:** 1
56+
57+
**Explanation:**
58+
59+
There are two possible numbers, 1 and 2.
60+
- Guess 1.
61+
- If this is my number, your total is $0. Otherwise, you pay $1.
62+
- If my number is higher, it must be 2. Guess 2. Your total is $1.
63+
The worst case is that you pay $1.
64+
65+
**Constraints:**
66+
67+
* `1 <= n <= 200`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0301_0400.s0376_wiggle_subsequence
2+
3+
// #Medium #Array #Dynamic_Programming #Greedy #Dynamic_Programming_I_Day_18
4+
// #2022_11_22_Time_162_ms_(88.89%)_Space_33.6_MB_(100.00%)
5+
6+
class Solution {
7+
fun wiggleMaxLength(nums: IntArray): Int {
8+
var lt = 1
9+
var gt = 1
10+
for (i in 1 until nums.size) {
11+
if (nums[i - 1] < nums[i]) {
12+
lt = gt + 1
13+
} else if (nums[i - 1] > nums[i]) {
14+
gt = lt + 1
15+
}
16+
}
17+
return Math.max(lt, gt)
18+
}
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
376\. Wiggle Subsequence
2+
3+
Medium
4+
5+
A **wiggle sequence** is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.
6+
7+
* For example, `[1, 7, 4, 9, 2, 5]` is a **wiggle sequence** because the differences `(6, -3, 5, -7, 3)` alternate between positive and negative.
8+
* In contrast, `[1, 4, 7, 2, 5]` and `[1, 7, 4, 5, 5]` are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.
9+
10+
A **subsequence** is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
11+
12+
Given an integer array `nums`, return _the length of the longest **wiggle subsequence** of_ `nums`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,7,4,9,2,5]
17+
18+
**Output:** 6
19+
20+
**Explanation:** The entire sequence is a wiggle sequence with differences (6, -3, 5, -7, 3).
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [1,17,5,10,13,15,10,5,16,8]
25+
26+
**Output:** 7
27+
28+
**Explanation:** There are several subsequences that achieve this length. One is [1, 17, 10, 13, 10, 16, 8] with differences (16, -7, 3, -3, 6, -8).
29+
30+
**Example 3:**
31+
32+
**Input:** nums = [1,2,3,4,5,6,7,8,9]
33+
34+
**Output:** 2
35+
36+
**Constraints:**
37+
38+
* `1 <= nums.length <= 1000`
39+
* `0 <= nums[i] <= 1000`
40+
41+
**Follow up:** Could you solve this in `O(n)` time?
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0301_0400.s0377_combination_sum_iv
2+
3+
// #Medium #Array #Dynamic_Programming #Dynamic_Programming_I_Day_21
4+
// #2022_11_22_Time_217_ms_(72.41%)_Space_33.9_MB_(86.21%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
private lateinit var storage: IntArray
10+
11+
fun combinationSum4(nums: IntArray, target: Int): Int {
12+
storage = IntArray(target + 1)
13+
Arrays.fill(storage, -1)
14+
return result(nums, target)
15+
}
16+
17+
private fun result(nums: IntArray, target: Int): Int {
18+
if (target < 0) {
19+
return 0
20+
}
21+
if (target == 0) {
22+
return 1
23+
}
24+
if (storage[target] != -1) {
25+
return storage[target]
26+
}
27+
var count = 0
28+
for (i in nums) {
29+
count += result(nums, target - i)
30+
}
31+
storage[target] = count
32+
return count
33+
}
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
377\. Combination Sum IV
2+
3+
Medium
4+
5+
Given an array of **distinct** integers `nums` and a target integer `target`, return _the number of possible combinations that add up to_ `target`.
6+
7+
The test cases are generated so that the answer can fit in a **32-bit** integer.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3], target = 4
12+
13+
**Output:** 7
14+
15+
**Explanation:**
16+
17+
The possible combination ways are:
18+
(1, 1, 1, 1)
19+
(1, 1, 2)
20+
(1, 2, 1)
21+
(1, 3)
22+
(2, 1, 1)
23+
(2, 2)
24+
(3, 1)
25+
Note that different sequences are counted as different combinations.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [9], target = 3
30+
31+
**Output:** 0
32+
33+
**Constraints:**
34+
35+
* `1 <= nums.length <= 200`
36+
* `1 <= nums[i] <= 1000`
37+
* All the elements of `nums` are **unique**.
38+
* `1 <= target <= 1000`
39+
40+
**Follow up:** What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0301_0400.s0380_insert_delete_getrandom_o1
2+
3+
import java.util.Random
4+
5+
// #Medium #Top_Interview_Questions #Array #Hash_Table #Math #Design #Randomized
6+
// #Programming_Skills_II_Day_20 #2022_11_22_Time_1326_ms_(68.23%)_Space_119.7_MB_(83.53%)
7+
8+
@Suppress("kotlin:S2245")
9+
class RandomizedSet {
10+
private val rand: Random
11+
private val list: MutableList<Int>
12+
private val map: MutableMap<Int, Int>
13+
14+
/* Initialize your data structure here. */
15+
init {
16+
rand = Random()
17+
list = ArrayList()
18+
map = HashMap()
19+
}
20+
21+
/* Inserts a value to the set. Returns true if the set did not already contain the specified element. */
22+
fun insert(`val`: Int): Boolean {
23+
if (map.containsKey(`val`)) {
24+
return false
25+
}
26+
map[`val`] = list.size
27+
list.add(`val`)
28+
return true
29+
}
30+
31+
/* Removes a value from the set. Returns true if the set contained the specified element. */
32+
fun remove(`val`: Int): Boolean {
33+
if (!map.containsKey(`val`)) {
34+
return false
35+
}
36+
val swap1 = map[`val`]!!
37+
val swap2 = list.size - 1
38+
val val2 = list[swap2]
39+
map[val2] = swap1
40+
map.remove(`val`)
41+
list[swap1] = val2
42+
list.removeAt(list.size - 1)
43+
return true
44+
}
45+
46+
/* Get a random element from the set. */
47+
fun getRandom(): Int {
48+
return list[rand.nextInt(list.size)]
49+
}
50+
}
51+
52+
/*
53+
* Your RandomizedSet object will be instantiated and called as such:
54+
* var obj = RandomizedSet()
55+
* var param_1 = obj.insert(`val`)
56+
* var param_2 = obj.remove(`val`)
57+
* var param_3 = obj.getRandom()
58+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
380\. Insert Delete GetRandom O(1)
2+
3+
Medium
4+
5+
Implement the `RandomizedSet` class:
6+
7+
* `RandomizedSet()` Initializes the `RandomizedSet` object.
8+
* `bool insert(int val)` Inserts an item `val` into the set if not present. Returns `true` if the item was not present, `false` otherwise.
9+
* `bool remove(int val)` Removes an item `val` from the set if present. Returns `true` if the item was present, `false` otherwise.
10+
* `int getRandom()` Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the **same probability** of being returned.
11+
12+
You must implement the functions of the class such that each function works in **average** `O(1)` time complexity.
13+
14+
**Example 1:**
15+
16+
**Input**
17+
18+
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
19+
[[], [1], [2], [2], [], [1], [2], []]
20+
21+
**Output:** [null, true, false, true, 2, true, false, 2]
22+
23+
**Explanation:**
24+
25+
RandomizedSet randomizedSet = new RandomizedSet();
26+
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
27+
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
28+
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
29+
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
30+
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
31+
randomizedSet.insert(2); // 2 was already in the set, so return false.
32+
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
33+
34+
**Constraints:**
35+
36+
* <code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code>
37+
* At most `2 * `<code>10<sup>5</sup></code> calls will be made to `insert`, `remove`, and `getRandom`.
38+
* There will be **at least one** element in the data structure when `getRandom` is called.

0 commit comments

Comments
 (0)