Skip to content

Commit 3437cad

Browse files
authored
Added tasks 846, 847, 848, 849
1 parent 02f5d21 commit 3437cad

File tree

13 files changed

+404
-0
lines changed

13 files changed

+404
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
109109

110110
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
111111
|-|-|-|-|-|-
112+
| 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 | 164 | 100.00
112113

113114
#### Day 11 Breadth First Search
114115

@@ -1717,6 +1718,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17171718
| 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
17181719
| 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
17191720
| 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
1721+
| 0849 |[Maximize Distance to Closest Person](src/main/kotlin/g0801_0900/s0849_maximize_distance_to_closest_person/Solution.kt)| Medium | Array | 196 | 88.46
1722+
| 0848 |[Shifting Letters](src/main/kotlin/g0801_0900/s0848_shifting_letters/Solution.kt)| Medium | Array, String | 537 | 93.75
1723+
| 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
1724+
| 0846 |[Hand of Straights](src/main/kotlin/g0801_0900/s0846_hand_of_straights/Solution.kt)| Medium | Array, Hash_Table, Sorting, Greedy | 306 | 96.15
17201725
| 0845 |[Longest Mountain in Array](src/main/kotlin/g0801_0900/s0845_longest_mountain_in_array/Solution.kt)| Medium | Array, Dynamic_Programming, Two_Pointers, Enumeration | 222 | 100.00
17211726
| 0844 |[Backspace String Compare](src/main/kotlin/g0801_0900/s0844_backspace_string_compare/Solution.kt)| Easy | String, Two_Pointers, Stack, Simulation, Algorithm_II_Day_4_Two_Pointers, Level_1_Day_14_Stack | 126 | 98.31
17221727
| 0843 |[Guess the Word](src/main/kotlin/g0801_0900/s0843_guess_the_word/Solution.kt)| Hard | Array, String, Math, Game_Theory, Interactive | 75 | 100.00
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0801_0900.s0846_hand_of_straights
2+
3+
// #Medium #Array #Hash_Table #Sorting #Greedy
4+
// #2023_03_29_Time_306_ms_(96.15%)_Space_39.4_MB_(46.15%)
5+
6+
class Solution {
7+
fun isNStraightHand(hand: IntArray, groupSize: Int): Boolean {
8+
val len = hand.size
9+
if (len % groupSize != 0) {
10+
return false
11+
}
12+
hand.sort()
13+
val map: MutableMap<Int, Int> = HashMap()
14+
for (num in hand) {
15+
var cnt = map.getOrDefault(num, 0)
16+
map[num] = ++cnt
17+
}
18+
for (num in hand) {
19+
var cnt = map[num]!!
20+
if (cnt <= 0) {
21+
continue
22+
}
23+
map[num] = --cnt
24+
var loop = 1
25+
while (loop < groupSize) {
26+
var curCnt = map.getOrDefault(num + loop, 0)
27+
if (curCnt <= 0) {
28+
return false
29+
}
30+
map[num + loop] = --curCnt
31+
++loop
32+
}
33+
}
34+
return true
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
846\. Hand of Straights
2+
3+
Medium
4+
5+
Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size `groupSize`, and consists of `groupSize` consecutive cards.
6+
7+
Given an integer array `hand` where `hand[i]` is the value written on the <code>i<sup>th</sup></code> card and an integer `groupSize`, return `true` if she can rearrange the cards, or `false` otherwise.
8+
9+
**Example 1:**
10+
11+
**Input:** hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
12+
13+
**Output:** true
14+
15+
**Explanation:** Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]
16+
17+
**Example 2:**
18+
19+
**Input:** hand = [1,2,3,4,5], groupSize = 4
20+
21+
**Output:** false
22+
23+
**Explanation:** Alice's hand can not be rearranged into groups of 4.
24+
25+
**Constraints:**
26+
27+
* <code>1 <= hand.length <= 10<sup>4</sup></code>
28+
* <code>0 <= hand[i] <= 10<sup>9</sup></code>
29+
* `1 <= groupSize <= hand.length`
30+
31+
**Note:** This question is the same as 1296: [https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0801_0900.s0847_shortest_path_visiting_all_nodes
2+
3+
// #Hard #Dynamic_Programming #Breadth_First_Search #Bit_Manipulation #Graph #Bitmask
4+
// #Graph_Theory_I_Day_10_Standard_Traversal
5+
// #2023_03_29_Time_164_ms_(100.00%)_Space_35.8_MB_(88.89%)
6+
7+
import java.util.LinkedList
8+
import java.util.Objects
9+
import java.util.Queue
10+
11+
class Solution {
12+
fun shortestPathLength(graph: Array<IntArray>): Int {
13+
val target = (1 shl graph.size) - 1
14+
val q: Queue<IntArray> = LinkedList()
15+
for (i in graph.indices) {
16+
q.offer(intArrayOf(i, 1 shl i))
17+
}
18+
var steps = 0
19+
val visited = Array(graph.size) {
20+
BooleanArray(
21+
target + 1
22+
)
23+
}
24+
while (!q.isEmpty()) {
25+
val size = q.size
26+
for (i in 0 until size) {
27+
val curr = q.poll()
28+
val currNode = Objects.requireNonNull(curr)[0]
29+
val currState = curr[1]
30+
if (currState == target) {
31+
return steps
32+
}
33+
for (n in graph[currNode]) {
34+
val newState = currState or (1 shl n)
35+
if (visited[n][newState]) {
36+
continue
37+
}
38+
visited[n][newState] = true
39+
q.offer(intArrayOf(n, newState))
40+
}
41+
}
42+
++steps
43+
}
44+
return -1
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
847\. Shortest Path Visiting All Nodes
2+
3+
Hard
4+
5+
You have an undirected, connected graph of `n` nodes labeled from `0` to `n - 1`. You are given an array `graph` where `graph[i]` is a list of all the nodes connected with node `i` by an edge.
6+
7+
Return _the length of the shortest path that visits every node_. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/05/12/shortest1-graph.jpg)
12+
13+
**Input:** graph = [[1,2,3],[0],[0],[0]]
14+
15+
**Output:** 4
16+
17+
**Explanation:** One possible path is [1,0,2,0,3]
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/05/12/shortest2-graph.jpg)
22+
23+
**Input:** graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
24+
25+
**Output:** 4
26+
27+
**Explanation:** One possible path is [0,1,4,2,3]
28+
29+
**Constraints:**
30+
31+
* `n == graph.length`
32+
* `1 <= n <= 12`
33+
* `0 <= graph[i].length < n`
34+
* `graph[i]` does not contain `i`.
35+
* If `graph[a]` contains `b`, then `graph[b]` contains `a`.
36+
* The input graph is always connected.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0801_0900.s0848_shifting_letters
2+
3+
// #Medium #Array #String #2023_03_29_Time_537_ms_(93.75%)_Space_49.3_MB_(93.75%)
4+
5+
class Solution {
6+
fun shiftingLetters(s: String, shifts: IntArray): String {
7+
val n = shifts.size
8+
var runningSum = 0
9+
for (i in n - 1 downTo 0) {
10+
shifts[i] = (shifts[i] + runningSum) % 26
11+
runningSum = shifts[i]
12+
}
13+
val str = StringBuilder()
14+
var i = 0
15+
for (c in s.toCharArray()) {
16+
val correctShift = (c.code - 'a'.code + shifts[i]) % 26
17+
str.append(('a'.code + correctShift).toChar())
18+
i++
19+
}
20+
return str.toString()
21+
}
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
848\. Shifting Letters
2+
3+
Medium
4+
5+
You are given a string `s` of lowercase English letters and an integer array `shifts` of the same length.
6+
7+
Call the `shift()` of a letter, the next letter in the alphabet, (wrapping around so that `'z'` becomes `'a'`).
8+
9+
* For example, `shift('a') = 'b'`, `shift('t') = 'u'`, and `shift('z') = 'a'`.
10+
11+
Now for each `shifts[i] = x`, we want to shift the first `i + 1` letters of `s`, `x` times.
12+
13+
Return _the final string after all such shifts to s are applied_.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "abc", shifts = [3,5,9]
18+
19+
**Output:** "rpl"
20+
21+
**Explanation:** We start with "abc".
22+
23+
After shifting the first 1 letters of s by 3, we have "dbc".
24+
25+
After shifting the first 2 letters of s by 5, we have "igc".
26+
27+
After shifting the first 3 letters of s by 9, we have "rpl", the answer.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "aaa", shifts = [1,2,3]
32+
33+
**Output:** "gfd"
34+
35+
**Constraints:**
36+
37+
* <code>1 <= s.length <= 10<sup>5</sup></code>
38+
* `s` consists of lowercase English letters.
39+
* `shifts.length == s.length`
40+
* <code>0 <= shifts[i] <= 10<sup>9</sup></code>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g0801_0900.s0849_maximize_distance_to_closest_person
2+
3+
// #Medium #Array #2023_03_29_Time_196_ms_(88.46%)_Space_37.6_MB_(76.92%)
4+
5+
class Solution {
6+
private var maxDist = 0
7+
8+
fun maxDistToClosest(seats: IntArray): Int {
9+
for (i in seats.indices) {
10+
if (seats[i] == 0) {
11+
extend(seats, i)
12+
}
13+
}
14+
return maxDist
15+
}
16+
17+
private fun extend(seats: IntArray, position: Int) {
18+
var left = position - 1
19+
var right = position + 1
20+
var leftMinDistance = 1
21+
while (left >= 0) {
22+
if (seats[left] == 0) {
23+
leftMinDistance++
24+
left--
25+
} else {
26+
break
27+
}
28+
}
29+
var rightMinDistance = 1
30+
while (right < seats.size) {
31+
if (seats[right] == 0) {
32+
rightMinDistance++
33+
right++
34+
} else {
35+
break
36+
}
37+
}
38+
val maxReach: Int = when (position) {
39+
0 -> rightMinDistance
40+
seats.size - 1 -> leftMinDistance
41+
else -> leftMinDistance.coerceAtMost(rightMinDistance)
42+
}
43+
maxDist = maxDist.coerceAtLeast(maxReach)
44+
}
45+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
849\. Maximize Distance to Closest Person
2+
3+
Medium
4+
5+
You are given an array representing a row of `seats` where `seats[i] = 1` represents a person sitting in the <code>i<sup>th</sup></code> seat, and `seats[i] = 0` represents that the <code>i<sup>th</sup></code> seat is empty **(0-indexed)**.
6+
7+
There is at least one empty seat, and at least one person sitting.
8+
9+
Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.
10+
11+
Return _that maximum distance to the closest person_.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/09/10/distance.jpg)
16+
17+
**Input:** seats = [1,0,0,0,1,0,1]
18+
19+
**Output:** 2
20+
21+
**Explanation:**
22+
23+
If Alex sits in the second open seat (i.e. seats[2]), then the closest person has distance 2.
24+
25+
If Alex sits in any other open seat, the closest person has distance 1.
26+
27+
Thus, the maximum distance to the closest person is 2.
28+
29+
**Example 2:**
30+
31+
**Input:** seats = [1,0,0,0]
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
If Alex sits in the last seat (i.e. seats[3]), the closest person is 3 seats away.
38+
39+
This is the maximum distance possible, so the answer is 3.
40+
41+
**Example 3:**
42+
43+
**Input:** seats = [0,1]
44+
45+
**Output:** 1
46+
47+
**Constraints:**
48+
49+
* <code>2 <= seats.length <= 2 * 10<sup>4</sup></code>
50+
* `seats[i]` is `0` or `1`.
51+
* At least one seat is **empty**.
52+
* At least one seat is **occupied**.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0801_0900.s0846_hand_of_straights
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 isNStraightHand() {
10+
assertThat(
11+
Solution().isNStraightHand(intArrayOf(1, 2, 3, 6, 2, 3, 4, 7, 8), 3),
12+
equalTo(true)
13+
)
14+
}
15+
16+
@Test
17+
fun isNStraightHand2() {
18+
assertThat(Solution().isNStraightHand(intArrayOf(1, 2, 3, 4, 5), 4), equalTo(false))
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0801_0900.s0847_shortest_path_visiting_all_nodes
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 shortestPathLength() {
10+
assertThat(
11+
Solution().shortestPathLength(arrayOf(intArrayOf(1, 2, 3), intArrayOf(0), intArrayOf(0), intArrayOf(0))),
12+
equalTo(4)
13+
)
14+
}
15+
16+
@Test
17+
fun shortestPathLength2() {
18+
assertThat(
19+
Solution()
20+
.shortestPathLength(
21+
arrayOf(
22+
intArrayOf(1),
23+
intArrayOf(0, 2, 3),
24+
intArrayOf(1, 3, 4),
25+
intArrayOf(2),
26+
intArrayOf(1, 2)
27+
)
28+
),
29+
equalTo(4)
30+
)
31+
}
32+
}

0 commit comments

Comments
 (0)