Skip to content

Commit b9dd866

Browse files
authored
Added tasks 834, 835, 836, 837
1 parent 9ef826c commit b9dd866

File tree

13 files changed

+423
-0
lines changed

13 files changed

+423
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17131713
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17141714
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
17151715
| 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
1716+
| 0837 |[New 21 Game](src/main/kotlin/g0801_0900/s0837_new_21_game/Solution.kt)| Medium | Dynamic_Programming, Math, Sliding_Window, Probability_and_Statistics | 144 | 75.00
1717+
| 0836 |[Rectangle Overlap](src/main/kotlin/g0801_0900/s0836_rectangle_overlap/Solution.kt)| Easy | Math, Geometry | 121 | 100.00
1718+
| 0835 |[Image Overlap](src/main/kotlin/g0801_0900/s0835_image_overlap/Solution.kt)| Medium | Array, Matrix | 163 | 100.00
1719+
| 0834 |[Sum of Distances in Tree](src/main/kotlin/g0801_0900/s0834_sum_of_distances_in_tree/Solution.kt)| Hard | Dynamic_Programming, Depth_First_Search, Tree, Graph | 746 | 100.00
17161720
| 0833 |[Find And Replace in String](src/main/kotlin/g0801_0900/s0833_find_and_replace_in_string/Solution.kt)| Medium | Array, String, Sorting | 158 | 100.00
17171721
| 0832 |[Flipping an Image](src/main/kotlin/g0801_0900/s0832_flipping_an_image/Solution.kt)| Easy | Array, Matrix, Two_Pointers, Simulation | 190 | 94.44
17181722
| 0831 |[Masking Personal Information](src/main/kotlin/g0801_0900/s0831_masking_personal_information/Solution.kt)| Medium | String | 149 | 100.00
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0801_0900.s0834_sum_of_distances_in_tree
2+
3+
// #Hard #Dynamic_Programming #Depth_First_Search #Tree #Graph
4+
// #2023_03_27_Time_746_ms_(100.00%)_Space_65.9_MB_(100.00%)
5+
6+
class Solution {
7+
private var n = 0
8+
private lateinit var count: IntArray
9+
private lateinit var answer: IntArray
10+
private lateinit var graph: Array<MutableList<Int>?>
11+
12+
private fun postorder(node: Int, parent: Int) {
13+
for (child in graph[node]!!) {
14+
if (child != parent) {
15+
postorder(child, node)
16+
count[node] += count[child]
17+
answer[node] += answer[child] + count[child]
18+
}
19+
}
20+
}
21+
22+
private fun preorder(node: Int, parent: Int) {
23+
for (child in graph[node]!!) {
24+
if (child != parent) {
25+
answer[child] = answer[node] - count[child] + n - count[child]
26+
preorder(child, node)
27+
}
28+
}
29+
}
30+
31+
fun sumOfDistancesInTree(n: Int, edges: Array<IntArray>): IntArray {
32+
this.n = n
33+
count = IntArray(n)
34+
answer = IntArray(n)
35+
graph = arrayOfNulls(n)
36+
count.fill(1)
37+
for (i in 0 until n) {
38+
graph[i] = ArrayList()
39+
}
40+
for (edge in edges) {
41+
graph[edge[0]]?.add(edge[1])
42+
graph[edge[1]]?.add(edge[0])
43+
}
44+
postorder(0, -1)
45+
preorder(0, -1)
46+
return answer
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
834\. Sum of Distances in Tree
2+
3+
Hard
4+
5+
There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
6+
7+
You are given the integer `n` and the array `edges` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.
8+
9+
Return an array `answer` of length `n` where `answer[i]` is the sum of the distances between the <code>i<sup>th</sup></code> node in the tree and all other nodes.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist1.jpg)
14+
15+
**Input:** n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
16+
17+
**Output:** [8,12,6,10,10,10]
18+
19+
**Explanation:** The tree is shown above.
20+
21+
We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8.
22+
23+
Hence, answer[0] = 8, and so on.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist2.jpg)
28+
29+
**Input:** n = 1, edges = []
30+
31+
**Output:** [0]
32+
33+
**Example 3:**
34+
35+
![](https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist3.jpg)
36+
37+
**Input:** n = 2, edges = [[1,0]]
38+
39+
**Output:** [1,1]
40+
41+
**Constraints:**
42+
43+
* <code>1 <= n <= 3 * 10<sup>4</sup></code>
44+
* `edges.length == n - 1`
45+
* `edges[i].length == 2`
46+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
47+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
48+
* The given input represents a valid tree.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0801_0900.s0835_image_overlap
2+
3+
// #Medium #Array #Matrix #2023_03_27_Time_163_ms_(100.00%)_Space_35.3_MB_(100.00%)
4+
5+
class Solution {
6+
fun largestOverlap(img1: Array<IntArray>, img2: Array<IntArray>): Int {
7+
val bits1 = bitwise(img1)
8+
val bits2 = bitwise(img2)
9+
val n = img1.size
10+
var res = 0
11+
for (hori in -1 * n + 1 until n) {
12+
for (veti in -1 * n + 1 until n) {
13+
var curOverLapping = 0
14+
if (veti < 0) {
15+
for (i in -1 * veti until n) {
16+
curOverLapping += if (hori < 0) {
17+
Integer.bitCount(
18+
bits1[i] shl -1 * hori and bits2[i - -1 * veti]
19+
)
20+
} else {
21+
Integer.bitCount(bits1[i] shr hori and bits2[i - -1 * veti])
22+
}
23+
}
24+
} else {
25+
for (i in 0 until n - veti) {
26+
curOverLapping += if (hori < 0) {
27+
Integer.bitCount(bits1[i] shl -1 * hori and bits2[veti + i])
28+
} else {
29+
Integer.bitCount(bits1[i] shr hori and bits2[veti + i])
30+
}
31+
}
32+
}
33+
res = Math.max(res, curOverLapping)
34+
}
35+
}
36+
return res
37+
}
38+
39+
private fun bitwise(img: Array<IntArray>): IntArray {
40+
val bits = IntArray(img.size)
41+
for (i in img.indices) {
42+
var cur = 0
43+
for (j in img[0].indices) {
44+
cur = cur * 2 + img[i][j]
45+
}
46+
bits[i] = cur
47+
}
48+
return bits
49+
}
50+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
835\. Image Overlap
2+
3+
Medium
4+
5+
You are given two images, `img1` and `img2`, represented as binary, square matrices of size `n x n`. A binary matrix has only `0`s and `1`s as values.
6+
7+
We **translate** one image however we choose by sliding all the `1` bits left, right, up, and/or down any number of units. We then place it on top of the other image. We can then calculate the **overlap** by counting the number of positions that have a `1` in **both** images.
8+
9+
Note also that a translation does **not** include any kind of rotation. Any `1` bits that are translated outside of the matrix borders are erased.
10+
11+
Return _the largest possible overlap_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/09/09/overlap1.jpg)
16+
17+
**Input:** img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]]
18+
19+
**Output:** 3
20+
21+
**Explanation:** We translate img1 to right by 1 unit and down by 1 unit. ![](https://assets.leetcode.com/uploads/2020/09/09/overlap_step1.jpg) The number of positions that have a 1 in both images is 3 (shown in red). ![](https://assets.leetcode.com/uploads/2020/09/09/overlap_step2.jpg)
22+
23+
**Example 2:**
24+
25+
**Input:** img1 = [[1]], img2 = [[1]]
26+
27+
**Output:** 1
28+
29+
**Example 3:**
30+
31+
**Input:** img1 = [[0]], img2 = [[0]]
32+
33+
**Output:** 0
34+
35+
**Constraints:**
36+
37+
* `n == img1.length == img1[i].length`
38+
* `n == img2.length == img2[i].length`
39+
* `1 <= n <= 30`
40+
* `img1[i][j]` is either `0` or `1`.
41+
* `img2[i][j]` is either `0` or `1`.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g0801_0900.s0836_rectangle_overlap
2+
3+
// #Easy #Math #Geometry #2023_03_27_Time_121_ms_(100.00%)_Space_33.3_MB_(90.91%)
4+
5+
class Solution {
6+
fun isRectangleOverlap(rec1: IntArray, rec2: IntArray): Boolean {
7+
return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3]
8+
}
9+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
836\. Rectangle Overlap
2+
3+
Easy
4+
5+
An axis-aligned rectangle is represented as a list `[x1, y1, x2, y2]`, where `(x1, y1)` is the coordinate of its bottom-left corner, and `(x2, y2)` is the coordinate of its top-right corner. Its top and bottom edges are parallel to the X-axis, and its left and right edges are parallel to the Y-axis.
6+
7+
Two rectangles overlap if the area of their intersection is **positive**. To be clear, two rectangles that only touch at the corner or edges do not overlap.
8+
9+
Given two axis-aligned rectangles `rec1` and `rec2`, return `true` _if they overlap, otherwise return_ `false`.
10+
11+
**Example 1:**
12+
13+
**Input:** rec1 = [0,0,2,2], rec2 = [1,1,3,3]
14+
15+
**Output:** true
16+
17+
**Example 2:**
18+
19+
**Input:** rec1 = [0,0,1,1], rec2 = [1,0,2,1]
20+
21+
**Output:** false
22+
23+
**Example 3:**
24+
25+
**Input:** rec1 = [0,0,1,1], rec2 = [2,2,3,3]
26+
27+
**Output:** false
28+
29+
**Constraints:**
30+
31+
* `rec1.length == 4`
32+
* `rec2.length == 4`
33+
* <code>-10<sup>9</sup> <= rec1[i], rec2[i] <= 10<sup>9</sup></code>
34+
* `rec1` and `rec2` represent a valid rectangle with a non-zero area.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0801_0900.s0837_new_21_game
2+
3+
// #Medium #Dynamic_Programming #Math #Sliding_Window #Probability_and_Statistics
4+
// #2023_03_27_Time_144_ms_(75.00%)_Space_35.2_MB_(25.00%)
5+
6+
class Solution {
7+
fun new21Game(n: Int, k: Int, maxPts: Int): Double {
8+
if (k == 0) return 1.00
9+
if (k == 1 && maxPts <= n) return 1.00
10+
val dp = DoubleArray(n + 1)
11+
dp[0] = 1.00
12+
var prev = 0.00
13+
for (i in 1..n) {
14+
if (i - maxPts - 1 >= 0) {
15+
prev -= dp[i - 1 - maxPts]
16+
}
17+
if (i - 1 < k) {
18+
prev += dp[i - 1]
19+
}
20+
dp[i] = prev / maxPts
21+
}
22+
var res = 0.00
23+
for (i in k..n) {
24+
res += dp[i]
25+
}
26+
return res
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
837\. New 21 Game
2+
3+
Medium
4+
5+
Alice plays the following game, loosely based on the card game **"21"**.
6+
7+
Alice starts with `0` points and draws numbers while she has less than `k` points. During each draw, she gains an integer number of points randomly from the range `[1, maxPts]`, where `maxPts` is an integer. Each draw is independent and the outcomes have equal probabilities.
8+
9+
Alice stops drawing numbers when she gets `k` **or more points**.
10+
11+
Return the probability that Alice has `n` or fewer points.
12+
13+
Answers within <code>10<sup>-5</sup></code> of the actual answer are considered accepted.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 10, k = 1, maxPts = 10
18+
19+
**Output:** 1.00000
20+
21+
**Explanation:** Alice gets a single card, then stops.
22+
23+
**Example 2:**
24+
25+
**Input:** n = 6, k = 1, maxPts = 10
26+
27+
**Output:** 0.60000
28+
29+
**Explanation:** Alice gets a single card, then stops. In 6 out of 10 possibilities, she is at or below 6 points.
30+
31+
**Example 3:**
32+
33+
**Input:** n = 21, k = 17, maxPts = 10
34+
35+
**Output:** 0.73278
36+
37+
**Constraints:**
38+
39+
* <code>0 <= k <= n <= 10<sup>4</sup></code>
40+
* <code>1 <= maxPts <= 10<sup>4</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0801_0900.s0834_sum_of_distances_in_tree
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 sumOfDistancesInTree() {
10+
assertThat(
11+
Solution()
12+
.sumOfDistancesInTree(
13+
6, arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(2, 3), intArrayOf(2, 4), intArrayOf(2, 5))
14+
),
15+
equalTo(intArrayOf(8, 12, 6, 10, 10, 10))
16+
)
17+
}
18+
19+
@Test
20+
fun sumOfDistancesInTree2() {
21+
assertThat(Solution().sumOfDistancesInTree(1, arrayOf()), equalTo(intArrayOf(0)))
22+
}
23+
24+
@Test
25+
fun sumOfDistancesInTree3() {
26+
assertThat(
27+
Solution().sumOfDistancesInTree(2, arrayOf(intArrayOf(1, 0))),
28+
equalTo(intArrayOf(1, 1))
29+
)
30+
}
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0801_0900.s0835_image_overlap
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 largestOverlap() {
10+
assertThat(
11+
Solution()
12+
.largestOverlap(
13+
arrayOf(intArrayOf(1, 1, 0), intArrayOf(0, 1, 0), intArrayOf(0, 1, 0)),
14+
arrayOf(intArrayOf(0, 0, 0), intArrayOf(0, 1, 1), intArrayOf(0, 0, 1))
15+
),
16+
equalTo(3)
17+
)
18+
}
19+
20+
@Test
21+
fun largestOverlap2() {
22+
assertThat(Solution().largestOverlap(arrayOf(intArrayOf(1)), arrayOf(intArrayOf(1))), equalTo(1))
23+
}
24+
25+
@Test
26+
fun largestOverlap3() {
27+
assertThat(Solution().largestOverlap(arrayOf(intArrayOf(0)), arrayOf(intArrayOf(0))), equalTo(0))
28+
}
29+
}

0 commit comments

Comments
 (0)