Skip to content

Commit b307084

Browse files
authored
Added tasks 850, 851, 852, 853
1 parent a69c6ab commit b307084

File tree

13 files changed

+430
-1
lines changed

13 files changed

+430
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LeetCode-in-Kotlin
22

3-
[![Maven Central](https://img.shields.io/maven-central/v/com.github.javadev/leetcode-in-kotlin?style=flat-square)](https://central.sonatype.com/artifact/com.github.javadev/leetcode-in-kotlin/1.10)
3+
[![Maven Central](https://img.shields.io/maven-central/v/com.github.javadev/leetcode-in-kotlin?style=flat-square)](https://central.sonatype.com/artifact/com.github.javadev/leetcode-in-kotlin/1.11)
44
[![MIT License](http://img.shields.io/badge/license-MIT-green.svg) ](https://github.com/javadev/leetcode-in-kotlin/blob/main/LICENSE)
55
[![Java CI with Maven](https://github.com/javadev/LeetCode-in-Kotlin/actions/workflows/maven.yml/badge.svg)](https://github.com/javadev/LeetCode-in-Kotlin/actions/workflows/maven.yml)
66
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=javadev_LeetCode-in-Kotlin&metric=sqale_rating)](https://sonarcloud.io/summary/overall?id=javadev_LeetCode-in-Kotlin)
@@ -1176,6 +1176,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
11761176
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11771177
|-|-|-|-|-|-
11781178
| 0035 |[Search Insert Position](src/main/kotlin/g0001_0100/s0035_search_insert_position/Solution.kt)| Easy | Top_100_Liked_Questions, Array, Binary_Search | 267 | 50.32
1179+
| 0852 |[Peak Index in a Mountain Array](src/main/kotlin/g0801_0900/s0852_peak_index_in_a_mountain_array/Solution.kt)| Easy | Array, Binary_Search | 433 | 94.29
11791180

11801181
#### Day 3
11811182

@@ -1718,6 +1719,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17181719
| 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
17191720
| 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
17201721
| 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
1722+
| 0853 |[Car Fleet](src/main/kotlin/g0801_0900/s0853_car_fleet/Solution.kt)| Medium | Array, Sorting, Stack, Monotonic_Stack | 757 | 85.29
1723+
| 0852 |[Peak Index in a Mountain Array](src/main/kotlin/g0801_0900/s0852_peak_index_in_a_mountain_array/Solution.kt)| Easy | Array, Binary_Search, Binary_Search_I_Day_2 | 433 | 94.29
1724+
| 0851 |[Loud and Rich](src/main/kotlin/g0801_0900/s0851_loud_and_rich/Solution.kt)| Medium | Array, Depth_First_Search, Graph, Topological_Sort | 347 | 100.00
1725+
| 0850 |[Rectangle Area II](src/main/kotlin/g0801_0900/s0850_rectangle_area_ii/Solution.kt)| Hard | Array, Ordered_Set, Segment_Tree, Line_Sweep | 171 | 100.00
17211726
| 0849 |[Maximize Distance to Closest Person](src/main/kotlin/g0801_0900/s0849_maximize_distance_to_closest_person/Solution.kt)| Medium | Array | 196 | 88.46
17221727
| 0848 |[Shifting Letters](src/main/kotlin/g0801_0900/s0848_shifting_letters/Solution.kt)| Medium | Array, String | 537 | 93.75
17231728
| 0847 |[Shortest Path Visiting All Nodes](src/main/kotlin/g0801_0900/s0847_shortest_path_visiting_all_nodes/Solution.kt)| Hard | Dynamic_Programming, Breadth_First_Search, Bit_Manipulation, Graph, Bitmask, Graph_Theory_I_Day_10_Standard_Traversal | 164 | 100.00
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g0801_0900.s0850_rectangle_area_ii
2+
3+
// #Hard #Array #Ordered_Set #Segment_Tree #Line_Sweep
4+
// #2023_03_30_Time_171_ms_(100.00%)_Space_35.7_MB_(100.00%)
5+
6+
class Solution {
7+
fun rectangleArea(rectangles: Array<IntArray>): Int {
8+
val memo: MutableList<IntArray> = ArrayList()
9+
for (rectangle in rectangles) {
10+
helper(0, rectangle, memo)
11+
}
12+
var res: Long = 0
13+
val mod = (1e9 + 7).toInt()
14+
for (m in memo) {
15+
res = (res + (m[2] - m[0]).toLong() * (m[3] - m[1]).toLong()) % mod
16+
}
17+
return res.toInt()
18+
}
19+
20+
private fun helper(id: Int, rectangle: IntArray, memo: MutableList<IntArray>) {
21+
if (id >= memo.size) {
22+
memo.add(rectangle)
23+
return
24+
}
25+
val cur = memo[id]
26+
if (rectangle[2] <= cur[0] || rectangle[0] >= cur[2] || rectangle[1] >= cur[3] || rectangle[3] <= cur[1]) {
27+
helper(id + 1, rectangle, memo)
28+
return
29+
}
30+
if (rectangle[0] < cur[0]) {
31+
helper(id + 1, intArrayOf(rectangle[0], rectangle[1], cur[0], rectangle[3]), memo)
32+
}
33+
if (rectangle[2] > cur[2]) {
34+
helper(id + 1, intArrayOf(cur[2], rectangle[1], rectangle[2], rectangle[3]), memo)
35+
}
36+
if (rectangle[1] < cur[1]) {
37+
helper(
38+
id + 1,
39+
intArrayOf(
40+
rectangle[0].coerceAtLeast(cur[0]),
41+
rectangle[1],
42+
rectangle[2].coerceAtMost(cur[2]),
43+
cur[1]
44+
),
45+
memo
46+
)
47+
}
48+
if (rectangle[3] > cur[3]) {
49+
helper(
50+
id + 1,
51+
intArrayOf(
52+
rectangle[0].coerceAtLeast(cur[0]),
53+
cur[3],
54+
rectangle[2].coerceAtMost(cur[2]),
55+
rectangle[3]
56+
),
57+
memo
58+
)
59+
}
60+
}
61+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
850\. Rectangle Area II
2+
3+
Hard
4+
5+
You are given a 2D array of axis-aligned `rectangles`. Each <code>rectangle[i] = [x<sub>i1</sub>, y<sub>i1</sub>, x<sub>i2</sub>, y<sub>i2</sub>]</code> denotes the <code>i<sup>th</sup></code> rectangle where <code>(x<sub>i1</sub>, y<sub>i1</sub>)</code> are the coordinates of the **bottom-left corner**, and <code>(x<sub>i2</sub>, y<sub>i2</sub>)</code> are the coordinates of the **top-right corner**.
6+
7+
Calculate the **total area** covered by all `rectangles` in the plane. Any area covered by two or more rectangles should only be counted **once**.
8+
9+
Return _the **total area**_. Since the answer may be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
**Example 1:**
12+
13+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/06/rectangle_area_ii_pic.png)
14+
15+
**Input:** rectangles = [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
16+
17+
**Output:** 6
18+
19+
**Explanation:** A total area of 6 is covered by all three rectangles, as illustrated in the picture.
20+
21+
From (1,1) to (2,2), the green and red rectangles overlap.
22+
23+
From (1,0) to (2,3), all three rectangles overlap.
24+
25+
**Example 2:**
26+
27+
**Input:** rectangles = [[0,0,1000000000,1000000000]]
28+
29+
**Output:** 49
30+
31+
**Explanation:** The answer is 10<sup>18</sup> modulo (10<sup>9</sup> + 7), which is 49.
32+
33+
**Constraints:**
34+
35+
* `1 <= rectangles.length <= 200`
36+
* `rectanges[i].length == 4`
37+
* <code>0 <= x<sub>i1</sub>, y<sub>i1</sub>, x<sub>i2</sub>, y<sub>i2</sub> <= 10<sup>9</sup></code>
38+
* <code>x<sub>i1 <=</sub> x<sub>i2</sub></code>
39+
* <code>y<sub>i1 <=</sub> y<sub>i2</sub></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0801_0900.s0851_loud_and_rich
2+
3+
// #Medium #Array #Depth_First_Search #Graph #Topological_Sort
4+
// #2023_03_30_Time_347_ms_(100.00%)_Space_54.4_MB_(80.00%)
5+
6+
class Solution {
7+
fun loudAndRich(richer: Array<IntArray>, quiet: IntArray): IntArray {
8+
val result = IntArray(quiet.size)
9+
for (i in quiet.indices) {
10+
result[i] = i
11+
}
12+
for (k in quiet.indices) {
13+
var changed = false
14+
for (r in richer) {
15+
if (quiet[result[r[0]]] < quiet[result[r[1]]]) {
16+
result[r[1]] = result[r[0]]
17+
changed = true
18+
}
19+
}
20+
if (!changed) {
21+
break
22+
}
23+
}
24+
return result
25+
}
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
851\. Loud and Rich
2+
3+
Medium
4+
5+
There is a group of `n` people labeled from `0` to `n - 1` where each person has a different amount of money and a different level of quietness.
6+
7+
You are given an array `richer` where <code>richer[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that <code>a<sub>i</sub></code> has more money than <code>b<sub>i</sub></code> and an integer array `quiet` where `quiet[i]` is the quietness of the <code>i<sup>th</sup></code> person. All the given data in richer are **logically correct** (i.e., the data will not lead you to a situation where `x` is richer than `y` and `y` is richer than `x` at the same time).
8+
9+
Return _an integer array_ `answer` _where_ `answer[x] = y` _if_ `y` _is the least quiet person (that is, the person_ `y` _with the smallest value of_ `quiet[y]`_) among all people who definitely have equal to or more money than the person_ `x`.
10+
11+
**Example 1:**
12+
13+
**Input:** richer = [[1,0],[2,1],[3,1],[3,7],[4,3],[5,3],[6,3]], quiet = [3,2,5,4,6,1,7,0]
14+
15+
**Output:** [5,5,2,5,4,5,6,7]
16+
17+
**Explanation:**
18+
19+
answer[0] = 5.
20+
21+
Person 5 has more money than 3, which has more money than 1, which has more money than 0.
22+
23+
The only person who is quieter (has lower quiet[x]) is person 7, but it is not clear if they have more money than person 0.
24+
25+
answer[7] = 7.
26+
27+
Among all people that definitely have equal to or more money than person 7 (which could be persons 3, 4, 5, 6, or 7), the person who is the quietest (has lower quiet[x]) is person 7.
28+
29+
The other answers can be filled out with similar reasoning.
30+
31+
**Example 2:**
32+
33+
**Input:** richer = [], quiet = [0]
34+
35+
**Output:** [0]
36+
37+
**Constraints:**
38+
39+
* `n == quiet.length`
40+
* `1 <= n <= 500`
41+
* `0 <= quiet[i] < n`
42+
* All the values of `quiet` are **unique**.
43+
* `0 <= richer.length <= n * (n - 1) / 2`
44+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
45+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
46+
* All the pairs of `richer` are **unique**.
47+
* The observations in `richer` are all logically consistent.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g0801_0900.s0852_peak_index_in_a_mountain_array
2+
3+
// #Easy #Array #Binary_Search #Binary_Search_I_Day_2
4+
// #2023_03_30_Time_433_ms_(94.29%)_Space_49.4_MB_(100.00%)
5+
6+
class Solution {
7+
fun peakIndexInMountainArray(arr: IntArray): Int {
8+
for (i in 1 until arr.size - 1) {
9+
if (arr[i] > arr[i + 1]) {
10+
return i
11+
}
12+
}
13+
return -1
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
852\. Peak Index in a Mountain Array
2+
3+
Medium
4+
5+
An array `arr` a **mountain** if the following properties hold:
6+
7+
* `arr.length >= 3`
8+
* There exists some `i` with `0 < i < arr.length - 1` such that:
9+
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
10+
* `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
11+
12+
Given a mountain array `arr`, return the index `i` such that `arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`.
13+
14+
You must solve it in `O(log(arr.length))` time complexity.
15+
16+
**Example 1:**
17+
18+
**Input:** arr = [0,1,0]
19+
20+
**Output:** 1
21+
22+
**Example 2:**
23+
24+
**Input:** arr = [0,2,1,0]
25+
26+
**Output:** 1
27+
28+
**Example 3:**
29+
30+
**Input:** arr = [0,10,5,2]
31+
32+
**Output:** 1
33+
34+
**Constraints:**
35+
36+
* <code>3 <= arr.length <= 10<sup>5</sup></code>
37+
* <code>0 <= arr[i] <= 10<sup>6</sup></code>
38+
* `arr` is **guaranteed** to be a mountain array.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0801_0900.s0853_car_fleet
2+
3+
// #Medium #Array #Sorting #Stack #Monotonic_Stack
4+
// #2023_03_30_Time_757_ms_(85.29%)_Space_50.7_MB_(100.00%)
5+
6+
class Solution {
7+
private class Car {
8+
var position = 0
9+
var speed = 0
10+
}
11+
12+
fun carFleet(target: Int, position: IntArray, speed: IntArray): Int {
13+
val cars: MutableList<Car> = ArrayList()
14+
for (i in position.indices) {
15+
val c = Car()
16+
c.position = position[i]
17+
c.speed = speed[i]
18+
cars.add(c)
19+
}
20+
cars.sortBy { it.position }
21+
var numFleets = 1
22+
var lastTime = (
23+
(target - cars[cars.size - 1].position).toFloat() /
24+
cars[cars.size - 1].speed
25+
)
26+
for (i in cars.size - 2 downTo 0) {
27+
val timeToTarget = (target - cars[i].position).toFloat() / cars[i].speed
28+
if (timeToTarget > lastTime) {
29+
numFleets++
30+
lastTime = timeToTarget
31+
}
32+
}
33+
return numFleets
34+
}
35+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
853\. Car Fleet
2+
3+
Medium
4+
5+
There are `n` cars going to the same destination along a one-lane road. The destination is `target` miles away.
6+
7+
You are given two integer array `position` and `speed`, both of length `n`, where `position[i]` is the position of the <code>i<sup>th</sup></code> car and `speed[i]` is the speed of the <code>i<sup>th</sup></code> car (in miles per hour).
8+
9+
A car can never pass another car ahead of it, but it can catch up to it and drive bumper to bumper **at the same speed**. The faster car will **slow down** to match the slower car's speed. The distance between these two cars is ignored (i.e., they are assumed to have the same position).
10+
11+
A **car fleet** is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.
12+
13+
If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.
14+
15+
Return _the **number of car fleets** that will arrive at the destination_.
16+
17+
**Example 1:**
18+
19+
**Input:** target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
20+
21+
**Output:** 3
22+
23+
**Explanation:**
24+
25+
The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12.
26+
27+
The car starting at 0 does not catch up to any other car, so it is a fleet by itself.
28+
29+
The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
30+
31+
Note that no other cars meet these fleets before the destination, so the answer is 3.
32+
33+
**Example 2:**
34+
35+
**Input:** target = 10, position = [3], speed = [3]
36+
37+
**Output:** 1
38+
39+
**Explanation:** There is only one car, hence there is only one fleet.
40+
41+
**Example 3:**
42+
43+
**Input:** target = 100, position = [0,2,4], speed = [4,2,1]
44+
45+
**Output:** 1
46+
47+
**Explanation:** The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The fleet moves at speed 2. Then, the fleet (speed 2) and the car starting at 4 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.
48+
49+
**Constraints:**
50+
51+
* `n == position.length == speed.length`
52+
* <code>1 <= n <= 10<sup>5</sup></code>
53+
* <code>0 < target <= 10<sup>6</sup></code>
54+
* `0 <= position[i] < target`
55+
* All the values of `position` are **unique**.
56+
* <code>0 < speed[i] <= 10<sup>6</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0801_0900.s0850_rectangle_area_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 rectangleArea() {
10+
assertThat(
11+
Solution()
12+
.rectangleArea(arrayOf(intArrayOf(0, 0, 2, 2), intArrayOf(1, 0, 2, 3), intArrayOf(1, 0, 3, 1))),
13+
equalTo(6)
14+
)
15+
}
16+
17+
@Test
18+
fun rectangleArea2() {
19+
assertThat(
20+
Solution().rectangleArea(arrayOf(intArrayOf(0, 0, 1000000000, 1000000000))),
21+
equalTo(49)
22+
)
23+
}
24+
}

0 commit comments

Comments
 (0)