Skip to content

Commit a860c08

Browse files
authored
Added tasks 632, 633, 636, 637
1 parent 3105409 commit a860c08

File tree

13 files changed

+458
-0
lines changed

13 files changed

+458
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
13161316
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13171317
|-|-|-|-|-|-
13181318
| 0350 |[Intersection of Two Arrays II](src.save/main/kotlin/g0301_0400/s0350_intersection_of_two_arrays_ii/Solution.kt)| Easy | Top_Interview_Questions, Array, Hash_Table, Sorting, Binary_Search, Two_Pointers | 321 | 73.37
1319+
| 0633 |[Sum of Square Numbers](src/main/kotlin/g0601_0700/s0633_sum_of_square_numbers/Solution.kt)| Medium | Math, Binary_Search, Two_Pointers | 126 | 100.00
13191320

13201321
#### Day 11
13211322

@@ -1673,6 +1674,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16731674
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16741675
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
16751676
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1677+
| 0637 |[Average of Levels in Binary Tree](src/main/kotlin/g0601_0700/s0637_average_of_levels_in_binary_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 249 | 100.00
1678+
| 0636 |[Exclusive Time of Functions](src/main/kotlin/g0601_0700/s0636_exclusive_time_of_functions/Solution.kt)| Medium | Array, Stack | 270 | 80.00
1679+
| 0633 |[Sum of Square Numbers](src/main/kotlin/g0601_0700/s0633_sum_of_square_numbers/Solution.kt)| Medium | Math, Binary_Search, Two_Pointers, Binary_Search_I_Day_10 | 126 | 100.00
1680+
| 0632 |[Smallest Range Covering Elements from K Lists](src/main/kotlin/g0601_0700/s0632_smallest_range_covering_elements_from_k_lists/Solution.kt)| Hard | Array, Hash_Table, Sorting, Greedy, Heap_Priority_Queue, Sliding_Window | 399 | 83.33
16761681
| 0630 |[Course Schedule III](src/main/kotlin/g0601_0700/s0630_course_schedule_iii/Solution.kt)| Hard | Array, Greedy, Heap_Priority_Queue | 536 | 100.00
16771682
| 0629 |[K Inverse Pairs Array](src/main/kotlin/g0601_0700/s0629_k_inverse_pairs_array/Solution.kt)| Hard | Dynamic_Programming | 155 | 100.00
16781683
| 0628 |[Maximum Product of Three Numbers](src/main/kotlin/g0601_0700/s0628_maximum_product_of_three_numbers/Solution.kt)| Easy | Array, Math, Sorting | 276 | 97.30
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0601_0700.s0632_smallest_range_covering_elements_from_k_lists
2+
3+
// #Hard #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Sliding_Window
4+
// #2023_02_09_Time_399_ms_(83.33%)_Space_59.2_MB_(66.67%)
5+
6+
import java.util.Objects
7+
import java.util.PriorityQueue
8+
9+
class Solution {
10+
internal class Triplet(var value: Int, var row: Int, var idx: Int) : Comparable<Triplet?> {
11+
override operator fun compareTo(other: Triplet?): Int {
12+
return value - other!!.value
13+
}
14+
}
15+
16+
fun smallestRange(nums: List<List<Int>>): IntArray {
17+
val pq = PriorityQueue<Triplet>()
18+
var maxInPq = Int.MIN_VALUE
19+
for (i in nums.indices) {
20+
pq.add(Triplet(nums[i][0], i, 0))
21+
if (maxInPq < nums[i][0]) {
22+
maxInPq = nums[i][0]
23+
}
24+
}
25+
var rangeSize = maxInPq - Objects.requireNonNull(pq.peek()).value + 1
26+
var rangeLeft = Objects.requireNonNull(pq.peek()).value
27+
var rangeRight = maxInPq
28+
while (true) {
29+
val nextNumber = pq.remove()
30+
if (nextNumber.idx + 1 < nums[nextNumber.row].size) {
31+
val `val` = nums[nextNumber.row][nextNumber.idx + 1]
32+
if (`val` > maxInPq) {
33+
maxInPq = `val`
34+
}
35+
pq.add(Triplet(`val`, nextNumber.row, nextNumber.idx + 1))
36+
if (maxInPq - Objects.requireNonNull(pq.peek()).value + 1 < rangeSize) {
37+
rangeSize = maxInPq - pq.peek().value + 1
38+
rangeLeft = maxInPq
39+
rangeRight = pq.peek().value
40+
}
41+
} else {
42+
break
43+
}
44+
}
45+
val answer = IntArray(2)
46+
answer[0] = rangeLeft
47+
answer[1] = rangeRight
48+
return answer
49+
}
50+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
632\. Smallest Range Covering Elements from K Lists
2+
3+
Hard
4+
5+
You have `k` lists of sorted integers in **non-decreasing order**. Find the **smallest** range that includes at least one number from each of the `k` lists.
6+
7+
We define the range `[a, b]` is smaller than range `[c, d]` if `b - a < d - c` **or** `a < c` if `b - a == d - c`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
12+
13+
**Output:** [20,24]
14+
15+
**Explanation:**
16+
17+
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
18+
19+
List 2: [0, 9, 12, 20], 20 is in range [20,24].
20+
21+
List 3: [5, 18, 22, 30], 22 is in range [20,24].
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [[1,2,3],[1,2,3],[1,2,3]]
26+
27+
**Output:** [1,1]
28+
29+
**Constraints:**
30+
31+
* `nums.length == k`
32+
* `1 <= k <= 3500`
33+
* `1 <= nums[i].length <= 50`
34+
* <code>-10<sup>5</sup> <= nums[i][j] <= 10<sup>5</sup></code>
35+
* `nums[i]` is sorted in **non-decreasing** order.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0601_0700.s0633_sum_of_square_numbers
2+
3+
// #Medium #Math #Binary_Search #Two_Pointers #Binary_Search_I_Day_10
4+
// #2023_02_09_Time_126_ms_(100.00%)_Space_33_MB_(90.00%)
5+
6+
import kotlin.math.sqrt
7+
8+
class Solution {
9+
fun judgeSquareSum(c: Int): Boolean {
10+
val right = sqrt(c.toDouble()).toInt()
11+
val left = sqrt(c.toDouble() / 2).toInt()
12+
for (i in left..right) {
13+
val j = sqrt(c - (i * i).toDouble()).toInt()
14+
if (i * i + j * j == c) {
15+
return true
16+
}
17+
}
18+
return false
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
633\. Sum of Square Numbers
2+
3+
Medium
4+
5+
Given a non-negative integer `c`, decide whether there're two integers `a` and `b` such that <code>a<sup>2</sup> + b<sup>2</sup> = c</code>.
6+
7+
**Example 1:**
8+
9+
**Input:** c = 5
10+
11+
**Output:** true
12+
13+
**Explanation:** 1 \* 1 + 2 \* 2 = 5
14+
15+
**Example 2:**
16+
17+
**Input:** c = 3
18+
19+
**Output:** false
20+
21+
**Constraints:**
22+
23+
* <code>0 <= c <= 2<sup>31</sup> - 1</code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0601_0700.s0636_exclusive_time_of_functions
2+
3+
// #Medium #Array #Stack #2023_02_10_Time_270_ms_(80.00%)_Space_37.3_MB_(20.00%)
4+
5+
import java.util.ArrayDeque
6+
import java.util.Deque
7+
8+
class Solution {
9+
fun exclusiveTime(n: Int, logs: List<String>): IntArray {
10+
val stack: Deque<Log> = ArrayDeque()
11+
val result = IntArray(n)
12+
for (content in logs) {
13+
val log = Log(content)
14+
if (log.isStart) {
15+
stack.push(log)
16+
} else {
17+
val top = stack.pop()
18+
val executionTime = log.time - top.time + 1
19+
result[top.id] += executionTime - top.waitingTime
20+
if (!stack.isEmpty()) {
21+
stack.peek().waitingTime += executionTime
22+
}
23+
}
24+
}
25+
return result
26+
}
27+
28+
private class Log internal constructor(content: String) {
29+
var id: Int
30+
var isStart: Boolean
31+
var time: Int
32+
var waitingTime: Int
33+
init {
34+
val tokens = content.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
35+
id = tokens[0].toInt()
36+
isStart = tokens[1] == "start"
37+
time = tokens[2].toInt()
38+
waitingTime = 0
39+
}
40+
}
41+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
636\. Exclusive Time of Functions
2+
3+
Medium
4+
5+
On a **single-threaded** CPU, we execute a program containing `n` functions. Each function has a unique ID between `0` and `n-1`.
6+
7+
Function calls are **stored in a [call stack](https://en.wikipedia.org/wiki/Call_stack)**: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is **the current function being executed**. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp.
8+
9+
You are given a list `logs`, where `logs[i]` represents the <code>i<sup>th</sup></code> log message formatted as a string `"{function_id}:{"start" | "end"}:{timestamp}"`. For example, `"0:start:3"` means a function call with function ID `0` **started at the beginning** of timestamp `3`, and `"1:end:2"` means a function call with function ID `1` **ended at the end** of timestamp `2`. Note that a function can be called **multiple times, possibly recursively**.
10+
11+
A function's **exclusive time** is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for `2` time units and another call executing for `1` time unit, the **exclusive time** is `2 + 1 = 3`.
12+
13+
Return _the **exclusive time** of each function in an array, where the value at the_ <code>i<sup>th</sup></code> _index represents the exclusive time for the function with ID_ `i`.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2019/04/05/diag1b.png)
18+
19+
**Input:** n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
20+
21+
**Output:** [3,4]
22+
23+
**Explanation:**
24+
25+
Function 0 starts at the beginning of time 0, then it executes 2 for units of time and reaches the end of time 1.
26+
27+
Function 1 starts at the beginning of time 2, executes for 4 units of time, and ends at the end of time 5.
28+
29+
Function 0 resumes execution at the beginning of time 6 and executes for 1 unit of time.
30+
31+
So function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.
32+
33+
**Example 2:**
34+
35+
**Input:** n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]
36+
37+
**Output:** [8]
38+
39+
**Explanation:**
40+
41+
Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
42+
43+
Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
44+
45+
Function 0 (initial call) resumes execution then immediately calls itself again.
46+
47+
Function 0 (2nd recursive call) starts at the beginning of time 6 and executes for 1 unit of time.
48+
49+
Function 0 (initial call) resumes execution at the beginning of time 7 and executes for 1 unit of time.
50+
51+
So function 0 spends 2 + 4 + 1 + 1 = 8 units of total time executing.
52+
53+
**Example 3:**
54+
55+
**Input:** n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]
56+
57+
**Output:** [7,1]
58+
59+
**Explanation:**
60+
61+
Function 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.
62+
63+
Function 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.
64+
65+
Function 0 (initial call) resumes execution then immediately calls function 1.
66+
67+
Function 1 starts at the beginning of time 6, executes 1 unit of time, and ends at the end of time 6.
68+
69+
Function 0 resumes execution at the beginning of time 6 and executes for 2 units of time.
70+
71+
So function 0 spends 2 + 4 + 1 = 7 units of total time executing, and function 1 spends 1 unit of total time executing.
72+
73+
**Constraints:**
74+
75+
* `1 <= n <= 100`
76+
* `1 <= logs.length <= 500`
77+
* `0 <= function_id < n`
78+
* <code>0 <= timestamp <= 10<sup>9</sup></code>
79+
* No two start events will happen at the same timestamp.
80+
* No two end events will happen at the same timestamp.
81+
* Each function has an `"end"` log for each `"start"` log.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0601_0700.s0637_average_of_levels_in_binary_tree
2+
3+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #2023_02_10_Time_249_ms_(100.00%)_Space_39.5_MB_(72.73%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
fun averageOfLevels(root: TreeNode?): List<Double> {
20+
val map: MutableMap<Int, Array<Double>> = HashMap()
21+
helper(root, map, 0)
22+
val result: MutableList<Double> = ArrayList()
23+
for (pair in map.values) {
24+
val avg = pair[1] / pair[0]
25+
result.add(avg)
26+
}
27+
return result
28+
}
29+
30+
private fun helper(root: TreeNode?, map: MutableMap<Int, Array<Double>>, level: Int) {
31+
if (root == null) {
32+
return
33+
}
34+
val pair = if (map.containsKey(level)) map[level]!! else arrayOf(0.0, 0.0)
35+
pair[0] += 1.0
36+
pair[1] = pair[1] + root.`val`
37+
map[level] = pair
38+
helper(root.left, map, level + 1)
39+
helper(root.right, map, level + 1)
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
637\. Average of Levels in Binary Tree
2+
3+
Easy
4+
5+
Given the `root` of a binary tree, return _the average value of the nodes on each level in the form of an array_. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg)
10+
11+
**Input:** root = [3,9,20,null,null,15,7]
12+
13+
**Output:** [3.00000,14.50000,11.00000] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/03/09/avg2-tree.jpg)
18+
19+
**Input:** root = [3,9,20,15,7]
20+
21+
**Output:** [3.00000,14.50000,11.00000]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
26+
* <code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0601_0700.s0632_smallest_range_covering_elements_from_k_lists
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 smallestRange() {
10+
assertThat(
11+
Solution()
12+
.smallestRange(
13+
listOf(
14+
listOf(4, 10, 15, 24, 26),
15+
listOf(0, 9, 12, 20),
16+
listOf(5, 18, 22, 30)
17+
)
18+
),
19+
equalTo(intArrayOf(24, 20))
20+
)
21+
}
22+
23+
@Test
24+
fun smallestRange2() {
25+
assertThat(
26+
Solution()
27+
.smallestRange(
28+
listOf(
29+
listOf(1, 2, 3),
30+
listOf(1, 2, 3),
31+
listOf(1, 2, 3)
32+
)
33+
),
34+
equalTo(intArrayOf(1, 1))
35+
)
36+
}
37+
}

0 commit comments

Comments
 (0)