Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bf9765d

Browse filesBrowse files
authored
Added tasks 879, 880, 881, 882
1 parent 0894ed5 commit bf9765d
Copy full SHA for bf9765d

File tree

Expand file treeCollapse file tree

13 files changed

+421
-0
lines changed
Filter options
Expand file treeCollapse file tree

13 files changed

+421
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17241724
|------|----------------|-------------|-------------|----------|---------
17251725
| 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
17261726
| 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
1727+
| 0882 |[Reachable Nodes In Subdivided Graph](src/main/kotlin/g0801_0900/s0882_reachable_nodes_in_subdivided_graph/Solution.kt)| Hard | Heap_Priority_Queue, Graph, Shortest_Path | 434 | 100.00
1728+
| 0881 |[Boats to Save People](src/main/kotlin/g0801_0900/s0881_boats_to_save_people/Solution.kt)| Medium | Array, Sorting, Greedy, Two_Pointers | 370 | 96.07
1729+
| 0880 |[Decoded String at Index](src/main/kotlin/g0801_0900/s0880_decoded_string_at_index/Solution.kt)| Medium | String, Stack | 134 | 100.00
1730+
| 0879 |[Profitable Schemes](src/main/kotlin/g0801_0900/s0879_profitable_schemes/Solution.kt)| Hard | Array, Dynamic_Programming | 198 | 75.00
17271731
| 0878 |[Nth Magical Number](src/main/kotlin/g0801_0900/s0878_nth_magical_number/Solution.kt)| Hard | Math, Binary_Search | 124 | 100.00
17281732
| 0877 |[Stone Game](src/main/kotlin/g0801_0900/s0877_stone_game/Solution.kt)| Medium | Array, Dynamic_Programming, Math, Game_Theory | 136 | 88.24
17291733
| 0876 |[Middle of the Linked List](src/main/kotlin/g0801_0900/s0876_middle_of_the_linked_list/Solution.kt)| Easy | Two_Pointers, Linked_List, Algorithm_I_Day_5_Two_Pointers, Programming_Skills_I_Day_10_Linked_List_and_Tree, Level_1_Day_4_Linked_List, Udemy_Linked_List | 136 | 76.52
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0801_0900.s0879_profitable_schemes
2+
3+
// #Hard #Array #Dynamic_Programming #2023_04_08_Time_198_ms_(75.00%)_Space_35.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun profitableSchemes(n: Int, minProfit: Int, group: IntArray, profit: IntArray): Int {
7+
val dp = Array(n + 1) { LongArray(minProfit + 1) }
8+
val modulus = 1000000007L
9+
for (i in dp.indices) {
10+
dp[i][0] = 1
11+
}
12+
for (i in group.indices) {
13+
val currWorker = group[i]
14+
val currProfit = profit[i]
15+
for (j in dp.size - 1 downTo currWorker) {
16+
for (k in dp[j].indices.reversed()) {
17+
dp[j][k] = (
18+
(dp[j][k] + dp[j - currWorker][(k - currProfit).coerceAtLeast(0)]) %
19+
modulus
20+
)
21+
}
22+
}
23+
}
24+
return dp[n][minProfit].toInt()
25+
}
26+
}
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
879\. Profitable Schemes
2+
3+
Hard
4+
5+
There is a group of `n` members, and a list of various crimes they could commit. The <code>i<sup>th</sup></code> crime generates a `profit[i]` and requires `group[i]` members to participate in it. If a member participates in one crime, that member can't participate in another crime.
6+
7+
Let's call a **profitable scheme** any subset of these crimes that generates at least `minProfit` profit, and the total number of members participating in that subset of crimes is at most `n`.
8+
9+
Return the number of schemes that can be chosen. Since the answer may be very large, **return it modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 5, minProfit = 3, group = [2,2], profit = [2,3]
14+
15+
**Output:** 2
16+
17+
**Explanation:** To make a profit of at least 3, the group could either commit crimes 0 and 1, or just crime 1. In total, there are 2 schemes.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
22+
23+
**Output:** 7
24+
25+
**Explanation:** To make a profit of at least 5, the group could commit any crimes, as long as they commit one. There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).
26+
27+
**Constraints:**
28+
29+
* `1 <= n <= 100`
30+
* `0 <= minProfit <= 100`
31+
* `1 <= group.length <= 100`
32+
* `1 <= group[i] <= 100`
33+
* `profit.length == group.length`
34+
* `0 <= profit[i] <= 100`
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0801_0900.s0880_decoded_string_at_index
2+
3+
// #Medium #String #Stack #2023_04_08_Time_134_ms_(100.00%)_Space_33.9_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun decodeAtIndex(s: String, k: Int): String {
8+
var k = k
9+
var i = 0
10+
var count: Long = 0
11+
while (i < s.length && count <= k) {
12+
val c = s[i]
13+
count = if (Character.isDigit(c)) count * (c.code - '0'.code) else count + 1
14+
i++
15+
}
16+
i--
17+
while (i < s.length) {
18+
val c = s[i]
19+
if (Character.isDigit(c)) {
20+
count /= (c.code - '0'.code).toLong()
21+
k %= count.toInt()
22+
} else {
23+
if (k % count == 0L) {
24+
break
25+
}
26+
--count
27+
}
28+
i--
29+
}
30+
return s[i].toString()
31+
}
32+
}
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
880\. Decoded String at Index
2+
3+
Medium
4+
5+
You are given an encoded string `s`. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:
6+
7+
* If the character read is a letter, that letter is written onto the tape.
8+
* If the character read is a digit `d`, the entire current tape is repeatedly written `d - 1` more times in total.
9+
10+
Given an integer `k`, return _the_ <code>k<sup>th</sup></code> _letter (**1-indexed)** in the decoded string_.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "leet2code3", k = 10
15+
16+
**Output:** "o"
17+
18+
**Explanation:** The decoded string is "leetleetcodeleetleetcodeleetleetcode". The 10<sup>th</sup> letter in the string is "o".
19+
20+
**Example 2:**
21+
22+
**Input:** s = "ha22", k = 5
23+
24+
**Output:** "h"
25+
26+
**Explanation:** The decoded string is "hahahaha". The 5<sup>th</sup> letter is "h".
27+
28+
**Example 3:**
29+
30+
**Input:** s = "a2345678999999999999999", k = 1
31+
32+
**Output:** "a"
33+
34+
**Explanation:** The decoded string is "a" repeated 8301530446056247680 times. The 1<sup>st</sup> letter is "a".
35+
36+
**Constraints:**
37+
38+
* `2 <= s.length <= 100`
39+
* `s` consists of lowercase English letters and digits `2` through `9`.
40+
* `s` starts with a letter.
41+
* <code>1 <= k <= 10<sup>9</sup></code>
42+
* It is guaranteed that `k` is less than or equal to the length of the decoded string.
43+
* The decoded string is guaranteed to have less than <code>2<sup>63</sup></code> letters.
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0801_0900.s0881_boats_to_save_people
2+
3+
// #Medium #Array #Sorting #Greedy #Two_Pointers
4+
// #2023_04_08_Time_370_ms_(96.07%)_Space_44.8_MB_(99.70%)
5+
6+
class Solution {
7+
fun numRescueBoats(people: IntArray, limit: Int): Int {
8+
people.sort()
9+
var i = 0
10+
var j = people.size - 1
11+
var boats = 0
12+
while (i < j) {
13+
if (people[i] + people[j] <= limit) {
14+
boats++
15+
i++
16+
j--
17+
} else if (people[i] + people[j] > limit) {
18+
boats++
19+
j--
20+
}
21+
}
22+
return if (i == j) {
23+
boats + 1
24+
} else boats
25+
}
26+
}
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
881\. Boats to Save People
2+
3+
Medium
4+
5+
You are given an array `people` where `people[i]` is the weight of the <code>i<sup>th</sup></code> person, and an **infinite number of boats** where each boat can carry a maximum weight of `limit`. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most `limit`.
6+
7+
Return _the minimum number of boats to carry every given person_.
8+
9+
**Example 1:**
10+
11+
**Input:** people = [1,2], limit = 3
12+
13+
**Output:** 1
14+
15+
**Explanation:** 1 boat (1, 2)
16+
17+
**Example 2:**
18+
19+
**Input:** people = [3,2,2,1], limit = 3
20+
21+
**Output:** 3
22+
23+
**Explanation:** 3 boats (1, 2), (2) and (3)
24+
25+
**Example 3:**
26+
27+
**Input:** people = [3,5,3,4], limit = 5
28+
29+
**Output:** 4
30+
31+
**Explanation:** 4 boats (3), (3), (4), (5)
32+
33+
**Constraints:**
34+
35+
* <code>1 <= people.length <= 5 * 10<sup>4</sup></code>
36+
* <code>1 <= people[i] <= limit <= 3 * 10<sup>4</sup></code>
+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g0801_0900.s0882_reachable_nodes_in_subdivided_graph
2+
3+
// #Hard #Heap_Priority_Queue #Graph #Shortest_Path
4+
// #2023_04_08_Time_434_ms_(100.00%)_Space_52_MB_(100.00%)
5+
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun reachableNodes(edges: Array<IntArray>, maxMoves: Int, n: Int): Int {
10+
val adList = getAdList(edges, n)
11+
val pQueue = PriorityQueue { a: IntArray, b: IntArray ->
12+
a[1] - b[1]
13+
}
14+
val minDis = IntArray(n)
15+
var res = 0
16+
pQueue.add(intArrayOf(0, 0))
17+
while (pQueue.size > 0) {
18+
val poll = pQueue.poll()
19+
val node = poll[0]
20+
val dist = poll[1]
21+
if (minDis[node] > 0) continue
22+
res++
23+
minDis[node] = dist
24+
for (child in adList[node]!!) {
25+
val cNode = child!![0]
26+
val weight = child[1]
27+
if (cNode != 0 && minDis[cNode] == 0) {
28+
res += (maxMoves - dist).coerceAtMost(weight)
29+
val cNodeDist = dist + weight + 1
30+
if (cNodeDist <= maxMoves) pQueue.add(intArrayOf(cNode, cNodeDist))
31+
} else {
32+
res += (weight - (maxMoves - minDis[cNode]).coerceAtMost(weight)).coerceAtMost(
33+
(maxMoves - dist).coerceAtMost(weight)
34+
)
35+
}
36+
}
37+
}
38+
return res
39+
}
40+
41+
private fun getAdList(edges: Array<IntArray>, n: Int): Array<ArrayList<IntArray?>?> {
42+
val adList: Array<ArrayList<IntArray?>?> = arrayOfNulls<ArrayList<IntArray?>?>(n)
43+
adList[0] = ArrayList()
44+
for (edge in edges) {
45+
val s = edge[0]
46+
val d = edge[1]
47+
val w = edge[2]
48+
if (adList[s] == null) adList[s] = ArrayList()
49+
if (adList[d] == null) adList[d] = ArrayList()
50+
adList[s]?.add(intArrayOf(d, w))
51+
adList[d]?.add(intArrayOf(s, w))
52+
}
53+
return adList
54+
}
55+
}
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
882\. Reachable Nodes In Subdivided Graph
2+
3+
Hard
4+
5+
You are given an undirected graph (the **"original graph"**) with `n` nodes labeled from `0` to `n - 1`. You decide to **subdivide** each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge.
6+
7+
The graph is given as a 2D array of `edges` where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, cnt<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the original graph, and <code>cnt<sub>i</sub></code> is the total number of new nodes that you will **subdivide** the edge into. Note that <code>cnt<sub>i</sub> == 0</code> means you will not subdivide the edge.
8+
9+
To **subdivide** the edge <code>[u<sub>i</sub>, v<sub>i</sub>]</code>, replace it with <code>(cnt<sub>i</sub> + 1)</code> new edges and <code>cnt<sub>i</sub></code> new nodes. The new nodes are <code>x<sub>1</sub></code>, <code>x<sub>2</sub></code>, ..., <code>x<sub>cnt<sub>i</sub></sub></code>, and the new edges are <code>[u<sub>i</sub>, x<sub>1</sub>]</code>, <code>[x<sub>1</sub>, x<sub>2</sub>]</code>, <code>[x<sub>2</sub>, x<sub>3</sub>]</code>, ..., <code>[x<sub>cnt<sub>i</sub>-1</sub>, x<sub>cnt<sub>i</sub></sub>]</code>, <code>[x<sub>cnt<sub>i</sub></sub>, v<sub>i</sub>]</code>.
10+
11+
In this **new graph**, you want to know how many nodes are **reachable** from the node `0`, where a node is **reachable** if the distance is `maxMoves` or less.
12+
13+
Given the original graph and `maxMoves`, return _the number of nodes that are **reachable** from node_ `0` _in the new graph_.
14+
15+
**Example 1:**
16+
17+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/01/origfinal.png)
18+
19+
**Input:** edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
20+
21+
**Output:** 13
22+
23+
**Explanation:** The edge subdivisions are shown in the image above. The nodes that are reachable are highlighted in yellow.
24+
25+
**Example 2:**
26+
27+
**Input:** edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], maxMoves = 10, n = 4
28+
29+
**Output:** 23
30+
31+
**Example 3:**
32+
33+
**Input:** edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5
34+
35+
**Output:** 1
36+
37+
**Explanation:** Node 0 is disconnected from the rest of the graph, so only node 0 is reachable.
38+
39+
**Constraints:**
40+
41+
* <code>0 <= edges.length <= min(n * (n - 1) / 2, 10<sup>4</sup>)</code>
42+
* `edges[i].length == 3`
43+
* <code>0 <= u<sub>i</sub> < v<sub>i</sub> < n</code>
44+
* There are **no multiple edges** in the graph.
45+
* <code>0 <= cnt<sub>i</sub> <= 10<sup>4</sup></code>
46+
* <code>0 <= maxMoves <= 10<sup>9</sup></code>
47+
* `1 <= n <= 3000`
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0801_0900.s0879_profitable_schemes
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 profitableSchemes() {
10+
assertThat(
11+
Solution().profitableSchemes(5, 3, intArrayOf(2, 2), intArrayOf(2, 3)),
12+
equalTo(2)
13+
)
14+
}
15+
16+
@Test
17+
fun profitableSchemes2() {
18+
assertThat(
19+
Solution().profitableSchemes(10, 5, intArrayOf(2, 3, 5), intArrayOf(6, 7, 8)),
20+
equalTo(7)
21+
)
22+
}
23+
}
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0801_0900.s0880_decoded_string_at_index
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 decodeAtIndex() {
10+
assertThat(Solution().decodeAtIndex("leet2code3", 10), equalTo("o"))
11+
}
12+
13+
@Test
14+
fun decodeAtIndex2() {
15+
assertThat(Solution().decodeAtIndex("ha22", 5), equalTo("h"))
16+
}
17+
18+
@Test
19+
fun decodeAtIndex3() {
20+
assertThat(Solution().decodeAtIndex("a2345678999999999999999", 1), equalTo("a"))
21+
}
22+
}
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0801_0900.s0881_boats_to_save_people
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 numRescueBoats() {
10+
assertThat(Solution().numRescueBoats(intArrayOf(1, 2), 3), equalTo(1))
11+
}
12+
13+
@Test
14+
fun numRescueBoats2() {
15+
assertThat(Solution().numRescueBoats(intArrayOf(3, 2, 2, 1), 3), equalTo(3))
16+
}
17+
18+
@Test
19+
fun numRescueBoats3() {
20+
assertThat(Solution().numRescueBoats(intArrayOf(3, 5, 3, 4), 5), equalTo(4))
21+
}
22+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.