Skip to content

Commit bfce334

Browse files
authored
Added tasks 904, 905, 906, 907
1 parent 6814f33 commit bfce334

File tree

13 files changed

+394
-0
lines changed

13 files changed

+394
-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+
| 0907 |[Sum of Subarray Minimums](src/main/kotlin/g0901_1000/s0907_sum_of_subarray_minimums/Solution.kt)| Medium | Array, Dynamic_Programming, Stack, Monotonic_Stack | 341 | 100.00
1730+
| 0906 |[Super Palindromes](src/main/kotlin/g0901_1000/s0906_super_palindromes/Solution.kt)| Hard | Math, Enumeration | 153 | 100.00
1731+
| 0905 |[Sort Array By Parity](src/main/kotlin/g0901_1000/s0905_sort_array_by_parity/Solution.kt)| Easy | Array, Sorting, Two_Pointers | 219 | 75.00
1732+
| 0904 |[Fruit Into Baskets](src/main/kotlin/g0901_1000/s0904_fruit_into_baskets/Solution.kt)| Medium | Array, Hash_Table, Sliding_Window | 371 | 100.00
17291733
| 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
17301734
| 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
17311735
| 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
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0901_1000.s0904_fruit_into_baskets
2+
3+
// #Medium #Array #Hash_Table #Sliding_Window #2023_04_14_Time_371_ms_(100.00%)_Space_46_MB_(87.50%)
4+
5+
class Solution {
6+
fun totalFruit(fruits: IntArray): Int {
7+
var end = 1
8+
var basket1 = fruits[0]
9+
var basket2 = -1
10+
var secondFruitIndex = -1
11+
var maxTotal = 1
12+
var counter = 1
13+
while (end < fruits.size) {
14+
if (fruits[end - 1] != fruits[end]) {
15+
if (basket2 == -1) {
16+
basket2 = fruits[end]
17+
secondFruitIndex = end
18+
counter++
19+
} else if (fruits[end] == basket1) {
20+
basket1 = basket2
21+
basket2 = fruits[end]
22+
secondFruitIndex = end
23+
counter++
24+
} else {
25+
counter = end - secondFruitIndex + 1
26+
basket1 = basket2
27+
basket2 = fruits[end]
28+
secondFruitIndex = end
29+
}
30+
} else {
31+
counter++
32+
}
33+
end++
34+
maxTotal = maxTotal.coerceAtLeast(counter)
35+
}
36+
return maxTotal
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
904\. Fruit Into Baskets
2+
3+
Medium
4+
5+
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array `fruits` where `fruits[i]` is the **type** of fruit the <code>i<sup>th</sup></code> tree produces.
6+
7+
You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
8+
9+
* You only have **two** baskets, and each basket can only hold a **single type** of fruit. There is no limit on the amount of fruit each basket can hold.
10+
* Starting from any tree of your choice, you must pick **exactly one fruit** from **every** tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
11+
* Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
12+
13+
Given the integer array `fruits`, return _the **maximum** number of fruits you can pick_.
14+
15+
**Example 1:**
16+
17+
**Input:** fruits = [<ins>1,2,1</ins>]
18+
19+
**Output:** 3
20+
21+
**Explanation:** We can pick from all 3 trees.
22+
23+
**Example 2:**
24+
25+
**Input:** fruits = [0,<ins>1,2,2</ins>]
26+
27+
**Output:** 3
28+
29+
**Explanation:** We can pick from trees [1,2,2]. If we had started at the first tree, we would only pick from trees [0,1].
30+
31+
**Example 3:**
32+
33+
**Input:** fruits = [1,<ins>2,3,2,2</ins>]
34+
35+
**Output:** 4
36+
37+
**Explanation:** We can pick from trees [2,3,2,2]. If we had started at the first tree, we would only pick from trees [1,2].
38+
39+
**Constraints:**
40+
41+
* <code>1 <= fruits.length <= 10<sup>5</sup></code>
42+
* `0 <= fruits[i] < fruits.length`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0901_1000.s0905_sort_array_by_parity
2+
3+
// #Easy #Array #Sorting #Two_Pointers #2023_04_14_Time_219_ms_(75.00%)_Space_36.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun sortArrayByParity(nums: IntArray): IntArray {
7+
var temp: Int
8+
var i = 0
9+
for (k in nums.indices) {
10+
if (nums[k] % 2 == 0) {
11+
temp = nums[k]
12+
nums[k] = nums[i]
13+
nums[i] = temp
14+
i++
15+
}
16+
}
17+
return nums
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
905\. Sort Array By Parity
2+
3+
Easy
4+
5+
Given an integer array `nums`, move all the even integers at the beginning of the array followed by all the odd integers.
6+
7+
Return _**any array** that satisfies this condition_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [3,1,2,4]
12+
13+
**Output:** [2,4,3,1]
14+
15+
**Explanation:** The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [0]
20+
21+
**Output:** [0]
22+
23+
**Constraints:**
24+
25+
* `1 <= nums.length <= 5000`
26+
* `0 <= nums[i] <= 5000`
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g0901_1000.s0906_super_palindromes
2+
3+
// #Hard #Math #Enumeration #2023_04_14_Time_153_ms_(100.00%)_Space_33.1_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun superpalindromesInRange(left: String, right: String): Int {
8+
val l = left.toLong()
9+
val r = right.toLong()
10+
var cnt = 0
11+
var cur: Long = 1
12+
while (true) {
13+
val p1 = getPalindromeIncLastDigit(cur)
14+
val p2 = getPalindromeExcLastDigit(cur)
15+
val sq1 = p1 * p1
16+
val sq2 = p2 * p2
17+
if (sq2 > r) {
18+
break
19+
}
20+
if (sq1 in l..r && isPalindrome(sq1)) {
21+
cnt++
22+
}
23+
if (sq2 >= l && isPalindrome(sq2)) {
24+
cnt++
25+
}
26+
cur++
27+
}
28+
return cnt
29+
}
30+
31+
private fun isPalindrome(`val`: Long): Boolean {
32+
var `val` = `val`
33+
var construct: Long = 0
34+
if (`val` % 10 == 0L && `val` >= 10) {
35+
return false
36+
}
37+
while (construct < `val`) {
38+
construct = construct * 10 + `val` % 10
39+
`val` /= 10
40+
}
41+
return construct == `val` || construct / 10 == `val`
42+
}
43+
44+
private fun getPalindromeIncLastDigit(`val`: Long): Long {
45+
var `val` = `val`
46+
var copy = `val`
47+
while (copy != 0L) {
48+
`val` = `val` * 10 + copy % 10
49+
copy /= 10
50+
}
51+
return `val`
52+
}
53+
54+
private fun getPalindromeExcLastDigit(`val`: Long): Long {
55+
var `val` = `val`
56+
var copy = `val` / 10
57+
while (copy != 0L) {
58+
`val` = `val` * 10 + copy % 10
59+
copy /= 10
60+
}
61+
return `val`
62+
}
63+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
906\. Super Palindromes
2+
3+
Hard
4+
5+
Let's say a positive integer is a **super-palindrome** if it is a palindrome, and it is also the square of a palindrome.
6+
7+
Given two positive integers `left` and `right` represented as strings, return _the number of **super-palindromes** integers in the inclusive range_ `[left, right]`.
8+
9+
**Example 1:**
10+
11+
**Input:** left = "4", right = "1000"
12+
13+
**Output:** 4
14+
15+
**Explanation:**: 4, 9, 121, and 484 are superpalindromes. Note that 676 is not a superpalindrome: 26 \* 26 = 676, but 26 is not a palindrome.
16+
17+
**Example 2:**
18+
19+
**Input:** left = "1", right = "2"
20+
21+
**Output:** 1
22+
23+
**Constraints:**
24+
25+
* `1 <= left.length, right.length <= 18`
26+
* `left` and `right` consist of only digits.
27+
* `left` and `right` cannot have leading zeros.
28+
* `left` and `right` represent integers in the range <code>[1, 10<sup>18</sup> - 1]</code>.
29+
* `left` is less than or equal to `right`.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0901_1000.s0907_sum_of_subarray_minimums
2+
3+
// #Medium #Array #Dynamic_Programming #Stack #Monotonic_Stack
4+
// #2023_04_14_Time_341_ms_(100.00%)_Space_44.3_MB_(100.00%)
5+
6+
class Solution {
7+
private fun calculateRight(i: Int, start: Int, right: IntArray, arr: IntArray, len: Int): Int {
8+
if (start >= len) {
9+
return 0
10+
}
11+
return if (arr[start] < arr[i]) {
12+
0
13+
} else (1 + right[start] + calculateRight(i, start + right[start] + 1, right, arr, len)) % MOD
14+
}
15+
16+
private fun calculateLeft(i: Int, start: Int, left: IntArray, arr: IntArray, len: Int): Int {
17+
if (start < 0) {
18+
return 0
19+
}
20+
return if (arr[start] <= arr[i]) {
21+
0
22+
} else (1 + left[start] + calculateLeft(i, start - left[start] - 1, left, arr, len)) % MOD
23+
}
24+
25+
fun sumSubarrayMins(arr: IntArray): Int {
26+
val len = arr.size
27+
val right = IntArray(len)
28+
val left = IntArray(len)
29+
right[len - 1] = 0
30+
for (i in len - 2 downTo 0) {
31+
right[i] = calculateRight(i, i + 1, right, arr, len)
32+
}
33+
left[0] = 0
34+
for (i in 1 until len) {
35+
left[i] = calculateLeft(i, i - 1, left, arr, len)
36+
}
37+
var answer = 0
38+
for (i in 0 until len) {
39+
val model: Long = 1000000007
40+
answer += ((1 + left[i]) * (1 + right[i]).toLong() % model * arr[i] % model).toInt()
41+
answer %= MOD
42+
}
43+
return answer
44+
}
45+
46+
companion object {
47+
private const val MOD = 1000000007
48+
}
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
907\. Sum of Subarray Minimums
2+
3+
Medium
4+
5+
Given an array of integers arr, find the sum of `min(b)`, where `b` ranges over every (contiguous) subarray of `arr`. Since the answer may be large, return the answer **modulo** <code>10<sup>9</sup> + 7</code>.
6+
7+
**Example 1:**
8+
9+
**Input:** arr = [3,1,2,4]
10+
11+
**Output:** 17
12+
13+
**Explanation:**
14+
15+
Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4].
16+
17+
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
18+
19+
Sum is 17.
20+
21+
**Example 2:**
22+
23+
**Input:** arr = [11,81,94,43,3]
24+
25+
**Output:** 444
26+
27+
**Constraints:**
28+
29+
* <code>1 <= arr.length <= 3 * 10<sup>4</sup></code>
30+
* <code>1 <= arr[i] <= 3 * 10<sup>4</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0901_1000.s0904_fruit_into_baskets
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 totalFruit() {
10+
assertThat(Solution().totalFruit(intArrayOf(1, 2, 1)), equalTo(3))
11+
}
12+
13+
@Test
14+
fun totalFruit2() {
15+
assertThat(Solution().totalFruit(intArrayOf(0, 1, 2, 2)), equalTo(3))
16+
}
17+
18+
@Test
19+
fun totalFruit3() {
20+
assertThat(Solution().totalFruit(intArrayOf(1, 2, 3, 2, 2)), equalTo(4))
21+
}
22+
}

0 commit comments

Comments
 (0)