Skip to content

Commit 989a602

Browse files
authored
Added tasks 526, 528, 529, 530.
1 parent 3ff1be4 commit 989a602

File tree

13 files changed

+431
-0
lines changed

13 files changed

+431
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
14551455
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14561456
|-|-|-|-|-|-
14571457
| 0154 |[Find Minimum in Rotated Sorted Array II](src.save/main/kotlin/g0101_0200/s0154_find_minimum_in_rotated_sorted_array_ii/Solution.kt)| Hard | Array, Binary_Search | 275 | 84.00
1458+
| 0528 |[Random Pick with Weight](src/main/kotlin/g0501_0600/s0528_random_pick_with_weight/Solution.kt)| Medium | Math, Binary_Search, Prefix_Sum, Randomized | 393 | 91.38
14581459

14591460
#### Day 14
14601461

@@ -1647,6 +1648,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16471648
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16481649
| 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
16491650
| 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
1651+
| 0530 |[Minimum Absolute Difference in BST](src/main/kotlin/g0501_0600/s0530_minimum_absolute_difference_in_bst/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 209 | 86.96
1652+
| 0529 |[Minesweeper](src/main/kotlin/g0501_0600/s0529_minesweeper/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 243 | 87.50
1653+
| 0528 |[Random Pick with Weight](src/main/kotlin/g0501_0600/s0528_random_pick_with_weight/Solution.kt)| Medium | Math, Binary_Search, Prefix_Sum, Randomized, Binary_Search_II_Day_13 | 393 | 91.38
1654+
| 0526 |[Beautiful Arrangement](src/main/kotlin/g0501_0600/s0526_beautiful_arrangement/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 107 | 100.00
16501655
| 0525 |[Contiguous Array](src/main/kotlin/g0501_0600/s0525_contiguous_array/Solution.kt)| Medium | Array, Hash_Table, Prefix_Sum | 471 | 100.00
16511656
| 0524 |[Longest Word in Dictionary through Deleting](src/main/kotlin/g0501_0600/s0524_longest_word_in_dictionary_through_deleting/Solution.kt)| Medium | Array, String, Sorting, Two_Pointers | 307 | 100.00
16521657
| 0523 |[Continuous Subarray Sum](src/main/kotlin/g0501_0600/s0523_continuous_subarray_sum/Solution.kt)| Medium | Array, Hash_Table, Math, Prefix_Sum | 682 | 95.45
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0501_0600.s0526_beautiful_arrangement
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2023_01_15_Time_107_ms_(100.00%)_Space_33.5_MB_(75.00%)
5+
6+
class Solution {
7+
fun countArrangement(n: Int): Int {
8+
return backtrack(n, n, arrayOfNulls(1 shl n + 1), 0)
9+
}
10+
11+
private fun backtrack(n: Int, index: Int, cache: Array<Int?>, cacheindex: Int): Int {
12+
if (index == 0) {
13+
return 1
14+
}
15+
var result = 0
16+
if (cache[cacheindex] != null) {
17+
return cache[cacheindex]!!
18+
}
19+
for (i in n downTo 1) {
20+
if (cacheindex and (1 shl i) == 0 && (i % index == 0 || index % i == 0)) {
21+
result += backtrack(n, index - 1, cache, cacheindex or (1 shl i))
22+
}
23+
}
24+
cache[cacheindex] = result
25+
return result
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
526\. Beautiful Arrangement
2+
3+
Medium
4+
5+
Suppose you have `n` integers labeled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:
6+
7+
* `perm[i]` is divisible by `i`.
8+
* `i` is divisible by `perm[i]`.
9+
10+
Given an integer `n`, return _the **number** of the **beautiful arrangements** that you can construct_.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 2
15+
16+
**Output:** 2
17+
18+
**Explanation:**
19+
20+
The first beautiful arrangement is [1,2]:
21+
22+
- perm[1] = 1 is divisible by i = 1
23+
24+
- perm[2] = 2 is divisible by i = 2
25+
26+
The second beautiful arrangement is [2,1]:
27+
28+
- perm[1] = 2 is divisible by i = 1
29+
30+
- i = 2 is divisible by perm[2] = 1
31+
32+
**Example 2:**
33+
34+
**Input:** n = 1
35+
36+
**Output:** 1
37+
38+
**Constraints:**
39+
40+
* `1 <= n <= 15`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0501_0600.s0528_random_pick_with_weight
2+
3+
// #Medium #Math #Binary_Search #Prefix_Sum #Randomized #Binary_Search_II_Day_13
4+
// #2023_01_15_Time_393_ms_(91.38%)_Space_47_MB_(98.28%)
5+
6+
import java.util.Random
7+
import java.util.TreeMap
8+
9+
@Suppress("kotlin:S2245")
10+
class Solution(val w: IntArray) {
11+
12+
var x: IntArray = IntArray(w.size) { 0 }
13+
val rand = Random()
14+
val tree = TreeMap<Int, Int>()
15+
var sum = 0
16+
17+
init {
18+
for (i in w.indices) {
19+
tree.put(sum, i)
20+
sum += w[i]
21+
}
22+
}
23+
24+
fun pickIndex(): Int {
25+
val r = rand.nextInt(sum)
26+
return tree.floorEntry(r).value!!
27+
}
28+
}
29+
30+
/*
31+
* Your Solution object will be instantiated and called as such:
32+
* var obj = Solution(w)
33+
* var param_1 = obj.pickIndex()
34+
*/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
528\. Random Pick with Weight
2+
3+
Medium
4+
5+
You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the <code>i<sup>th</sup></code> index.
6+
7+
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i] / sum(w)`.
8+
9+
* For example, if `w = [1, 3]`, the probability of picking index `0` is `1 / (1 + 3) = 0.25` (i.e., `25%`), and the probability of picking index `1` is `3 / (1 + 3) = 0.75` (i.e., `75%`).
10+
11+
**Example 1:**
12+
13+
**Input** ["Solution","pickIndex"] [[[1]],[]]
14+
15+
**Output:** [null,0]
16+
17+
**Explanation:**
18+
19+
Solution solution = new Solution([1]);
20+
solution.pickIndex(); // return 0. The only option is to return 0 since there is only one element in w.
21+
22+
**Example 2:**
23+
24+
**Input**
25+
26+
["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"]
27+
[[[1,3]],[],[],[],[],[]]
28+
29+
**Output:** [null,1,1,1,1,0]
30+
31+
**Explanation:**
32+
33+
Solution solution = new Solution([1, 3]);
34+
solution.pickIndex(); // return 1. It is returning the second element (index = 1) that has a probability of 3/4.
35+
solution.pickIndex(); // return 1
36+
solution.pickIndex(); // return 1
37+
solution.pickIndex(); // return 1
38+
solution.pickIndex(); // return 0. It is returning the first element (index = 0) that has a probability of 1/4.
39+
40+
Since this is a randomization problem, multiple answers are allowed.
41+
All of the following outputs can be considered correct:
42+
[null,1,1,1,1,0]
43+
[null,1,1,1,1,1]
44+
[null,1,1,1,0,0]
45+
[null,1,1,1,0,1]
46+
[null,1,0,1,0,0]
47+
......
48+
and so on.
49+
50+
**Constraints:**
51+
52+
* <code>1 <= w.length <= 10<sup>4</sup></code>
53+
* <code>1 <= w[i] <= 10<sup>5</sup></code>
54+
* `pickIndex` will be called at most <code>10<sup>4</sup></code> times.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g0501_0600.s0529_minesweeper
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
// #2023_01_15_Time_243_ms_(87.50%)_Space_37_MB_(87.50%)
5+
6+
class Solution {
7+
private var row = 0
8+
private var col = 0
9+
private fun dfs(board: Array<CharArray>, row: Int, col: Int) {
10+
if (row < 0 || row >= this.row || col < 0 || col >= this.col) {
11+
return
12+
}
13+
if (board[row][col] == 'E') {
14+
val numOfMine = bfs(board, row, col)
15+
if (numOfMine != 0) {
16+
board[row][col] = (numOfMine + '0'.code).toChar()
17+
return
18+
} else {
19+
board[row][col] = 'B'
20+
}
21+
for (i in DIRECTION) {
22+
dfs(board, row + i[0], col + i[1])
23+
}
24+
}
25+
}
26+
27+
private fun bfs(board: Array<CharArray>, row: Int, col: Int): Int {
28+
var numOfMine = 0
29+
for (i in DIRECTION) {
30+
val newRow = row + i[0]
31+
val newCol = col + i[1]
32+
if (newRow >= 0 && newRow < this.row && newCol >= 0 && newCol < this.col && board[newRow][newCol] == 'M') {
33+
numOfMine++
34+
}
35+
}
36+
return numOfMine
37+
}
38+
39+
fun updateBoard(board: Array<CharArray>, c: IntArray): Array<CharArray> {
40+
if (board[c[0]][c[1]] == 'M') {
41+
board[c[0]][c[1]] = 'X'
42+
return board
43+
} else {
44+
row = board.size
45+
col = board[0].size
46+
dfs(board, c[0], c[1])
47+
}
48+
return board
49+
}
50+
51+
companion object {
52+
private val DIRECTION = arrayOf(
53+
intArrayOf(1, 0),
54+
intArrayOf(-1, 0),
55+
intArrayOf(0, 1),
56+
intArrayOf(0, -1),
57+
intArrayOf(-1, -1),
58+
intArrayOf(-1, 1),
59+
intArrayOf(1, -1),
60+
intArrayOf(1, 1)
61+
)
62+
}
63+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
529\. Minesweeper
2+
3+
Medium
4+
5+
Let's play the minesweeper game ([Wikipedia](https://en.wikipedia.org/wiki/Minesweeper_(video_game)), [online game](http://minesweeperonline.com))!
6+
7+
You are given an `m x n` char matrix `board` representing the game board where:
8+
9+
* `'M'` represents an unrevealed mine,
10+
* `'E'` represents an unrevealed empty square,
11+
* `'B'` represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),
12+
* digit (`'1'` to `'8'`) represents how many mines are adjacent to this revealed square, and
13+
* `'X'` represents a revealed mine.
14+
15+
You are also given an integer array `click` where <code>click = [click<sub>r</sub>, click<sub>c</sub>]</code> represents the next click position among all the unrevealed squares (`'M'` or `'E'`).
16+
17+
Return _the board after revealing this position according to the following rules_:
18+
19+
1. If a mine `'M'` is revealed, then the game is over. You should change it to `'X'`.
20+
2. If an empty square `'E'` with no adjacent mines is revealed, then change it to a revealed blank `'B'` and all of its adjacent unrevealed squares should be revealed recursively.
21+
3. If an empty square `'E'` with at least one adjacent mine is revealed, then change it to a digit (`'1'` to `'8'`) representing the number of adjacent mines.
22+
4. Return the board when no more squares will be revealed.
23+
24+
**Example 1:**
25+
26+
![](https://assets.leetcode.com/uploads/2018/10/12/minesweeper_example_1.png)
27+
28+
**Input:** board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0]
29+
30+
**Output:** [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
31+
32+
**Example 2:**
33+
34+
![](https://assets.leetcode.com/uploads/2018/10/12/minesweeper_example_2.png)
35+
36+
**Input:** board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
37+
38+
**Output:** [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
39+
40+
**Constraints:**
41+
42+
* `m == board.length`
43+
* `n == board[i].length`
44+
* `1 <= m, n <= 50`
45+
* `board[i][j]` is either `'M'`, `'E'`, `'B'`, or a digit from `'1'` to `'8'`.
46+
* `click.length == 2`
47+
* <code>0 <= click<sub>r</sub> < m</code>
48+
* <code>0 <= click<sub>c</sub> < n</code>
49+
* <code>board[click<sub>r</sub>][click<sub>c</sub>]</code> is either `'M'` or `'E'`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0501_0600.s0530_minimum_absolute_difference_in_bst
2+
3+
import com_github_leetcode.TreeNode
4+
5+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
6+
// #2023_01_15_Time_209_ms_(86.96%)_Space_38.5_MB_(69.57%)
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
private var ans = Int.MAX_VALUE
20+
private var prev = Int.MAX_VALUE
21+
fun getMinimumDifference(root: TreeNode?): Int {
22+
if (root == null) {
23+
return ans
24+
}
25+
getMinimumDifference(root.left)
26+
ans = Math.min(ans, Math.abs(root.`val` - prev))
27+
prev = root.`val`
28+
getMinimumDifference(root.right)
29+
return ans
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
530\. Minimum Absolute Difference in BST
2+
3+
Easy
4+
5+
Given the `root` of a Binary Search Tree (BST), return _the minimum absolute difference between the values of any two different nodes in the tree_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg)
10+
11+
**Input:** root = [4,2,6,1,3]
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg)
18+
19+
**Input:** root = [1,0,48,null,null,12,49]
20+
21+
**Output:** 1
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.
26+
* <code>0 <= Node.val <= 10<sup>5</sup></code>
27+
28+
**Note:** This question is the same as 783: [https://leetcode.com/problems/minimum-distance-between-bst-nodes/](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0501_0600.s0526_beautiful_arrangement
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 countArrangement() {
10+
assertThat(Solution().countArrangement(2), equalTo(2))
11+
}
12+
13+
@Test
14+
fun countArrangement2() {
15+
assertThat(Solution().countArrangement(1), equalTo(1))
16+
}
17+
}

0 commit comments

Comments
 (0)