Skip to content

Commit b555f5b

Browse files
authored
Added tasks 900, 901, 902, 903
1 parent 7aa7e3a commit b555f5b

File tree

13 files changed

+380
-0
lines changed

13 files changed

+380
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17261726
|------|----------------|-------------|-------------|----------|---------
17271727
| 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
17281728
| 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
1729+
| 0903 |[Valid Permutations for DI Sequence](src/main/kotlin/g0901_1000/s0903_valid_permutations_for_di_sequence/Solution.kt)| Hard | Dynamic_Programming | 140 | 100.00
1730+
| 0902 |[Numbers At Most N Given Digit Set](src/main/kotlin/g0901_1000/s0902_numbers_at_most_n_given_digit_set/Solution.kt)| Hard | Array, Dynamic_Programming, Math, Binary_Search | 138 | 100.00
1731+
| 0901 |[Online Stock Span](src/main/kotlin/g0901_1000/s0901_online_stock_span/StockSpanner.kt)| Medium | Stack, Design, Monotonic_Stack, Data_Stream | 641 | 75.00
1732+
| 0900 |[RLE Iterator](src/main/kotlin/g0801_0900/s0900_rle_iterator/RLEIterator.kt)| Medium | Array, Design, Counting, Iterator | 175 | 83.33
17291733
| 0899 |[Orderly Queue](src/main/kotlin/g0801_0900/s0899_orderly_queue/Solution.kt)| Hard | String, Math, Sorting | 148 | 100.00
17301734
| 0898 |[Bitwise ORs of Subarrays](src/main/kotlin/g0801_0900/s0898_bitwise_ors_of_subarrays/Solution.kt)| Medium | Array, Dynamic_Programming, Bit_Manipulation | 812 | 100.00
17311735
| 0897 |[Increasing Order Search Tree](src/main/kotlin/g0801_0900/s0897_increasing_order_search_tree/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Stack, Binary_Search_Tree | 128 | 85.71
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0801_0900.s0900_rle_iterator
2+
3+
// #Medium #Array #Design #Counting #Iterator
4+
// #2023_04_13_Time_175_ms_(83.33%)_Space_36.2_MB_(83.33%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class RLEIterator(private val array: IntArray) {
8+
private var index = 0
9+
fun next(n: Int): Int {
10+
var n = n
11+
var lastElement = -1
12+
while (n > 0 && index < array.size) {
13+
if (array[index] > n) {
14+
array[index] -= n
15+
lastElement = array[index + 1]
16+
break
17+
} else if (array[index] == n) {
18+
array[index] = 0
19+
lastElement = array[index + 1]
20+
index += 2
21+
break
22+
} else {
23+
n -= array[index]
24+
index += 2
25+
}
26+
}
27+
return lastElement
28+
}
29+
}
30+
31+
/*
32+
* Your RLEIterator object will be instantiated and called as such:
33+
* var obj = RLEIterator(encoding)
34+
* var param_1 = obj.next(n)
35+
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
900\. RLE Iterator
2+
3+
Medium
4+
5+
We can use run-length encoding (i.e., **RLE**) to encode a sequence of integers. In a run-length encoded array of even length `encoding` (**0-indexed**), for all even `i`, `encoding[i]` tells us the number of times that the non-negative integer value `encoding[i + 1]` is repeated in the sequence.
6+
7+
* For example, the sequence `arr = [8,8,8,5,5]` can be encoded to be `encoding = [3,8,2,5]`. `encoding = [3,8,0,9,2,5]` and `encoding = [2,8,1,8,2,5]` are also valid **RLE** of `arr`.
8+
9+
Given a run-length encoded array, design an iterator that iterates through it.
10+
11+
Implement the `RLEIterator` class:
12+
13+
* `RLEIterator(int[] encoded)` Initializes the object with the encoded array `encoded`.
14+
* `int next(int n)` Exhausts the next `n` elements and returns the last element exhausted in this way. If there is no element left to exhaust, return `-1` instead.
15+
16+
**Example 1:**
17+
18+
**Input**
19+
20+
["RLEIterator", "next", "next", "next", "next"]
21+
22+
[[[3, 8, 0, 9, 2, 5]], [2], [1], [1], [2]]
23+
24+
**Output:** [null, 8, 8, 5, -1]
25+
26+
**Explanation:**
27+
28+
RLEIterator rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // This maps to the sequence [8,8,8,5,5].
29+
rLEIterator.next(2); // exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5].
30+
rLEIterator.next(1); // exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5].
31+
rLEIterator.next(1); // exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5].
32+
rLEIterator.next(2); // exhausts 2 terms, returning -1. This is because the first term exhausted was 5,
33+
// but the second term did not exist. Since the last term exhausted does not exist, we return -1.
34+
35+
**Constraints:**
36+
37+
* `2 <= encoding.length <= 1000`
38+
* `encoding.length` is even.
39+
* <code>0 <= encoding[i] <= 10<sup>9</sup></code>
40+
* <code>1 <= n <= 10<sup>9</sup></code>
41+
* At most `1000` calls will be made to `next`.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0901_1000.s0901_online_stock_span
2+
3+
// #Medium #Stack #Design #Monotonic_Stack #Data_Stream
4+
// #2023_04_13_Time_641_ms_(75.00%)_Space_66.5_MB_(10.71%)
5+
6+
import java.util.Deque
7+
import java.util.LinkedList
8+
9+
class StockSpanner {
10+
private val map: MutableMap<Int, Int>
11+
private val stocks: Deque<Int>
12+
private var index: Int
13+
14+
init {
15+
stocks = LinkedList()
16+
index = -1
17+
map = HashMap()
18+
stocks.push(-1)
19+
}
20+
21+
fun next(price: Int): Int {
22+
if (index != -1) {
23+
stocks.push(index)
24+
}
25+
map[++index] = price
26+
if (stocks.size == 1) {
27+
return index - stocks.peek()
28+
}
29+
while (stocks.size > 1 && map[stocks.peek()]!! <= price) {
30+
stocks.pop()
31+
}
32+
return index - stocks.peek()
33+
}
34+
}
35+
/*
36+
* Your StockSpanner object will be instantiated and called as such:
37+
* var obj = StockSpanner()
38+
* var param_1 = obj.next(price)
39+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
901\. Online Stock Span
2+
3+
Medium
4+
5+
Design an algorithm that collects daily price quotes for some stock and returns **the span** of that stock's price for the current day.
6+
7+
The **span** of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today's price.
8+
9+
* For example, if the price of a stock over the next `7` days were `[100,80,60,70,60,75,85]`, then the stock spans would be `[1,1,1,2,1,4,6]`.
10+
11+
Implement the `StockSpanner` class:
12+
13+
* `StockSpanner()` Initializes the object of the class.
14+
* `int next(int price)` Returns the **span** of the stock's price given that today's price is `price`.
15+
16+
**Example 1:**
17+
18+
**Input**
19+
20+
["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
21+
22+
[[], [100], [80], [60], [70], [60], [75], [85]]
23+
24+
**Output:** [null, 1, 1, 1, 2, 1, 4, 6]
25+
26+
**Explanation:**
27+
28+
StockSpanner stockSpanner = new StockSpanner();
29+
stockSpanner.next(100); // return 1
30+
stockSpanner.next(80); // return 1
31+
stockSpanner.next(60); // return 1
32+
stockSpanner.next(70); // return 2
33+
stockSpanner.next(60); // return 1
34+
stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price.
35+
stockSpanner.next(85); // return 6
36+
37+
**Constraints:**
38+
39+
* <code>1 <= price <= 10<sup>5</sup></code>
40+
* At most <code>10<sup>4</sup></code> calls will be made to `next`.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0901_1000.s0902_numbers_at_most_n_given_digit_set
2+
3+
// #Hard #Array #Dynamic_Programming #Math #Binary_Search
4+
// #2023_04_13_Time_138_ms_(100.00%)_Space_33.7_MB_(100.00%)
5+
6+
import kotlin.math.pow
7+
8+
class Solution {
9+
fun atMostNGivenDigitSet(digits: Array<String>, n: Int): Int {
10+
var ans = 0
11+
val num = "" + n
12+
val d = digits.size
13+
val l = num.length
14+
for (i in 1 until l) {
15+
ans += d.toDouble().pow(i.toDouble()).toInt()
16+
}
17+
for (i in 0 until l) {
18+
var flag = false
19+
for (digit in digits) {
20+
if (digit[0] < num[i]) {
21+
ans += d.toDouble().pow((l - i - 1) * 1.0).toInt()
22+
} else if (num[i] == digit[0]) {
23+
flag = true
24+
break
25+
}
26+
}
27+
if (!flag) {
28+
return ans
29+
}
30+
}
31+
return ans + 1
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
902\. Numbers At Most N Given Digit Set
2+
3+
Hard
4+
5+
Given an array of `digits` which is sorted in **non-decreasing** order. You can write numbers using each `digits[i]` as many times as we want. For example, if `digits = ['1','3','5']`, we may write numbers such as `'13'`, `'551'`, and `'1351315'`.
6+
7+
Return _the number of positive integers that can be generated_ that are less than or equal to a given integer `n`.
8+
9+
**Example 1:**
10+
11+
**Input:** digits = ["1","3","5","7"], n = 100
12+
13+
**Output:** 20
14+
15+
**Explanation:** The 20 numbers that can be written are: 1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
16+
17+
**Example 2:**
18+
19+
**Input:** digits = ["1","4","9"], n = 1000000000
20+
21+
**Output:** 29523
22+
23+
**Explanation:** We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers, 81 four digit numbers, 243 five digit numbers, 729 six digit numbers, 2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers. In total, this is 29523 integers that can be written using the digits array.
24+
25+
**Example 3:**
26+
27+
**Input:** digits = ["7"], n = 8
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `1 <= digits.length <= 9`
34+
* `digits[i].length == 1`
35+
* `digits[i]` is a digit from `'1'` to `'9'`.
36+
* All the values in `digits` are **unique**.
37+
* `digits` is sorted in **non-decreasing** order.
38+
* <code>1 <= n <= 10<sup>9</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0901_1000.s0903_valid_permutations_for_di_sequence
2+
3+
// #Hard #Dynamic_Programming #2023_04_13_Time_140_ms_(100.00%)_Space_34.9_MB_(100.00%)
4+
5+
class Solution {
6+
fun numPermsDISequence(s: String): Int {
7+
val n = s.length
8+
val mod = 1e9.toInt() + 7
9+
val dp = Array(n + 1) { IntArray(n + 1) }
10+
for (j in 0..n) {
11+
dp[0][j] = 1
12+
}
13+
for (i in 0 until n) {
14+
var cur = 0
15+
if (s[i] == 'I') {
16+
for (j in 0 until n - i) {
17+
cur = (cur + dp[i][j]) % mod
18+
dp[i + 1][j] = cur
19+
}
20+
} else {
21+
for (j in n - i - 1 downTo 0) {
22+
cur = (cur + dp[i][j + 1]) % mod
23+
dp[i + 1][j] = cur
24+
}
25+
}
26+
}
27+
return dp[n][0]
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
903\. Valid Permutations for DI Sequence
2+
3+
Hard
4+
5+
You are given a string `s` of length `n` where `s[i]` is either:
6+
7+
* `'D'` means decreasing, or
8+
* `'I'` means increasing.
9+
10+
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` is called a **valid permutation** if for all valid `i`:
11+
12+
* If `s[i] == 'D'`, then `perm[i] > perm[i + 1]`, and
13+
* If `s[i] == 'I'`, then `perm[i] < perm[i + 1]`.
14+
15+
Return _the number of **valid permutations**_ `perm`. Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
16+
17+
**Example 1:**
18+
19+
**Input:** s = "DID"
20+
21+
**Output:** 5
22+
23+
**Explanation:** The 5 valid permutations of (0, 1, 2, 3) are:
24+
25+
(1, 0, 3, 2)
26+
(2, 0, 3, 1)
27+
(2, 1, 3, 0)
28+
(3, 0, 2, 1)
29+
(3, 1, 2, 0)
30+
31+
**Example 2:**
32+
33+
**Input:** s = "D"
34+
35+
**Output:** 1
36+
37+
**Constraints:**
38+
39+
* `n == s.length`
40+
* `1 <= n <= 200`
41+
* `s[i]` is either `'I'` or `'D'`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0801_0900.s0900_rle_iterator
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class RLEIteratorTest {
8+
@Test
9+
fun rleIteratorTest() {
10+
val rleIterator = RLEIterator(intArrayOf(3, 8, 0, 9, 2, 5))
11+
assertThat(rleIterator.next(2), equalTo(8))
12+
assertThat(rleIterator.next(1), equalTo(8))
13+
assertThat(rleIterator.next(1), equalTo(5))
14+
assertThat(rleIterator.next(2), equalTo(-1))
15+
}
16+
}

0 commit comments

Comments
 (0)