Skip to content

Commit f631bcd

Browse files
authored
Added tasks 473, 474, 475, 476.
1 parent 5431252 commit f631bcd

File tree

13 files changed

+339
-0
lines changed

13 files changed

+339
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16401640
| 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
16411641
| 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
16421642
| 0494 |[Target Sum](src/main/kotlin/g0401_0500/s0494_target_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking | 308 | 89.61
1643+
| 0476 |[Number Complement](src/main/kotlin/g0401_0500/s0476_number_complement/Solution.kt)| Easy | Bit_Manipulation | 133 | 100.00
1644+
| 0475 |[Heaters](src/main/kotlin/g0401_0500/s0475_heaters/Solution.kt)| Medium | Array, Sorting, Binary_Search, Two_Pointers | 356 | 87.50
1645+
| 0474 |[Ones and Zeroes](src/main/kotlin/g0401_0500/s0474_ones_and_zeroes/Solution.kt)| Medium | Array, String, Dynamic_Programming | 204 | 100.00
1646+
| 0473 |[Matchsticks to Square](src/main/kotlin/g0401_0500/s0473_matchsticks_to_square/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 255 | 100.00
16431647
| 0472 |[Concatenated Words](src/main/kotlin/g0401_0500/s0472_concatenated_words/Solution.kt)| Hard | Array, String, Dynamic_Programming, Depth_First_Search, Trie | 484 | 100.00
16441648
| 0470 |[Implement Rand10() Using Rand7()](src/main/kotlin/g0401_0500/s0470_implement_rand10_using_rand7/Solution.kt)| Medium | Math, Randomized, Probability_and_Statistics, Rejection_Sampling | 220 | 100.00
16451649
| 0468 |[Validate IP Address](src/main/kotlin/g0401_0500/s0468_validate_ip_address/Solution.kt)| Medium | String | 192 | 62.50
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0401_0500.s0473_matchsticks_to_square
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2022_12_31_Time_255_ms_(100.00%)_Space_33.9_MB_(100.00%)
5+
6+
class Solution {
7+
fun makesquare(matchsticks: IntArray): Boolean {
8+
if (matchsticks.size < 4) {
9+
return false
10+
}
11+
var per = 0
12+
for (ele in matchsticks) {
13+
per = ele + per
14+
}
15+
if (per % 4 != 0) {
16+
return false
17+
}
18+
matchsticks.sort()
19+
val side = per / 4
20+
val sides = intArrayOf(side, side, side, side)
21+
return help(matchsticks, matchsticks.size - 1, sides)
22+
}
23+
24+
private fun help(nums: IntArray, i: Int, side: IntArray): Boolean {
25+
if (i == -1) {
26+
return side[0] == 0 && side[1] == 0 && side[2] == 0 && side[3] == 0
27+
}
28+
for (j in 0..3) {
29+
if (nums[i] > side[j]) {
30+
continue
31+
}
32+
side[j] -= nums[i]
33+
if (help(nums, i - 1, side)) {
34+
return true
35+
}
36+
side[j] += nums[i]
37+
}
38+
return false
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
473\. Matchsticks to Square
2+
3+
Medium
4+
5+
You are given an integer array `matchsticks` where `matchsticks[i]` is the length of the <code>i<sup>th</sup></code> matchstick. You want to use **all the matchsticks** to make one square. You **should not break** any stick, but you can link them up, and each matchstick must be used **exactly one time**.
6+
7+
Return `true` if you can make this square and `false` otherwise.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg)
12+
13+
**Input:** matchsticks = [1,1,2,2,2]
14+
15+
**Output:** true
16+
17+
**Explanation:** You can form a square with length 2, one side of the square came two sticks with length 1.
18+
19+
**Example 2:**
20+
21+
**Input:** matchsticks = [3,3,3,3,4]
22+
23+
**Output:** false
24+
25+
**Explanation:** You cannot find a way to form a square with all the matchsticks.
26+
27+
**Constraints:**
28+
29+
* `1 <= matchsticks.length <= 15`
30+
* <code>1 <= matchsticks[i] <= 10<sup>8</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0401_0500.s0474_ones_and_zeroes
2+
3+
// #Medium #Array #String #Dynamic_Programming
4+
// #2022_12_31_Time_204_ms_(100.00%)_Space_35.7_MB_(92.31%)
5+
6+
class Solution {
7+
/*
8+
* The problem can be interpreted as:
9+
* What's the max number of str can we pick from strs with limitation of m "0"s and n "1"s.
10+
*
11+
* Thus we can define dp[i][j] as it stands for max number of str can we pick from strs with limitation
12+
* of i "0"s and j "1"s.
13+
*
14+
* For each str, assume it has a "0"s and b "1"s, we update the dp array iteratively
15+
* and set dp[i][j] = Math.max(dp[i][j], dp[i - a][j - b] + 1).
16+
* So at the end, dp[m][n] is the answer.
17+
*/
18+
fun findMaxForm(strs: Array<String>, m: Int, n: Int): Int {
19+
val dp = Array(m + 1) { IntArray(n + 1) }
20+
for (str in strs) {
21+
val count = count(str)
22+
for (i in m downTo count[0]) {
23+
for (j in n downTo count[1]) {
24+
dp[i][j] = Math.max(dp[i][j], dp[i - count[0]][j - count[1]] + 1)
25+
}
26+
}
27+
}
28+
return dp[m][n]
29+
}
30+
31+
private fun count(str: String): IntArray {
32+
val res = IntArray(2)
33+
for (i in 0 until str.length) {
34+
res[str[i].code - '0'.code]++
35+
}
36+
return res
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
474\. Ones and Zeroes
2+
3+
Medium
4+
5+
You are given an array of binary strings `strs` and two integers `m` and `n`.
6+
7+
Return _the size of the largest subset of `strs` such that there are **at most**_ `m` `0`_'s and_ `n` `1`_'s in the subset_.
8+
9+
A set `x` is a **subset** of a set `y` if all elements of `x` are also elements of `y`.
10+
11+
**Example 1:**
12+
13+
**Input:** strs = ["10","0001","111001","1","0"], m = 5, n = 3
14+
15+
**Output:** 4
16+
17+
**Explanation:** The largest subset with at most 5 0's and 3 1's is {"10", "0001", "1", "0"}, so the answer is 4.
18+
19+
Other valid but smaller subsets include {"0001", "1"} and {"10", "1", "0"}.
20+
21+
{"111001"} is an invalid subset because it contains 4 1's, greater than the maximum of 3.
22+
23+
**Example 2:**
24+
25+
**Input:** strs = ["10","0","1"], m = 1, n = 1
26+
27+
**Output:** 2
28+
29+
**Explanation:** The largest subset is {"0", "1"}, so the answer is 2.
30+
31+
**Constraints:**
32+
33+
* `1 <= strs.length <= 600`
34+
* `1 <= strs[i].length <= 100`
35+
* `strs[i]` consists only of digits `'0'` and `'1'`.
36+
* `1 <= m, n <= 100`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0401_0500.s0475_heaters
2+
3+
// #Medium #Array #Sorting #Binary_Search #Two_Pointers
4+
// #2022_12_31_Time_356_ms_(87.50%)_Space_44.6_MB_(87.50%)
5+
6+
class Solution {
7+
fun findRadius(houses: IntArray, heaters: IntArray): Int {
8+
var res = 0
9+
val m = houses.size
10+
val n = heaters.size
11+
var hs = 0
12+
var ht = 0
13+
houses.sort()
14+
heaters.sort()
15+
if (n == 1) {
16+
return Math.max(Math.abs(houses[0] - heaters[0]), Math.abs(houses[m - 1] - heaters[0]))
17+
}
18+
while (hs < m && ht < n - 1) {
19+
if (houses[hs] <= heaters[ht]) {
20+
res = Math.max(heaters[ht] - houses[hs], res)
21+
hs++
22+
} else if (houses[hs] <= heaters[ht + 1]) {
23+
res = Math.max(
24+
res,
25+
Math.min(houses[hs] - heaters[ht], heaters[ht + 1] - houses[hs])
26+
)
27+
hs++
28+
} else {
29+
ht++
30+
}
31+
}
32+
if (ht == n - 1) {
33+
res = Math.max(res, houses[m - 1] - heaters[n - 1])
34+
}
35+
return res
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
475\. Heaters
2+
3+
Medium
4+
5+
Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.
6+
7+
Every house can be warmed, as long as the house is within the heater's warm radius range.
8+
9+
Given the positions of `houses` and `heaters` on a horizontal line, return _the minimum radius standard of heaters so that those heaters could cover all houses._
10+
11+
**Notice** that all the `heaters` follow your radius standard, and the warm radius will the same.
12+
13+
**Example 1:**
14+
15+
**Input:** houses = [1,2,3], heaters = [2]
16+
17+
**Output:** 1
18+
19+
**Explanation:** The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
20+
21+
**Example 2:**
22+
23+
**Input:** houses = [1,2,3,4], heaters = [1,4]
24+
25+
**Output:** 1
26+
27+
**Explanation:** The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.
28+
29+
**Example 3:**
30+
31+
**Input:** houses = [1,5], heaters = [2]
32+
33+
**Output:** 3
34+
35+
**Constraints:**
36+
37+
* <code>1 <= houses.length, heaters.length <= 3 * 10<sup>4</sup></code>
38+
* <code>1 <= houses[i], heaters[i] <= 10<sup>9</sup></code>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g0401_0500.s0476_number_complement
2+
3+
// #Easy #Bit_Manipulation #2022_12_31_Time_133_ms_(100.00%)_Space_33.1_MB_(76.47%)
4+
5+
class Solution {
6+
fun findComplement(num: Int): Int {
7+
return num.inv() and (Integer.highestOneBit(num) shl 1) - 1
8+
}
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
476\. Number Complement
2+
3+
Easy
4+
5+
The **complement** of an integer is the integer you get when you flip all the `0`'s to `1`'s and all the `1`'s to `0`'s in its binary representation.
6+
7+
* For example, The integer `5` is `"101"` in binary and its **complement** is `"010"` which is the integer `2`.
8+
9+
Given an integer `num`, return _its complement_.
10+
11+
**Example 1:**
12+
13+
**Input:** num = 5
14+
15+
**Output:** 2
16+
17+
**Explanation:** The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
18+
19+
**Example 2:**
20+
21+
**Input:** num = 1
22+
23+
**Output:** 0
24+
25+
**Explanation:** The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= num < 2<sup>31</sup></code>
30+
31+
**Note:** This question is the same as 1009: [https://leetcode.com/problems/complement-of-base-10-integer/](https://leetcode.com/problems/complement-of-base-10-integer/)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0473_matchsticks_to_square
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 makeSquare() {
10+
assertThat(Solution().makesquare(intArrayOf(1, 1, 2, 2, 2)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun makeSquare2() {
15+
assertThat(Solution().makesquare(intArrayOf(3, 3, 3, 3, 4)), equalTo(false))
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0401_0500.s0474_ones_and_zeroes
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 findMaxForm() {
10+
assertThat(
11+
Solution().findMaxForm(arrayOf("10", "0001", "111001", "1", "0"), 5, 3),
12+
equalTo(4)
13+
)
14+
}
15+
16+
@Test
17+
fun findMaxForm2() {
18+
assertThat(Solution().findMaxForm(arrayOf("10", "0", "1"), 1, 1), equalTo(2))
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0401_0500.s0475_heaters
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 findRadius() {
10+
assertThat(Solution().findRadius(intArrayOf(1, 2, 3), intArrayOf(2)), equalTo(1))
11+
}
12+
13+
@Test
14+
fun findRadius2() {
15+
assertThat(Solution().findRadius(intArrayOf(1, 2, 3, 4), intArrayOf(1, 4)), equalTo(1))
16+
}
17+
18+
@Test
19+
fun findRadius3() {
20+
assertThat(Solution().findRadius(intArrayOf(1, 5), intArrayOf(2)), equalTo(3))
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0476_number_complement
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 findComplement() {
10+
assertThat(Solution().findComplement(5), equalTo(2))
11+
}
12+
13+
@Test
14+
fun findComplement2() {
15+
assertThat(Solution().findComplement(1), equalTo(0))
16+
}
17+
}

0 commit comments

Comments
 (0)