Skip to content

Commit 4cb15e7

Browse files
authored
Improved task 994
1 parent 039bf6f commit 4cb15e7

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
223223

224224
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
225225
|-|-|-|-|-|-
226-
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 308 | 57.93
226+
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 164 | 82.95
227227
| 0417 |[Pacific Atlantic Water Flow](src.save/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 319 | 100.00
228228

229229
#### Day 11 Graph/BFS/DFS
@@ -833,7 +833,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
833833
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
834834
|-|-|-|-|-|-
835835
| 0542 |[01 Matrix](src.save/main/kotlin/g0501_0600/s0542_01_matrix/Solution.kt)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 441 | 94.06
836-
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 308 | 57.93
836+
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix | 164 | 82.95
837837

838838
#### Day 10 Recursion Backtracking
839839

@@ -1752,7 +1752,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
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
17531753
| 1024 |[Video Stitching](src/main/kotlin/g1001_1100/s1024_video_stitching/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy | 141 | 100.00
17541754
| 1023 |[Camelcase Matching](src/main/kotlin/g1001_1100/s1023_camelcase_matching/Solution.kt)| Medium | String, Two_Pointers, Trie, String_Matching | 149 | 60.00
1755-
| 1022 |[Remove Outermost Parentheses](src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 158 | 88.89
1755+
| 1022 |[Sum of Root To Leaf Binary Numbers](src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 158 | 88.89
17561756
| 1021 |[Remove Outermost Parentheses](src/main/kotlin/g1001_1100/s1021_remove_outermost_parentheses/Solution.kt)| Easy | String, Stack | 156 | 60.00
17571757
| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Graph_Theory_I_Day_3_Matrix_Related_Problems | 369 | 76.26
17581758
| 1019 |[Next Greater Node In Linked List](src/main/kotlin/g1001_1100/s1019_next_greater_node_in_linked_list/Solution.kt)| Medium | Array, Stack, Linked_List, Monotonic_Stack | 472 | 75.00
@@ -1780,7 +1780,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17801780
| 0997 |[Find the Town Judge](src/main/kotlin/g0901_1000/s0997_find_the_town_judge/Solution.kt)| Easy | Array, Hash_Table, Graph, Data_Structure_II_Day_19_Graph, Graph_Theory_I_Day_13_Graph_Theory | 475 | 58.62
17811781
| 0996 |[Number of Squareful Arrays](src/main/kotlin/g0901_1000/s0996_number_of_squareful_arrays/Solution.kt)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Backtracking, Bitmask | 139 | 100.00
17821782
| 0995 |[Minimum Number of K Consecutive Bit Flips](src/main/kotlin/g0901_1000/s0995_minimum_number_of_k_consecutive_bit_flips/Solution.kt)| Hard | Array, Bit_Manipulation, Prefix_Sum, Sliding_Window | 480 | 100.00
1783-
| 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
1783+
| 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 | 164 | 82.95
17841784
| 0993 |[Cousins in Binary Tree](src/main/kotlin/g0901_1000/s0993_cousins_in_binary_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 152 | 71.43
17851785
| 0992 |[Subarrays with K Different Integers](src/main/kotlin/g0901_1000/s0992_subarrays_with_k_different_integers/Solution.kt)| Hard | Array, Hash_Table, Counting, Sliding_Window | 341 | 87.50
17861786
| 0991 |[Broken Calculator](src/main/kotlin/g0901_1000/s0991_broken_calculator/Solution.kt)| Medium | Math, Greedy | 127 | 100.00

src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ package g0901_1000.s0994_rotting_oranges
22

33
// #Medium #Array #Breadth_First_Search #Matrix
44
// #Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search #Level_2_Day_10_Graph/BFS/DFS
5-
// #2022_09_13_Time_308_ms_(57.93%)_Space_41.8_MB_(26.83%)
6-
7-
import java.util.LinkedList
8-
import java.util.Queue
5+
// #2023_05_22_Time_164_ms_(82.95%)_Space_36.2_MB_(49.61%)
96

107
class Solution {
118
fun orangesRotting(grid: Array<IntArray>): Int {
12-
val queue: Queue<IntArray> = LinkedList()
9+
val queue = ArrayDeque<IntArray>()
1310
val row = grid.size
1411
val col: Int = grid[0].size
1512
var countActive = 0
@@ -34,7 +31,7 @@ class Solution {
3431
val size: Int = queue.size
3532
count += size
3633
for (i in 0 until size) {
37-
val arr: IntArray = queue.poll()
34+
val arr: IntArray = queue.removeFirst()
3835
for (j in 0..3) {
3936
val x = arr[0] + dx[j]
4037
val y = arr[1] + dy[j]
Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
1-
1021\. Remove Outermost Parentheses
1+
1022\. Sum of Root To Leaf Binary Numbers
22

33
Easy
44

5-
A valid parentheses string is either empty `""`, `"(" + A + ")"`, or `A + B`, where `A` and `B` are valid parentheses strings, and `+` represents string concatenation.
5+
You are given the `root` of a binary tree where each node has a value `0` or `1`. Each root-to-leaf path represents a binary number starting with the most significant bit.
66

7-
* For example, `""`, `"()"`, `"(())()"`, and `"(()(()))"` are all valid parentheses strings.
7+
* For example, if the path is `0 -> 1 -> 1 -> 0 -> 1`, then this could represent `01101` in binary, which is `13`.
88

9-
A valid parentheses string `s` is primitive if it is nonempty, and there does not exist a way to split it into `s = A + B`, with `A` and `B` nonempty valid parentheses strings.
9+
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return _the sum of these numbers_.
1010

11-
Given a valid parentheses string `s`, consider its primitive decomposition: <code>s = P<sub>1</sub> + P<sub>2</sub> + ... + P<sub>k</sub></code>, where <code>P<sub>i</sub></code> are primitive valid parentheses strings.
12-
13-
Return `s` _after removing the outermost parentheses of every primitive string in the primitive decomposition of_ `s`.
11+
The test cases are generated so that the answer fits in a **32-bits** integer.
1412

1513
**Example 1:**
1614

17-
**Input:** s = "(()())(())"
18-
19-
**Output:** "()()()"
15+
![](https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png)
2016

21-
**Explanation:** The input string is "(()())(())", with primitive decomposition "(()())" + "(())". After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
22-
23-
**Example 2:**
17+
**Input:** root = [1,0,1,0,1,0,1]
2418

25-
**Input:** s = "(()())(())(()(()))"
19+
**Output:** 22
2620

27-
**Output:** "()()()()(())"
21+
**Explanation:** (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
2822

29-
**Explanation:** The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))". After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
30-
31-
**Example 3:**
32-
33-
**Input:** s = "()()"
23+
**Example 2:**
3424

35-
**Output:** ""
25+
**Input:** root = [0]
3626

37-
**Explanation:** The input string is "()()", with primitive decomposition "()" + "()". After removing outer parentheses of each part, this is "" + "" = "".
27+
**Output:** 0
3828

3929
**Constraints:**
4030

41-
* <code>1 <= s.length <= 10<sup>5</sup></code>
42-
* `s[i]` is either `'('` or `')'`.
43-
* `s` is a valid parentheses string.
31+
* The number of nodes in the tree is in the range `[1, 1000]`.
32+
* `Node.val` is `0` or `1`.

0 commit comments

Comments
 (0)