Skip to content

Commit 3f6e4fc

Browse files
authored
Added tasks 1029, 1030, 1031, 1032
1 parent de06da4 commit 3f6e4fc

File tree

13 files changed

+489
-0
lines changed

13 files changed

+489
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17501750
| # | Title | Difficulty | Tag | Time, ms | Time, %
17511751
|------|----------------|-------------|-------------|----------|---------
17521752
| 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
1753+
| 1032 |[Maximum Sum of Two Non-Overlapping Subarrays](src/main/kotlin/g1001_1100/s1032_stream_of_characters/StreamChecker.kt)| Hard | Array, String, Design, Trie, Data_Stream | 733 | 100.00
1754+
| 1031 |[Maximum Sum of Two Non-Overlapping Subarrays](src/main/kotlin/g1001_1100/s1031_maximum_sum_of_two_non_overlapping_subarrays/Solution.kt)| Medium | Array, Dynamic_Programming, Sliding_Window | 172 | 100.00
1755+
| 1030 |[Matrix Cells in Distance Order](src/main/kotlin/g1001_1100/s1030_matrix_cells_in_distance_order/Solution.kt)| Easy | Array, Math, Sorting, Matrix, Geometry | 426 | 100.00
1756+
| 1029 |[Two City Scheduling](src/main/kotlin/g1001_1100/s1029_two_city_scheduling/Solution.kt)| Medium | Array, Sorting, Greedy | 148 | 100.00
17531757
| 1028 |[Recover a Tree From Preorder Traversal](src/main/kotlin/g1001_1100/s1028_recover_a_tree_from_preorder_traversal/Solution.kt)| Hard | String, Depth_First_Search, Tree, Binary_Tree | 246 | 100.00
17541758
| 1027 |[Longest Arithmetic Subsequence](src/main/kotlin/g1001_1100/s1027_longest_arithmetic_subsequence/Solution.kt)| Medium | Array, Hash_Table, Dynamic_Programming, Binary_Search | 330 | 100.00
17551759
| 1026 |[Maximum Difference Between Node and Ancestor](src/main/kotlin/g1001_1100/s1026_maximum_difference_between_node_and_ancestor/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree | 155 | 77.78
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1001_1100.s1029_two_city_scheduling
2+
3+
// #Medium #Array #Sorting #Greedy #2023_05_24_Time_148_ms_(100.00%)_Space_35.4_MB_(92.31%)
4+
5+
import java.util.Arrays
6+
7+
class Solution {
8+
fun twoCitySchedCost(costs: Array<IntArray>): Int {
9+
Arrays.sort(costs) { a: IntArray, b: IntArray ->
10+
a[0] - a[1] - (b[0] - b[1])
11+
}
12+
var cost = 0
13+
for (i in costs.indices) {
14+
cost += if (i < costs.size / 2) {
15+
costs[i][0]
16+
} else {
17+
costs[i][1]
18+
}
19+
}
20+
return cost
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
1029\. Two City Scheduling
2+
3+
Medium
4+
5+
A company is planning to interview `2n` people. Given the array `costs` where <code>costs[i] = [aCost<sub>i</sub>, bCost<sub>i</sub>]</code>, the cost of flying the <code>i<sup>th</sup></code> person to city `a` is <code>aCost<sub>i</sub></code>, and the cost of flying the <code>i<sup>th</sup></code> person to city `b` is <code>bCost<sub>i</sub></code>.
6+
7+
Return _the minimum cost to fly every person to a city_ such that exactly `n` people arrive in each city.
8+
9+
**Example 1:**
10+
11+
**Input:** costs = [[10,20],[30,200],[400,50],[30,20]]
12+
13+
**Output:** 110
14+
15+
**Explanation:**
16+
17+
The first person goes to city A for a cost of 10.
18+
19+
The second person goes to city A for a cost of 30.
20+
21+
The third person goes to city B for a cost of 50.
22+
23+
The fourth person goes to city B for a cost of 20.
24+
25+
The total minimum cost is 10 + 30 + 50 + 20 = 110 to have half the people interviewing in each city.
26+
27+
**Example 2:**
28+
29+
**Input:** costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
30+
31+
**Output:** 1859
32+
33+
**Example 3:**
34+
35+
**Input:** costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
36+
37+
**Output:** 3086
38+
39+
**Constraints:**
40+
41+
* `2 * n == costs.length`
42+
* `2 <= costs.length <= 100`
43+
* `costs.length` is even.
44+
* <code>1 <= aCost<sub>i</sub>, bCost<sub>i</sub> <= 1000</code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1001_1100.s1030_matrix_cells_in_distance_order
2+
3+
// #Easy #Array #Math #Sorting #Matrix #Geometry
4+
// #2023_05_24_Time_426_ms_(100.00%)_Space_99.9_MB_(100.00%)
5+
6+
import java.util.TreeMap
7+
import kotlin.math.abs
8+
9+
class Solution {
10+
fun allCellsDistOrder(rows: Int, cols: Int, rCenter: Int, cCenter: Int): Array<IntArray?> {
11+
val map: MutableMap<Int, MutableList<IntArray>> = TreeMap()
12+
for (i in 0 until rows) {
13+
for (j in 0 until cols) {
14+
map.computeIfAbsent(
15+
abs(i - rCenter) + abs(j - cCenter)
16+
) { ArrayList() }
17+
.add(intArrayOf(i, j))
18+
}
19+
}
20+
val res = arrayOfNulls<IntArray>(rows * cols)
21+
var i = 0
22+
for (list in map.values) {
23+
for (each in list) {
24+
res[i++] = each
25+
}
26+
}
27+
return res
28+
}
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1030\. Matrix Cells in Distance Order
2+
3+
Easy
4+
5+
You are given four integers `row`, `cols`, `rCenter`, and `cCenter`. There is a `rows x cols` matrix and you are on the cell with the coordinates `(rCenter, cCenter)`.
6+
7+
Return _the coordinates of all cells in the matrix, sorted by their **distance** from_ `(rCenter, cCenter)` _from the smallest distance to the largest distance_. You may return the answer in **any order** that satisfies this condition.
8+
9+
The **distance** between two cells <code>(r<sub>1</sub>, c<sub>1</sub>)</code> and <code>(r<sub>2</sub>, c<sub>2</sub>)</code> is <code>|r<sub>1</sub> - r<sub>2</sub>| + |c<sub>1</sub> - c<sub>2</sub>|</code>.
10+
11+
**Example 1:**
12+
13+
**Input:** rows = 1, cols = 2, rCenter = 0, cCenter = 0
14+
15+
**Output:** [[0,0],[0,1]]
16+
17+
**Explanation:** The distances from (0, 0) to other cells are: [0,1]
18+
19+
**Example 2:**
20+
21+
**Input:** rows = 2, cols = 2, rCenter = 0, cCenter = 1
22+
23+
**Output:** [[0,1],[0,0],[1,1],[1,0]]
24+
25+
**Explanation:** The distances from (0, 1) to other cells are: [0,1,1,2] The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.
26+
27+
**Example 3:**
28+
29+
**Input:** rows = 2, cols = 3, rCenter = 1, cCenter = 2
30+
31+
**Output:** [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
32+
33+
**Explanation:** The distances from (1, 2) to other cells are: [0,1,1,2,2,3] There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
34+
35+
**Constraints:**
36+
37+
* `1 <= rows, cols <= 100`
38+
* `0 <= rCenter < rows`
39+
* `0 <= cCenter < cols`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g1001_1100.s1031_maximum_sum_of_two_non_overlapping_subarrays
2+
3+
// #Medium #Array #Dynamic_Programming #Sliding_Window
4+
// #2023_05_24_Time_172_ms_(100.00%)_Space_36.7_MB_(100.00%)
5+
6+
class Solution {
7+
fun maxSumTwoNoOverlap(nums: IntArray, firstLen: Int, secondLen: Int): Int {
8+
val firstLenSum = getFirstLenSums(nums, firstLen)
9+
return getMaxLenSum(nums, secondLen, firstLenSum)
10+
}
11+
12+
private fun getMaxLenSum(nums: IntArray, secondLen: Int, firstLenSum: Array<IntArray>): Int {
13+
var maxSum = 0
14+
var currentSum = 0
15+
var onRight: Int
16+
for (i in 0 until secondLen) {
17+
currentSum += nums[i]
18+
}
19+
onRight = firstLenSum[1][secondLen]
20+
maxSum = maxSum.coerceAtLeast(currentSum + onRight)
21+
var i = 1
22+
var j = secondLen
23+
while (j < nums.size) {
24+
currentSum = currentSum - nums[i - 1] + nums[j]
25+
onRight = if (j < nums.size - 1) firstLenSum[1][j + 1] else 0
26+
maxSum = maxSum.coerceAtLeast(currentSum + firstLenSum[0][i - 1].coerceAtLeast(onRight))
27+
i++
28+
j++
29+
}
30+
return maxSum
31+
}
32+
33+
private fun getFirstLenSums(nums: IntArray, windowSize: Int): Array<IntArray> {
34+
// sum[0] - maximum from left to right, sum[1] - max from right to left.
35+
val sum = Array(2) { IntArray(nums.size) }
36+
var currentLeftSum = 0
37+
var currentRightSum = 0
38+
run {
39+
var i = 0
40+
var j = nums.size - 1
41+
while (i < windowSize) {
42+
currentLeftSum += nums[i]
43+
currentRightSum += nums[j]
44+
i++
45+
j--
46+
}
47+
}
48+
sum[0][windowSize - 1] = currentLeftSum
49+
sum[1][nums.size - windowSize] = currentRightSum
50+
var i = windowSize
51+
var j = nums.size - windowSize - 1
52+
while (i < nums.size) {
53+
currentLeftSum = currentLeftSum - nums[i - windowSize] + nums[i]
54+
currentRightSum = currentRightSum - nums[j + windowSize] + nums[j]
55+
sum[0][i] = sum[0][i - 1].coerceAtLeast(currentLeftSum)
56+
sum[1][j] = sum[1][j + 1].coerceAtLeast(currentRightSum)
57+
i++
58+
j--
59+
}
60+
return sum
61+
}
62+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1031\. Maximum Sum of Two Non-Overlapping Subarrays
2+
3+
Medium
4+
5+
Given an integer array `nums` and two integers `firstLen` and `secondLen`, return _the maximum sum of elements in two non-overlapping **subarrays** with lengths_ `firstLen` _and_ `secondLen`.
6+
7+
The array with length `firstLen` could occur before or after the array with length `secondLen`, but they have to be non-overlapping.
8+
9+
A **subarray** is a **contiguous** part of an array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
14+
15+
**Output:** 20
16+
17+
**Explanation:** One choice of subarrays is [9] with length 1, and [6,5] with length 2.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
22+
23+
**Output:** 29
24+
25+
**Explanation:** One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
30+
31+
**Output:** 31
32+
33+
**Explanation:** One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.
34+
35+
**Constraints:**
36+
37+
* `1 <= firstLen, secondLen <= 1000`
38+
* `2 <= firstLen + secondLen <= 1000`
39+
* `firstLen + secondLen <= nums.length <= 1000`
40+
* `0 <= nums[i] <= 1000`
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g1001_1100.s1032_stream_of_characters
2+
3+
// #Hard #Array #String #Design #Trie #Data_Stream
4+
// #2023_05_24_Time_733_ms_(100.00%)_Space_158.5_MB_(50.00%)
5+
6+
class StreamChecker(words: Array<String>) {
7+
internal class Node {
8+
var child: Array<Node?> = arrayOfNulls(26)
9+
var isEnd = false
10+
}
11+
12+
private val sb: StringBuilder = StringBuilder()
13+
private val root: Node = Node()
14+
fun insert(s: String) {
15+
var curr: Node? = root
16+
for (i in s.length - 1 downTo 0) {
17+
val c = s[i]
18+
if (curr!!.child[c.code - 'a'.code] == null) {
19+
curr.child[c.code - 'a'.code] = Node()
20+
}
21+
curr = curr.child[c.code - 'a'.code]
22+
}
23+
curr!!.isEnd = true
24+
}
25+
26+
init {
27+
for (s in words) {
28+
insert(s)
29+
}
30+
}
31+
32+
fun query(letter: Char): Boolean {
33+
sb.append(letter)
34+
var curr: Node? = root
35+
for (i in sb.length - 1 downTo 0) {
36+
val c = sb[i]
37+
if (curr!!.child[c.code - 'a'.code] == null) {
38+
return false
39+
}
40+
if (curr.child[c.code - 'a'.code]!!.isEnd) {
41+
return true
42+
}
43+
curr = curr.child[c.code - 'a'.code]
44+
}
45+
return false
46+
}
47+
}
48+
49+
/*
50+
* Your StreamChecker object will be instantiated and called as such:
51+
* var obj = StreamChecker(words)
52+
* var param_1 = obj.query(letter)
53+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1031\. Maximum Sum of Two Non-Overlapping Subarrays
2+
3+
Medium
4+
5+
Given an integer array `nums` and two integers `firstLen` and `secondLen`, return _the maximum sum of elements in two non-overlapping **subarrays** with lengths_ `firstLen` _and_ `secondLen`.
6+
7+
The array with length `firstLen` could occur before or after the array with length `secondLen`, but they have to be non-overlapping.
8+
9+
A **subarray** is a **contiguous** part of an array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
14+
15+
**Output:** 20
16+
17+
**Explanation:** One choice of subarrays is [9] with length 1, and [6,5] with length 2.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
22+
23+
**Output:** 29
24+
25+
**Explanation:** One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
30+
31+
**Output:** 31
32+
33+
**Explanation:** One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.
34+
35+
**Constraints:**
36+
37+
* `1 <= firstLen, secondLen <= 1000`
38+
* `2 <= firstLen + secondLen <= 1000`
39+
* `firstLen + secondLen <= nums.length <= 1000`
40+
* `0 <= nums[i] <= 1000`

0 commit comments

Comments
 (0)