Skip to content

Commit 2eb4aa6

Browse files
authored
Added tasks 518, 519, 520, 521.
1 parent c31f0ab commit 2eb4aa6

File tree

13 files changed

+326
-0
lines changed

13 files changed

+326
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16251625
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16261626
|-|-|-|-|-|-
16271627
| 0322 |[Coin Change](src.save/main/kotlin/g0301_0400/s0322_coin_change/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search | 332 | 50.68
1628+
| 0518 |[Coin Change II](src/main/kotlin/g0501_0600/s0518_coin_change_2/Solution.kt)| Medium | Array, Dynamic_Programming | 139 | 100.00
16281629

16291630
#### Day 21
16301631

@@ -1646,6 +1647,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16461647
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16471648
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
16481649
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 307 | 43.93
1650+
| 0521 |[Longest Uncommon Subsequence I](src/main/kotlin/g0501_0600/s0521_longest_uncommon_subsequence_i/Solution.kt)| Easy | String | 146 | 88.89
1651+
| 0520 |[Detect Capital](src/main/kotlin/g0501_0600/s0520_detect_capital/Solution.kt)| Easy | String | 161 | 84.54
1652+
| 0519 |[Random Flip Matrix](src/main/kotlin/g0501_0600/s0519_random_flip_matrix/Solution.kt)| Medium | Hash_Table, Math, Randomized, Reservoir_Sampling | 270 | 100.00
1653+
| 0518 |[Coin Change II](src/main/kotlin/g0501_0600/s0518_coin_change_2/Solution.kt)| Medium | Array, Dynamic_Programming, Dynamic_Programming_I_Day_20 | 139 | 100.00
16491654
| 0517 |[Super Washing Machines](src/main/kotlin/g0501_0600/s0517_super_washing_machines/Solution.kt)| Hard | Array, Greedy | 210 | 100.00
16501655
| 0516 |[Longest Palindromic Subsequence](src/main/kotlin/g0501_0600/s0516_longest_palindromic_subsequence/Solution.kt)| Medium | String, Dynamic_Programming, Dynamic_Programming_I_Day_17 | 243 | 87.50
16511656
| 0515 |[Find Largest Value in Each Tree Row](src/main/kotlin/g0501_0600/s0515_find_largest_value_in_each_tree_row/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 238 | 73.33
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0501_0600.s0518_coin_change_2
2+
3+
// #Medium #Array #Dynamic_Programming #Dynamic_Programming_I_Day_20
4+
// #2023_01_13_Time_139_ms_(100.00%)_Space_33.6_MB_(94.00%)
5+
6+
class Solution {
7+
fun change(amount: Int, coins: IntArray): Int {
8+
val dp = IntArray(amount + 1)
9+
dp[0] = 1
10+
for (coin in coins) {
11+
for (i in coin..amount) {
12+
dp[i] += dp[i - coin]
13+
}
14+
}
15+
return dp[amount]
16+
}
17+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
518\. Coin Change II
2+
3+
Medium
4+
5+
You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
6+
7+
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
8+
9+
You may assume that you have an infinite number of each kind of coin.
10+
11+
The answer is **guaranteed** to fit into a signed **32-bit** integer.
12+
13+
**Example 1:**
14+
15+
**Input:** amount = 5, coins = [1,2,5]
16+
17+
**Output:** 4
18+
19+
**Explanation:** there are four ways to make up the amount:
20+
21+
5=5
22+
23+
5=2+2+1
24+
25+
5=2+1+1+1
26+
27+
5=1+1+1+1+1
28+
29+
**Example 2:**
30+
31+
**Input:** amount = 3, coins = [2]
32+
33+
**Output:** 0
34+
35+
**Explanation:** the amount of 3 cannot be made up just with coins of 2.
36+
37+
**Example 3:**
38+
39+
**Input:** amount = 10, coins = [10]
40+
41+
**Output:** 1
42+
43+
**Constraints:**
44+
45+
* `1 <= coins.length <= 300`
46+
* `1 <= coins[i] <= 5000`
47+
* All the values of `coins` are **unique**.
48+
* `0 <= amount <= 5000`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0501_0600.s0519_random_flip_matrix
2+
3+
// #Medium #Hash_Table #Math #Randomized #Reservoir_Sampling
4+
// #2023_01_13_Time_270_ms_(100.00%)_Space_37.6_MB_(100.00%)
5+
6+
import java.util.Random
7+
8+
@Suppress("kotlin:S2245")
9+
class Solution(nRows: Int, private val cols: Int) {
10+
private val total: Int
11+
private val rand: Random = Random()
12+
private val available: MutableSet<Int>
13+
14+
init {
15+
available = HashSet()
16+
total = nRows * cols
17+
}
18+
19+
fun flip(): IntArray {
20+
var x: Int = rand.nextInt(total)
21+
while (available.contains(x)) {
22+
x = rand.nextInt(total)
23+
}
24+
available.add(x)
25+
return intArrayOf(x / cols, x % cols)
26+
}
27+
28+
fun reset() {
29+
available.clear()
30+
}
31+
}
32+
33+
/*
34+
* Your Solution object will be instantiated and called as such:
35+
* var obj = Solution(m, n)
36+
* var param_1 = obj.flip()
37+
* obj.reset()
38+
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
519\. Random Flip Matrix
2+
3+
Medium
4+
5+
There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
6+
7+
Optimize your algorithm to minimize the number of calls made to the **built-in** random function of your language and optimize the time and space complexity.
8+
9+
Implement the `Solution` class:
10+
11+
* `Solution(int m, int n)` Initializes the object with the size of the binary matrix `m` and `n`.
12+
* `int[] flip()` Returns a random index `[i, j]` of the matrix where `matrix[i][j] == 0` and flips it to `1`.
13+
* `void reset()` Resets all the values of the matrix to be `0`.
14+
15+
**Example 1:**
16+
17+
**Input** ["Solution", "flip", "flip", "flip", "reset", "flip"] [[3, 1], [], [], [], [], []]
18+
19+
**Output:** [null, [1, 0], [2, 0], [0, 0], null, [2, 0]]
20+
21+
**Explanation:**
22+
23+
Solution solution = new Solution(3, 1);
24+
solution.flip(); // return [1, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
25+
solution.flip(); // return [2, 0], Since [1,0] was returned, [2,0] and [0,0]
26+
solution.flip(); // return [0, 0], Based on the previously returned indices, only [0,0] can be returned.
27+
solution.reset(); // All the values are reset to 0 and can be returned.
28+
solution.flip(); // return [2, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= m, n <= 10<sup>4</sup></code>
33+
* There will be at least one free cell for each call to `flip`.
34+
* At most `1000` calls will be made to `flip` and `reset`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0501_0600.s0520_detect_capital
2+
3+
// #Easy #String #2023_01_13_Time_161_ms_(84.54%)_Space_33.9_MB_(92.99%)
4+
5+
class Solution {
6+
fun detectCapitalUse(word: String?): Boolean {
7+
if (word == null || word.length == 0) {
8+
return false
9+
}
10+
var upper = 0
11+
var lower = 0
12+
val n = word.length
13+
var firstUpper = Character.isUpperCase(word[0])
14+
for (i in 0 until n) {
15+
if (Character.isUpperCase(word[i])) {
16+
upper++
17+
} else if (Character.isLowerCase(word[i])) {
18+
lower++
19+
}
20+
}
21+
if (firstUpper && upper > 1) {
22+
firstUpper = false
23+
}
24+
return upper == n || lower == n || firstUpper
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
520\. Detect Capital
2+
3+
Easy
4+
5+
We define the usage of capitals in a word to be right when one of the following cases holds:
6+
7+
* All letters in this word are capitals, like `"USA"`.
8+
* All letters in this word are not capitals, like `"leetcode"`.
9+
* Only the first letter in this word is capital, like `"Google"`.
10+
11+
Given a string `word`, return `true` if the usage of capitals in it is right.
12+
13+
**Example 1:**
14+
15+
**Input:** word = "USA"
16+
17+
**Output:** true
18+
19+
**Example 2:**
20+
21+
**Input:** word = "FlaG"
22+
23+
**Output:** false
24+
25+
**Constraints:**
26+
27+
* `1 <= word.length <= 100`
28+
* `word` consists of lowercase and uppercase English letters.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package g0501_0600.s0521_longest_uncommon_subsequence_i
2+
3+
// #Easy #String #2023_01_14_Time_146_ms_(88.89%)_Space_33.5_MB_(66.67%)
4+
5+
class Solution {
6+
fun findLUSlength(a: String, b: String): Int {
7+
return if (a == b) {
8+
-1
9+
} else a.length.coerceAtLeast(b.length)
10+
}
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
521\. Longest Uncommon Subsequence I
2+
3+
Easy
4+
5+
Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
6+
7+
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
8+
9+
A **subsequence** of a string `s` is a string that can be obtained after deleting any number of characters from `s`.
10+
11+
* For example, `"abc"` is a subsequence of `"aebdc"` because you can delete the underlined characters in <code>"a<ins>e</ins>b<ins>d</ins>c"</code> to get `"abc"`. Other subsequences of `"aebdc"` include `"aebdc"`, `"aeb"`, and `""` (empty string).
12+
13+
**Example 1:**
14+
15+
**Input:** a = "aba", b = "cdc"
16+
17+
**Output:** 3
18+
19+
**Explanation:** One longest uncommon subsequence is "aba" because "aba" is a subsequence of "aba" but not "cdc". Note that "cdc" is also a longest uncommon subsequence.
20+
21+
**Example 2:**
22+
23+
**Input:** a = "aaa", b = "bbb"
24+
25+
**Output:** 3
26+
27+
**Explanation:** The longest uncommon subsequences are "aaa" and "bbb".
28+
29+
**Example 3:**
30+
31+
**Input:** a = "aaa", b = "aaa"
32+
33+
**Output:** -1
34+
35+
**Explanation:** Every subsequence of string a is also a subsequence of string b. Similarly, every subsequence of string b is also a subsequence of string a.
36+
37+
**Constraints:**
38+
39+
* `1 <= a.length, b.length <= 100`
40+
* `a` and `b` consist of lower-case English letters.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0501_0600.s0518_coin_change_2
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 change() {
10+
assertThat(Solution().change(5, intArrayOf(1, 2, 5)), equalTo(4))
11+
}
12+
13+
@Test
14+
fun change2() {
15+
assertThat(Solution().change(3, intArrayOf(2)), equalTo(0))
16+
}
17+
18+
@Test
19+
fun change3() {
20+
assertThat(Solution().change(10, intArrayOf(10)), equalTo(1))
21+
}
22+
}

0 commit comments

Comments
 (0)