Skip to content

Commit 5b1d0be

Browse files
authored
Added tasks 688, 689, 690, 691
1 parent 69981e7 commit 5b1d0be

File tree

15 files changed

+485
-0
lines changed

15 files changed

+485
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16761676
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16771677
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16781678
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
1679+
| 0691 |[Stickers to Spell Word](src/main/kotlin/g0601_0700/s0691_stickers_to_spell_word/Solution.kt)| Hard | Array, String, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 249 | 100.00
1680+
| 0690 |[Employee Importance](src/main/kotlin/g0601_0700/s0690_employee_importance/Solution.kt)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search | 255 | 100.00
1681+
| 0689 |[Knight Probability in Chessboard](src/main/kotlin/g0601_0700/s0689_maximum_sum_of_3_non_overlapping_subarrays/Solution.kt)| Hard | Array, Dynamic_Programming | 248 | 100.00
1682+
| 0688 |[Knight Probability in Chessboard](src/main/kotlin/g0601_0700/s0688_knight_probability_in_chessboard/Solution.kt)| Medium | Dynamic_Programming | 144 | 100.00
16791683
| 0687 |[Longest Univalue Path](src/main/kotlin/g0601_0700/s0687_longest_univalue_path/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree | 303 | 100.00
16801684
| 0686 |[Repeated String Match](src/main/kotlin/g0601_0700/s0686_repeated_string_match/Solution.kt)| Medium | String, String_Matching | 164 | 100.00
16811685
| 0685 |[Redundant Connection II](src/main/kotlin/g0601_0700/s0685_redundant_connection_ii/Solution.kt)| Hard | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 176 | 100.00
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com_github_leetcode
2+
3+
class Employee(
4+
/** It's the unique id of each node; unique id of this employee */
5+
var id: Int,
6+
/** the importance value of this employee */
7+
var importance: Int,
8+
/** the id of direct subordinates */
9+
var subordinates: List<Int> = listOf()
10+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0601_0700.s0688_knight_probability_in_chessboard
2+
3+
// #Medium #Dynamic_Programming #2023_02_20_Time_144_ms_(100.00%)_Space_34.9_MB_(100.00%)
4+
5+
class Solution {
6+
private val directions = arrayOf(
7+
intArrayOf(-2, -1),
8+
intArrayOf(-2, 1),
9+
intArrayOf(-1, 2),
10+
intArrayOf(1, 2),
11+
intArrayOf(2, -1),
12+
intArrayOf(2, 1),
13+
intArrayOf(1, -2),
14+
intArrayOf(-1, -2)
15+
)
16+
private lateinit var probabilityGiven: Array<Array<DoubleArray>>
17+
fun knightProbability(n: Int, k: Int, row: Int, column: Int): Double {
18+
probabilityGiven = Array(n) {
19+
Array(n) {
20+
DoubleArray(
21+
k + 1
22+
)
23+
}
24+
}
25+
return probability(row, column, k, n)
26+
}
27+
28+
private fun probability(row: Int, column: Int, k: Int, n: Int): Double {
29+
return if (k == 0) {
30+
1.0
31+
} else if (probabilityGiven[row][column][k] != 0.0) {
32+
probabilityGiven[row][column][k]
33+
} else {
34+
var p = 0.0
35+
for (dir in directions) {
36+
if (isValid(row + dir[0], column + dir[1], n)) {
37+
p += probability(row + dir[0], column + dir[1], k - 1, n)
38+
}
39+
}
40+
probabilityGiven[row][column][k] = p / 8.0
41+
probabilityGiven[row][column][k]
42+
}
43+
}
44+
45+
private fun isValid(row: Int, column: Int, n: Int): Boolean {
46+
return row in 0 until n && column >= 0 && column < n
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
688\. Knight Probability in Chessboard
2+
3+
Medium
4+
5+
On an `n x n` chessboard, a knight starts at the cell `(row, column)` and attempts to make exactly `k` moves. The rows and columns are **0-indexed**, so the top-left cell is `(0, 0)`, and the bottom-right cell is `(n - 1, n - 1)`.
6+
7+
A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
8+
9+
![](https://assets.leetcode.com/uploads/2018/10/12/knight.png)
10+
11+
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
12+
13+
The knight continues moving until it has made exactly `k` moves or has moved off the chessboard.
14+
15+
Return _the probability that the knight remains on the board after it has stopped moving_.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 3, k = 2, row = 0, column = 0
20+
21+
**Output:** 0.06250
22+
23+
**Explanation:** There are two moves (to (1,2), (2,1)) that will keep the knight on the board. From each of those positions, there are also two moves that will keep the knight on the board. The total probability the knight stays on the board is 0.0625.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 1, k = 0, row = 0, column = 0
28+
29+
**Output:** 1.00000
30+
31+
**Constraints:**
32+
33+
* `1 <= n <= 25`
34+
* `0 <= k <= 100`
35+
* `0 <= row, column <= n - 1`
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package g0601_0700.s0689_maximum_sum_of_3_non_overlapping_subarrays
2+
3+
// #Hard #Array #Dynamic_Programming #2023_02_20_Time_248_ms_(100.00%)_Space_37.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun maxSumOfThreeSubarrays(nums: IntArray, k: Int): IntArray {
7+
val len = nums.size
8+
if (len < 3 * k) {
9+
return intArrayOf()
10+
}
11+
val res = IntArray(3)
12+
val left = Array(2) { IntArray(len) }
13+
val right = Array(2) { IntArray(len) }
14+
var s = 0
15+
for (i in 0 until k) {
16+
s += nums[i]
17+
}
18+
left[0][k - 1] = s
19+
run {
20+
var i = k
21+
while (i + 2 * k <= len) {
22+
s = s + nums[i] - nums[i - k]
23+
if (s > left[0][i - 1]) {
24+
left[0][i] = s
25+
left[1][i] = i - k + 1
26+
} else {
27+
left[0][i] = left[0][i - 1]
28+
left[1][i] = left[1][i - 1]
29+
}
30+
i++
31+
}
32+
}
33+
s = 0
34+
for (i in len - 1 downTo len - k) {
35+
s += nums[i]
36+
}
37+
right[0][len - k] = s
38+
right[1][len - k] = len - k
39+
for (i in len - k - 1 downTo 0) {
40+
s = s + nums[i] - nums[i + k]
41+
if (s >= right[0][i + 1]) {
42+
right[0][i] = s
43+
right[1][i] = i
44+
} else {
45+
right[0][i] = right[0][i + 1]
46+
right[1][i] = right[1][i + 1]
47+
}
48+
}
49+
var mid = 0
50+
for (i in k until 2 * k) {
51+
mid += nums[i]
52+
}
53+
var max = 0
54+
var i = k
55+
while (i + 2 * k <= len) {
56+
val total = left[0][i - 1] + right[0][i + k] + mid
57+
if (total > max) {
58+
res[0] = left[1][i - 1]
59+
res[1] = i
60+
res[2] = right[1][i + k]
61+
max = total
62+
}
63+
mid = mid + nums[i + k] - nums[i]
64+
i++
65+
}
66+
return res
67+
}
68+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
688\. Knight Probability in Chessboard
2+
3+
Medium
4+
5+
On an `n x n` chessboard, a knight starts at the cell `(row, column)` and attempts to make exactly `k` moves. The rows and columns are **0-indexed**, so the top-left cell is `(0, 0)`, and the bottom-right cell is `(n - 1, n - 1)`.
6+
7+
A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.
8+
9+
![](https://assets.leetcode.com/uploads/2018/10/12/knight.png)
10+
11+
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
12+
13+
The knight continues moving until it has made exactly `k` moves or has moved off the chessboard.
14+
15+
Return _the probability that the knight remains on the board after it has stopped moving_.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 3, k = 2, row = 0, column = 0
20+
21+
**Output:** 0.06250
22+
23+
**Explanation:** There are two moves (to (1,2), (2,1)) that will keep the knight on the board. From each of those positions, there are also two moves that will keep the knight on the board. The total probability the knight stays on the board is 0.0625.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 1, k = 0, row = 0, column = 0
28+
29+
**Output:** 1.00000
30+
31+
**Constraints:**
32+
33+
* `1 <= n <= 25`
34+
* `0 <= k <= 100`
35+
* `0 <= row, column <= n - 1`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0601_0700.s0690_employee_importance
2+
3+
// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search
4+
// #2023_02_20_Time_255_ms_(100.00%)_Space_46.3_MB_(71.43%)
5+
6+
import com_github_leetcode.Employee
7+
8+
/*
9+
* // Definition for Employee.
10+
* class Employee {
11+
* var id:Int = 0
12+
* var importance:Int = 0
13+
* var subordinates:List<Int> = listOf()
14+
* }
15+
*/
16+
17+
class Solution {
18+
fun getImportance(employees: List<Employee?>, id: Int): Int {
19+
val map: MutableMap<Int, Employee> = HashMap()
20+
for (emp in employees) {
21+
map[emp!!.id] = emp
22+
}
23+
return calculateImportance(id, map)
24+
}
25+
26+
private fun calculateImportance(id: Int, map: Map<Int, Employee>): Int {
27+
val employee = map[id]
28+
var sum = employee!!.importance
29+
for (sub in employee.subordinates) {
30+
sum += calculateImportance(sub, map)
31+
}
32+
return sum
33+
}
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
690\. Employee Importance
2+
3+
Medium
4+
5+
You have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs.
6+
7+
You are given an array of employees `employees` where:
8+
9+
* `employees[i].id` is the ID of the <code>i<sup>th</sup></code> employee.
10+
* `employees[i].importance` is the importance value of the <code>i<sup>th</sup></code> employee.
11+
* `employees[i].subordinates` is a list of the IDs of the direct subordinates of the <code>i<sup>th</sup></code> employee.
12+
13+
Given an integer `id` that represents an employee's ID, return _the **total** importance value of this employee and all their direct and indirect subordinates_.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/05/31/emp1-tree.jpg)
18+
19+
**Input:** employees = [[1,5,[2,3]],[2,3,[]],[3,3,[]]], id = 1
20+
21+
**Output:** 11
22+
23+
**Explanation:** Employee 1 has an importance value of 5 and has two direct subordinates: employee 2 and employee 3. They both have an importance value of 3. Thus, the total importance value of employee 1 is 5 + 3 + 3 = 11.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2021/05/31/emp2-tree.jpg)
28+
29+
**Input:** employees = [[1,2,[5]],[5,-3,[]]], id = 5
30+
31+
**Output:** -3
32+
33+
**Explanation:** Employee 5 has an importance value of -3 and has no direct subordinates. Thus, the total importance value of employee 5 is -3.
34+
35+
**Constraints:**
36+
37+
* `1 <= employees.length <= 2000`
38+
* `1 <= employees[i].id <= 2000`
39+
* All `employees[i].id` are **unique**.
40+
* `-100 <= employees[i].importance <= 100`
41+
* One employee has at most one direct leader and may have several subordinates.
42+
* The IDs in `employees[i].subordinates` are valid IDs.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g0601_0700.s0691_stickers_to_spell_word
2+
3+
// #Hard #Array #String #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2023_02_20_Time_249_ms_(100.00%)_Space_42.6_MB_(100.00%)
5+
6+
class Solution {
7+
// count the characters of every sticker
8+
private lateinit var counts: Array<IntArray>
9+
10+
// For each character, save the sticker index which has this character
11+
private val map: HashMap<Char, HashSet<Int>> = HashMap()
12+
private val cache: HashMap<Int, Int> = HashMap()
13+
fun minStickers(stickers: Array<String>, target: String): Int {
14+
counts = Array(stickers.size) { IntArray(26) }
15+
for (i in 0..25) {
16+
map[('a'.code + i).toChar()] = HashSet()
17+
}
18+
for (i in stickers.indices) {
19+
for (c in stickers[i].toCharArray()) {
20+
counts[i][c.code - 'a'.code]++
21+
map[c]!!.add(i)
22+
}
23+
}
24+
val res = dp(0, target)
25+
return if (res > target.length) {
26+
-1
27+
} else res
28+
}
29+
30+
private fun dp(bits: Int, target: String): Int {
31+
val len = target.length
32+
if (bits == (1 shl len) - 1) {
33+
// all bits are 1
34+
return 0
35+
}
36+
if (cache.containsKey(bits)) {
37+
return cache[bits]!!
38+
}
39+
var index = 0
40+
// find the first bit which is 0
41+
for (i in 0 until len) {
42+
if (bits and (1 shl i) == 0) {
43+
index = i
44+
break
45+
}
46+
}
47+
// In worst case, each character use 1 sticker. So, len + 1 means impossible
48+
var res = len + 1
49+
for (key in map[target[index]]!!) {
50+
val count = counts[key].clone()
51+
var mask = bits
52+
for (i in index until len) {
53+
if (mask and (1 shl i) != 0) {
54+
// this bit has already been 1
55+
continue
56+
}
57+
val c = target[i]
58+
if (count[c.code - 'a'.code] > 0) {
59+
count[c.code - 'a'.code]--
60+
mask = mask or (1 shl i)
61+
}
62+
}
63+
val `val` = dp(mask, target) + 1
64+
res = res.coerceAtMost(`val`)
65+
}
66+
cache[bits] = res
67+
return res
68+
}
69+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
691\. Stickers to Spell Word
2+
3+
Hard
4+
5+
We are given `n` different types of `stickers`. Each sticker has a lowercase English word on it.
6+
7+
You would like to spell out the given string `target` by cutting individual letters from your collection of stickers and rearranging them. You can use each sticker more than once if you want, and you have infinite quantities of each sticker.
8+
9+
Return _the minimum number of stickers that you need to spell out_ `target`. If the task is impossible, return `-1`.
10+
11+
**Note:** In all test cases, all words were chosen randomly from the `1000` most common US English words, and `target` was chosen as a concatenation of two random words.
12+
13+
**Example 1:**
14+
15+
**Input:** stickers = ["with","example","science"], target = "thehat"
16+
17+
**Output:** 3
18+
19+
**Explanation:** We can use 2 "with" stickers, and 1 "example" sticker. After cutting and rearrange the letters of those stickers, we can form the target "thehat". Also, this is the minimum number of stickers necessary to form the target string.
20+
21+
**Example 2:**
22+
23+
**Input:** stickers = ["notice","possible"], target = "basicbasic"
24+
25+
**Output:** -1 Explanation: We cannot form the target "basicbasic" from cutting letters from the given stickers.
26+
27+
**Constraints:**
28+
29+
* `n == stickers.length`
30+
* `1 <= n <= 50`
31+
* `1 <= stickers[i].length <= 10`
32+
* `1 <= target.length <= 15`
33+
* `stickers[i]` and `target` consist of lowercase English letters.

0 commit comments

Comments
 (0)