Skip to content

Commit 92f6ba6

Browse files
authored
Added tasks 462, 463, 464, 466.
1 parent ec56d29 commit 92f6ba6

File tree

13 files changed

+360
-0
lines changed

13 files changed

+360
-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+
| 0466 |[Count The Repetitions](src/main/kotlin/g0401_0500/s0466_count_the_repetitions/Solution.kt)| Hard | String, Dynamic_Programming | 147 | 100.00
1644+
| 0464 |[Can I Win](src/main/kotlin/g0401_0500/s0464_can_i_win/Solution.kt)| Medium | Dynamic_Programming, Math, Bit_Manipulation, Bitmask, Memoization, Game_Theory | 213 | 100.00
1645+
| 0463 |[Island Perimeter](src/main/kotlin/g0401_0500/s0463_island_perimeter/Solution.kt)| Easy | Array, Depth_First_Search, Breadth_First_Search, Matrix | 381 | 98.04
1646+
| 0462 |[Minimum Moves to Equal Array Elements II](src/main/kotlin/g0401_0500/s0462_minimum_moves_to_equal_array_elements_ii/Solution.kt)| Medium | Array, Math, Sorting | 210 | 100.00
16431647
| 0461 |[Hamming Distance](src/main/kotlin/g0401_0500/s0461_hamming_distance/Solution.kt)| Easy | Bit_Manipulation, Udemy_Bit_Manipulation | 150 | 96.15
16441648
| 0460 |[LFU Cache](src/main/kotlin/g0401_0500/s0460_lfu_cache/LFUCache.kt)| Hard | Hash_Table, Design, Linked_List, Doubly_Linked_List | 1143 | 100.00
16451649
| 0459 |[Repeated Substring Pattern](src/main/kotlin/g0401_0500/s0459_repeated_substring_pattern/Solution.kt)| Easy | String, String_Matching, Programming_Skills_II_Day_2 | 201 | 100.00
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0462_minimum_moves_to_equal_array_elements_ii
2+
3+
// #Medium #Array #Math #Sorting #2022_12_28_Time_210_ms_(100.00%)_Space_38.1_MB_(87.50%)
4+
5+
class Solution {
6+
fun minMoves2(nums: IntArray): Int {
7+
nums.sort()
8+
val median = (nums.size - 1) / 2
9+
var ops = 0
10+
for (num in nums) {
11+
if (num != nums[median]) {
12+
ops += Math.abs(nums[median] - num)
13+
}
14+
}
15+
return ops
16+
}
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
462\. Minimum Moves to Equal Array Elements II
2+
3+
Medium
4+
5+
Given an integer array `nums` of size `n`, return _the minimum number of moves required to make all array elements equal_.
6+
7+
In one move, you can increment or decrement an element of the array by `1`.
8+
9+
Test cases are designed so that the answer will fit in a **32-bit** integer.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3]
14+
15+
**Output:** 2
16+
17+
**Explanation:** Only two moves are needed (remember each move increments or decrements one element):
18+
19+
[<ins>1</ins>,2,3] => [2,2,<ins>3</ins>] => [2,2,2]
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,10,2,9]
24+
25+
**Output:** 16
26+
27+
**Constraints:**
28+
29+
* `n == nums.length`
30+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
31+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0401_0500.s0463_island_perimeter
2+
3+
// #Easy #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
// #2022_12_28_Time_381_ms_(98.04%)_Space_57.8_MB_(98.04%)
5+
6+
class Solution {
7+
fun islandPerimeter(grid: Array<IntArray>): Int {
8+
var islands = 0
9+
var neighbours = 0
10+
for (i in grid.indices) {
11+
for (j in grid[i].indices) {
12+
if (grid[i][j] == 1) {
13+
islands++
14+
if (i < grid.size - 1 && grid[i + 1][j] == 1) {
15+
neighbours++
16+
}
17+
if (j < grid[i].size - 1 && grid[i][j + 1] == 1) {
18+
neighbours++
19+
}
20+
}
21+
}
22+
}
23+
return 4 * islands - 2 * neighbours
24+
}
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
463\. Island Perimeter
2+
3+
Easy
4+
5+
You are given `row x col` `grid` representing a map where `grid[i][j] = 1` represents land and `grid[i][j] = 0` represents water.
6+
7+
Grid cells are connected **horizontally/vertically** (not diagonally). The `grid` is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
8+
9+
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2018/10/12/island.png)
14+
15+
**Input:** grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
16+
17+
**Output:** 16
18+
19+
**Explanation:** The perimeter is the 16 yellow stripes in the image above.
20+
21+
**Example 2:**
22+
23+
**Input:** grid = [[1]]
24+
25+
**Output:** 4
26+
27+
**Example 3:**
28+
29+
**Input:** grid = [[1,0]]
30+
31+
**Output:** 4
32+
33+
**Constraints:**
34+
35+
* `row == grid.length`
36+
* `col == grid[i].length`
37+
* `1 <= row, col <= 100`
38+
* `grid[i][j]` is `0` or `1`.
39+
* There is exactly one island in `grid`.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0401_0500.s0464_can_i_win
2+
3+
// #Medium #Dynamic_Programming #Math #Bit_Manipulation #Bitmask #Memoization #Game_Theory
4+
// #2022_12_28_Time_213_ms_(100.00%)_Space_38.8_MB_(100.00%)
5+
6+
class Solution {
7+
fun canIWin(maxChoosableInteger: Int, desiredTotal: Int): Boolean {
8+
if (desiredTotal <= maxChoosableInteger) {
9+
return true
10+
}
11+
return if (1.0 * maxChoosableInteger * (1 + maxChoosableInteger) / 2 < desiredTotal) {
12+
false
13+
} else canWin(0, arrayOfNulls(1 shl maxChoosableInteger), desiredTotal, maxChoosableInteger)
14+
}
15+
16+
private fun canWin(state: Int, dp: Array<Boolean?>, desiredTotal: Int, maxChoosableInteger: Int): Boolean {
17+
// state is the bitmap representation of the current state of choosable integers left
18+
// dp[state] represents whether the current player can win the game at state
19+
if (dp[state] != null) {
20+
return dp[state]!!
21+
}
22+
for (i in 1..maxChoosableInteger) {
23+
// current number to pick
24+
val cur = 1 shl i - 1
25+
if (cur and state == 0 &&
26+
(
27+
i >= desiredTotal ||
28+
!canWin(state or cur, dp, desiredTotal - i, maxChoosableInteger)
29+
)
30+
) {
31+
// i is greater than the desired total
32+
// or the other player cannot win after the current player picks i
33+
dp[state] = true
34+
return dp[state]!!
35+
}
36+
}
37+
dp[state] = false
38+
return dp[state]!!
39+
}
40+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
464\. Can I Win
2+
3+
Medium
4+
5+
In the "100 game" two players take turns adding, to a running total, any integer from `1` to `10`. The player who first causes the running total to **reach or exceed** 100 wins.
6+
7+
What if we change the game so that players **cannot** re-use integers?
8+
9+
For example, two players might take turns drawing from a common pool of numbers from 1 to 15 without replacement until they reach a total >= 100.
10+
11+
Given two integers `maxChoosableInteger` and `desiredTotal`, return `true` if the first player to move can force a win, otherwise, return `false`. Assume both players play **optimally**.
12+
13+
**Example 1:**
14+
15+
**Input:** maxChoosableInteger = 10, desiredTotal = 11
16+
17+
**Output:** false
18+
19+
**Explanation:**
20+
21+
No matter which integer the first player choose, the first player will lose.
22+
23+
The first player can choose an integer from 1 up to 10.
24+
25+
If the first player choose 1, the second player can only choose integers from 2 up to 10.
26+
27+
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
28+
29+
Same with other integers chosen by the first player, the second player will always win.
30+
31+
**Example 2:**
32+
33+
**Input:** maxChoosableInteger = 10, desiredTotal = 0
34+
35+
**Output:** true
36+
37+
**Example 3:**
38+
39+
**Input:** maxChoosableInteger = 10, desiredTotal = 1
40+
41+
**Output:** true
42+
43+
**Constraints:**
44+
45+
* `1 <= maxChoosableInteger <= 20`
46+
* `0 <= desiredTotal <= 300`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0401_0500.s0466_count_the_repetitions
2+
3+
// #Hard #String #Dynamic_Programming #2022_12_28_Time_147_ms_(100.00%)_Space_33.2_MB_(100.00%)
4+
5+
class Solution {
6+
fun getMaxRepetitions(s1: String, n1: Int, s2: String, n2: Int): Int {
7+
val n = s2.length
8+
val ss1 = s1.toCharArray()
9+
val ss2 = s2.toCharArray()
10+
val memo = arrayOfNulls<IntArray>(n)
11+
val s2CountMap = IntArray(n + 1)
12+
var s1Count = 0
13+
var s2Count = 0
14+
var s2Idx = 0
15+
while (memo[s2Idx] == null) {
16+
memo[s2Idx] = intArrayOf(s1Count, s2Count)
17+
for (c1 in ss1) {
18+
if (c1 == ss2[s2Idx]) {
19+
s2Idx++
20+
if (s2Idx == n) {
21+
s2Count++
22+
s2Idx = 0
23+
}
24+
}
25+
}
26+
s1Count++
27+
s2CountMap[s1Count] = s2Count
28+
}
29+
var n1Left = n1 - memo[s2Idx]!![0]
30+
val matchedPatternCount = n1Left / (s1Count - memo[s2Idx]!![0]) * (s2Count - memo[s2Idx]!![1])
31+
n1Left = n1Left % (s1Count - memo[s2Idx]!![0])
32+
val leftS2Count = s2CountMap[memo[s2Idx]!![0] + n1Left] - memo[s2Idx]!![1]
33+
val totalCount = leftS2Count + matchedPatternCount + memo[s2Idx]!![1]
34+
return totalCount / n2
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
466\. Count The Repetitions
2+
3+
Hard
4+
5+
We define `str = [s, n]` as the string `str` which consists of the string `s` concatenated `n` times.
6+
7+
* For example, `str == ["abc", 3] =="abcabcabc"`.
8+
9+
We define that string `s1` can be obtained from string `s2` if we can remove some characters from `s2` such that it becomes `s1`.
10+
11+
* For example, `s1 = "abc"` can be obtained from <code>s2 = "ab**<ins>dbe</ins>**c"</code> based on our definition by removing the bolded underlined characters.
12+
13+
You are given two strings `s1` and `s2` and two integers `n1` and `n2`. You have the two strings `str1 = [s1, n1]` and `str2 = [s2, n2]`.
14+
15+
Return _the maximum integer_ `m` _such that_ `str = [str2, m]` _can be obtained from_ `str1`.
16+
17+
**Example 1:**
18+
19+
**Input:** s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
20+
21+
**Output:** 2
22+
23+
**Example 2:**
24+
25+
**Input:** s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
26+
27+
**Output:** 1
28+
29+
**Constraints:**
30+
31+
* `1 <= s1.length, s2.length <= 100`
32+
* `s1` and `s2` consist of lowercase English letters.
33+
* <code>1 <= n1, n2 <= 10<sup>6</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0462_minimum_moves_to_equal_array_elements_ii
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 minMoves2() {
10+
assertThat(Solution().minMoves2(intArrayOf(1, 2, 3)), equalTo(2))
11+
}
12+
13+
@Test
14+
fun minMoves22() {
15+
assertThat(Solution().minMoves2(intArrayOf(1, 10, 2, 9)), equalTo(16))
16+
}
17+
}

0 commit comments

Comments
 (0)