Skip to content

Commit 117f143

Browse files
authored
Added tasks 396, 398, 399, 400.
1 parent 6828cb3 commit 117f143

File tree

13 files changed

+391
-0
lines changed

13 files changed

+391
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16181618
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer | 561 | 54.68
16191619
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
16201620
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
1621+
| 0400 |[Nth Digit](src/main/kotlin/g0301_0400/s0400_nth_digit/Solution.kt)| Medium | Math, Binary_Search | 271 | 50.00
1622+
| 0399 |[Evaluate Division](src/main/kotlin/g0301_0400/s0399_evaluate_division/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path | 183 | 91.49
1623+
| 0398 |[Random Pick Index](src/main/kotlin/g0301_0400/s0398_random_pick_index/Solution.kt)| Medium | Hash_Table, Math, Randomized, Reservoir_Sampling | 1091 | 75.00
16211624
| 0397 |[Integer Replacement](src/main/kotlin/g0301_0400/s0397_integer_replacement/Solution.kt)| Medium | Dynamic_Programming, Greedy, Bit_Manipulation, Memoization | 145 | 87.50
1625+
| 0396 |[Rotate Function](src/main/kotlin/g0301_0400/s0396_rotate_function/Solution.kt)| Medium | Array, Dynamic_Programming, Math | 562 | 87.50
16221626
| 0395 |[Longest Substring with At Least K Repeating Characters](src/main/kotlin/g0301_0400/s0395_longest_substring_with_at_least_k_repeating_characters/Solution.kt)| Medium | Top_Interview_Questions, String, Hash_Table, Sliding_Window, Divide_and_Conquer | 274 | 66.67
16231627
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 224 | 64.86
16241628
| 0393 |[UTF-8 Validation](src/main/kotlin/g0301_0400/s0393_utf_8_validation/Solution.kt)| Medium | Array, Bit_Manipulation | 219 | 100.00
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0301_0400.s0396_rotate_function
2+
3+
// #Medium #Array #Dynamic_Programming #Math #2022_11_30_Time_571_ms_(87.50%)_Space_54.1_MB_(62.50%)
4+
5+
class Solution {
6+
fun maxRotateFunction(nums: IntArray): Int {
7+
var allSum = 0
8+
val len = nums.size
9+
var f = 0
10+
for (i in 0 until len) {
11+
f += i * nums[i]
12+
allSum += nums[i]
13+
}
14+
var max = f
15+
for (i in len - 1 downTo 1) {
16+
f += allSum - len * nums[i]
17+
max = Math.max(f, max)
18+
}
19+
return max
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
396\. Rotate Function
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n`.
6+
7+
Assume <code>arr<sub>k</sub></code> to be an array obtained by rotating `nums` by `k` positions clock-wise. We define the **rotation function** `F` on `nums` as follow:
8+
9+
* <code>F(k) = 0 * arr<sub>k</sub>[0] + 1 * arr<sub>k</sub>[1] + ... + (n - 1) * arr<sub>k</sub>[n - 1].</code>
10+
11+
Return _the maximum value of_ `F(0), F(1), ..., F(n-1)`.
12+
13+
The test cases are generated so that the answer fits in a **32-bit** integer.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [4,3,2,6]
18+
19+
**Output:** 26
20+
21+
**Explanation:**
22+
23+
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
24+
25+
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
26+
27+
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
28+
29+
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
30+
31+
So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [100]
36+
37+
**Output:** 0
38+
39+
**Constraints:**
40+
41+
* `n == nums.length`
42+
* <code>1 <= n <= 10<sup>5</sup></code>
43+
* `-100 <= nums[i] <= 100`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0301_0400.s0398_random_pick_index
2+
3+
// #Medium #Hash_Table #Math #Randomized #Reservoir_Sampling
4+
// #2022_11_29_Time_1091_ms_(75.00%)_Space_84.3_MB_(25.00%)
5+
6+
import java.util.Random
7+
8+
@Suppress("kotlin:S2245")
9+
class Solution(nums: IntArray) {
10+
// O(n) time | O(n) space
11+
private val map: MutableMap<Int, MutableList<Int>>
12+
private val rand: Random
13+
14+
init {
15+
map = HashMap()
16+
rand = Random()
17+
for (i in nums.indices) {
18+
map.computeIfAbsent(
19+
nums[i]
20+
) { ArrayList() }.add(i)
21+
}
22+
}
23+
24+
fun pick(target: Int): Int {
25+
val list: List<Int> = map[target]!!
26+
return list[rand.nextInt(list.size)]
27+
}
28+
}
29+
30+
/*
31+
* Your Solution object will be instantiated and called as such:
32+
* var obj = Solution(nums)
33+
* var param_1 = obj.pick(target)
34+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
398\. Random Pick Index
2+
3+
Medium
4+
5+
Given an integer array `nums` with possible **duplicates**, randomly output the index of a given `target` number. You can assume that the given target number must exist in the array.
6+
7+
Implement the `Solution` class:
8+
9+
* `Solution(int[] nums)` Initializes the object with the array `nums`.
10+
* `int pick(int target)` Picks a random index `i` from `nums` where `nums[i] == target`. If there are multiple valid i's, then each index should have an equal probability of returning.
11+
12+
**Example 1:**
13+
14+
**Input**
15+
16+
["Solution", "pick", "pick", "pick"]
17+
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
18+
19+
**Output:** [null, 4, 0, 2]
20+
21+
**Explanation:**
22+
23+
Solution solution = new Solution([1, 2, 3, 3, 3]);
24+
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
25+
solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1.
26+
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
27+
28+
**Constraints:**
29+
30+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
31+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
32+
* `target` is an integer from `nums`.
33+
* At most <code>10<sup>4</sup></code> calls will be made to `pick`.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g0301_0400.s0399_evaluate_division
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Graph #Union_Find #Shortest_Path
4+
// #2022_11_29_Time_183_ms_(91.49%)_Space_34.6_MB_(95.74%)
5+
6+
class Solution {
7+
private var root: MutableMap<String?, String?>? = null
8+
private var rate: MutableMap<String?, Double>? = null
9+
fun calcEquation(
10+
equations: List<List<String?>>,
11+
values: DoubleArray,
12+
queries: List<List<String?>>
13+
): DoubleArray {
14+
root = HashMap()
15+
rate = HashMap()
16+
val n = equations.size
17+
for (equation in equations) {
18+
val x = equation[0]
19+
val y = equation[1]
20+
(root as HashMap<String?, String?>)[x] = x
21+
(root as HashMap<String?, String?>)[y] = y
22+
(rate as HashMap<String?, Double>)[x] = 1.0
23+
(rate as HashMap<String?, Double>)[y] = 1.0
24+
}
25+
for (i in 0 until n) {
26+
val x = equations[i][0]
27+
val y = equations[i][1]
28+
union(x, y, values[i])
29+
}
30+
val result = DoubleArray(queries.size)
31+
for (i in queries.indices) {
32+
val x = queries[i][0]
33+
val y = queries[i][1]
34+
if (!(root as HashMap<String?, String?>).containsKey(x) ||
35+
!(root as HashMap<String?, String?>).containsKey(y)
36+
) {
37+
result[i] = -1.0
38+
continue
39+
}
40+
val rootX = findRoot(x, x, 1.0)
41+
val rootY = findRoot(y, y, 1.0)
42+
result[i] = if (rootX == rootY) (rate as HashMap<String?, Double>).get(x)!! /
43+
(rate as HashMap<String?, Double>).get(y)!! else -1.0
44+
}
45+
return result
46+
}
47+
48+
private fun union(x: String?, y: String?, v: Double) {
49+
val rootX = findRoot(x, x, 1.0)
50+
val rootY = findRoot(y, y, 1.0)
51+
root!![rootX] = rootY
52+
val r1 = rate!![x]!!
53+
val r2 = rate!![y]!!
54+
rate!![rootX] = v * r2 / r1
55+
}
56+
57+
private fun findRoot(originalX: String?, x: String?, r: Double): String? {
58+
if (root!![x] == x) {
59+
root!![originalX] = x
60+
rate!![originalX] = r * rate!![x]!!
61+
return x
62+
}
63+
return findRoot(originalX, root!![x], r * rate!![x]!!)
64+
}
65+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
399\. Evaluate Division
2+
3+
Medium
4+
5+
You are given an array of variable pairs `equations` and an array of real numbers `values`, where <code>equations[i] = [A<sub>i</sub>, B<sub>i</sub>]</code> and `values[i]` represent the equation <code>A<sub>i</sub> / B<sub>i</sub> = values[i]</code>. Each <code>A<sub>i</sub></code> or <code>B<sub>i</sub></code> is a string that represents a single variable.
6+
7+
You are also given some `queries`, where <code>queries[j] = [C<sub>j</sub>, D<sub>j</sub>]</code> represents the <code>j<sup>th</sup></code> query where you must find the answer for <code>C<sub>j</sub> / D<sub>j</sub> = ?</code>.
8+
9+
Return _the answers to all queries_. If a single answer cannot be determined, return `-1.0`.
10+
11+
**Note:** The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
12+
13+
**Example 1:**
14+
15+
**Input:** equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
16+
17+
**Output:** [6.00000,0.50000,-1.00000,1.00000,-1.00000]
18+
19+
**Explanation:** Given: _a / b = 2.0_, _b / c = 3.0_ queries are: _a / c = ?_, _b / a = ?_, _a / e = ?_, _a / a = ?_, _x / x = ?_ return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
20+
21+
**Example 2:**
22+
23+
**Input:** equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
24+
25+
**Output:** [3.75000,0.40000,5.00000,0.20000]
26+
27+
**Example 3:**
28+
29+
**Input:** equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
30+
31+
**Output:** [0.50000,2.00000,-1.00000,-1.00000]
32+
33+
**Constraints:**
34+
35+
* `1 <= equations.length <= 20`
36+
* `equations[i].length == 2`
37+
* <code>1 <= A<sub>i</sub>.length, B<sub>i</sub>.length <= 5</code>
38+
* `values.length == equations.length`
39+
* `0.0 < values[i] <= 20.0`
40+
* `1 <= queries.length <= 20`
41+
* `queries[i].length == 2`
42+
* <code>1 <= C<sub>j</sub>.length, D<sub>j</sub>.length <= 5</code>
43+
* <code>A<sub>i</sub>, B<sub>i</sub>, C<sub>j</sub>, D<sub>j</sub></code> consist of lower case English letters and digits.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0301_0400.s0400_nth_digit
2+
3+
// #Medium #Math #Binary_Search #2022_11_29_Time_271_ms_(50.00%)_Space_33.7_MB_(50.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
/*
8+
* 1. find the length of the number where the nth digit is from
9+
* 2. find the actual number where the nth digit is from
10+
* 3. find the nth digit and return
11+
*/
12+
fun findNthDigit(n: Int): Int {
13+
var n = n
14+
var len = 1
15+
var count: Long = 9
16+
var start = 1
17+
while (n > len * count) {
18+
n -= (len * count).toInt()
19+
len += 1
20+
count *= 10
21+
start *= 10
22+
}
23+
start += (n - 1) / len
24+
val s = Integer.toString(start)
25+
return Character.getNumericValue(s[(n - 1) % len])
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
400\. Nth Digit
2+
3+
Medium
4+
5+
Given an integer `n`, return the <code>n<sup>th</sup></code> digit of the infinite integer sequence `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 3
10+
11+
**Output:** 3
12+
13+
**Example 2:**
14+
15+
**Input:** n = 11
16+
17+
**Output:** 0
18+
19+
**Explanation:** The 11<sup>th</sup> digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
20+
21+
**Constraints:**
22+
23+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0301_0400.s0396_rotate_function
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 maxRotateFunction() {
10+
assertThat(Solution().maxRotateFunction(intArrayOf(4, 3, 2, 6)), equalTo(26))
11+
}
12+
13+
@Test
14+
fun maxRotateFunction2() {
15+
assertThat(Solution().maxRotateFunction(intArrayOf(100)), equalTo(0))
16+
}
17+
}

0 commit comments

Comments
 (0)