Skip to content

Commit 977ebdd

Browse files
authored
Added tasks 1080-1093
1 parent 96100b5 commit 977ebdd

File tree

9 files changed

+704
-0
lines changed

9 files changed

+704
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@
437437

438438
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
439439
|-|-|-|-|-|-
440+
| 1091 |[Shortest Path in Binary Matrix](src/main/kotlin/g1001_1100/s1091_shortest_path_in_binary_matrix)| Medium | Array, Breadth_First_Search, Matrix | 305 | 98.28
440441
| 0130 |[Surrounded Regions](src/main/kotlin/g0101_0200/s0130_surrounded_regions)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 355 | 84.42
441442
| 0797 |[All Paths From Source to Target](src/main/kotlin/g0701_0800/s0797_all_paths_from_source_to_target)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Backtracking | 232 | 100.00
442443

@@ -1130,6 +1131,7 @@
11301131

11311132
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11321133
|-|-|-|-|-|-
1134+
| 1091 |[Shortest Path in Binary Matrix](src/main/kotlin/g1001_1100/s1091_shortest_path_in_binary_matrix)| Medium | Array, Breadth_First_Search, Matrix | 305 | 98.28
11331135
| 0542 |[01 Matrix](src/main/kotlin/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 441 | 94.06
11341136

11351137
#### Day 6 Matrix Related Problems
@@ -1259,6 +1261,7 @@
12591261
|-|-|-|-|-|-
12601262
| 0182 |[Duplicate Emails](src/main/kotlin/g0101_0200/s0182_duplicate_emails)| Easy | Database | 396 | 68.40
12611263
| 1050 |[Actors and Directors Who Cooperated At Least Three Times](src/main/kotlin/g1001_1100/s1050_actors_and_directors_who_cooperated_at_least_three_times)| Easy | LeetCode_Curated_SQL_70, Database | 629 | 81.02
1264+
| 1084 |[Sales Analysis III](src/main/kotlin/g1001_1100/s1084_sales_analysis_iii)| Easy | LeetCode_Curated_SQL_70, Database | 1881 | 79.36
12621265

12631266
### Level 1
12641267

@@ -1784,6 +1787,14 @@
17841787
| 1105 |[Filling Bookcase Shelves](src/main/kotlin/g1101_1200/s1105_filling_bookcase_shelves)| Medium | Array, Dynamic_Programming | 175 | 33.33
17851788
| 1104 |[Path In Zigzag Labelled Binary Tree](src/main/kotlin/g1101_1200/s1104_path_in_zigzag_labelled_binary_tree)| Medium | Math, Tree, Binary_Tree | 136 | 100.00
17861789
| 1103 |[Distribute Candies to People](src/main/kotlin/g1101_1200/s1103_distribute_candies_to_people)| Easy | Math, Simulation | 129 | 100.00
1790+
| 1093 |[Statistics from a Large Sample](src/main/kotlin/g1001_1100/s1093_statistics_from_a_large_sample)| Medium | Math, Two_Pointers, Probability_and_Statistics | 191 | 100.00
1791+
| 1092 |[Shortest Common Supersequence](src/main/kotlin/g1001_1100/s1092_shortest_common_supersequence)| Hard | String, Dynamic_Programming | 174 | 100.00
1792+
| 1091 |[Shortest Path in Binary Matrix](src/main/kotlin/g1001_1100/s1091_shortest_path_in_binary_matrix)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_5_Matrix_Related_Problems | 305 | 98.28
1793+
| 1090 |[Largest Values From Labels](src/main/kotlin/g1001_1100/s1090_largest_values_from_labels)| Medium | Array, Hash_Table, Sorting, Greedy, Counting | 281 | 100.00
1794+
| 1089 |[Duplicate Zeros](src/main/kotlin/g1001_1100/s1089_duplicate_zeros)| Easy | Array, Two_Pointers | 185 | 93.60
1795+
| 1084 |[Sales Analysis III](src/main/kotlin/g1001_1100/s1084_sales_analysis_iii)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_10_Where | 1881 | 79.36
1796+
| 1081 |[Smallest Subsequence of Distinct Characters](src/main/kotlin/g1001_1100/s1081_smallest_subsequence_of_distinct_characters)| Medium | String, Greedy, Stack, Monotonic_Stack | 146 | 100.00
1797+
| 1080 |[Insufficient Nodes in Root to Leaf Paths](src/main/kotlin/g1001_1100/s1080_insufficient_nodes_in_root_to_leaf_paths)| Medium | Depth_First_Search, Tree, Binary_Tree | 271 | 100.00
17871798
| 1079 |[Letter Tile Possibilities](src/main/kotlin/g1001_1100/s1079_letter_tile_possibilities)| Medium | String, Backtracking | 144 | 100.00
17881799
| 1078 |[Occurrences After Bigram](src/main/kotlin/g1001_1100/s1078_occurrences_after_bigram)| Easy | String | 151 | 88.24
17891800
| 1075 |[Project Employees I](src/main/kotlin/g1001_1100/s1075_project_employees_i)| Easy | Database | 1268 | 94.71
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 1080\. Insufficient Nodes in Root to Leaf Paths
5+
6+
Medium
7+
8+
Given the `root` of a binary tree and an integer `limit`, delete all **insufficient nodes** in the tree simultaneously, and return _the root of the resulting binary tree_.
9+
10+
A node is **insufficient** if every root to **leaf** path intersecting this node has a sum strictly less than `limit`.
11+
12+
A **leaf** is a node with no children.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2019/06/05/insufficient-11.png)
17+
18+
**Input:** root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1
19+
20+
**Output:** [1,2,3,4,null,null,7,8,9,null,14]
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2019/06/05/insufficient-3.png)
25+
26+
**Input:** root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22
27+
28+
**Output:** [5,4,8,11,null,17,4,7,null,null,null,5]
29+
30+
**Example 3:**
31+
32+
![](https://assets.leetcode.com/uploads/2019/06/11/screen-shot-2019-06-11-at-83301-pm.png)
33+
34+
**Input:** root = [1,2,-3,-5,null,4,null], limit = -1
35+
36+
**Output:** [1,null,-3,4]
37+
38+
**Constraints:**
39+
40+
* The number of nodes in the tree is in the range `[1, 5000]`.
41+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
42+
* <code>-10<sup>9</sup> <= limit <= 10<sup>9</sup></code>
43+
44+
## Solution
45+
46+
```kotlin
47+
import com_github_leetcode.TreeNode
48+
49+
/*
50+
* Example:
51+
* var ti = TreeNode(5)
52+
* var v = ti.`val`
53+
* Definition for a binary tree node.
54+
* class TreeNode(var `val`: Int) {
55+
* var left: TreeNode? = null
56+
* var right: TreeNode? = null
57+
* }
58+
*/
59+
class Solution {
60+
fun sufficientSubset(root: TreeNode?, limit: Int): TreeNode? {
61+
return if (sufficientSubset(root, limit, 0, root!!.left == null && root.right == null) < limit) null else root
62+
}
63+
64+
fun sufficientSubset(root: TreeNode?, limit: Int, sum: Int, isLeaf: Boolean): Int {
65+
if (root != null) {
66+
val leftSum = sufficientSubset(
67+
root.left,
68+
limit,
69+
sum + root.`val`,
70+
root.left == null && root.right == null
71+
)
72+
val rightSum = sufficientSubset(
73+
root.right,
74+
limit,
75+
sum + root.`val`,
76+
root.left == null && root.right == null
77+
)
78+
if (leftSum < limit) {
79+
root.left = null
80+
}
81+
if (rightSum < limit) {
82+
root.right = null
83+
}
84+
return leftSum.coerceAtLeast(rightSum)
85+
}
86+
return if (isLeaf) sum else Int.MIN_VALUE
87+
}
88+
}
89+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 1081\. Smallest Subsequence of Distinct Characters
5+
6+
Medium
7+
8+
Given a string `s`, return _the_ _lexicographically smallest_ _subsequence_ _of_ `s` _that contains all the distinct characters of_ `s` _exactly once_.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "bcabc"
13+
14+
**Output:** "abc"
15+
16+
**Example 2:**
17+
18+
**Input:** s = "cbacdcbc"
19+
20+
**Output:** "acdb"
21+
22+
**Constraints:**
23+
24+
* `1 <= s.length <= 1000`
25+
* `s` consists of lowercase English letters.
26+
27+
**Note:** This question is the same as 316: [https://leetcode.com/problems/remove-duplicate-letters/](https://leetcode.com/problems/remove-duplicate-letters/)
28+
29+
## Solution
30+
31+
```kotlin
32+
import java.util.Arrays
33+
import java.util.Deque
34+
import java.util.LinkedList
35+
36+
class Solution {
37+
fun smallestSubsequence(s: String): String {
38+
val n = s.length
39+
val stk: Deque<Char> = LinkedList()
40+
val freq = IntArray(26)
41+
val exist = BooleanArray(26)
42+
Arrays.fill(exist, false)
43+
for (ch in s.toCharArray()) {
44+
freq[ch.code - 'a'.code]++
45+
}
46+
for (i in 0 until n) {
47+
val ch = s[i]
48+
freq[ch.code - 'a'.code]--
49+
if (exist[ch.code - 'a'.code]) {
50+
continue
51+
}
52+
while (stk.isNotEmpty() && stk.peek() > ch && freq[stk.peek().code - 'a'.code] > 0) {
53+
val rem = stk.pop()
54+
exist[rem.code - 'a'.code] = false
55+
}
56+
stk.push(ch)
57+
exist[ch.code - 'a'.code] = true
58+
}
59+
val ans = CharArray(stk.size)
60+
var index = 0
61+
while (stk.isNotEmpty()) {
62+
ans[index] = stk.pop()
63+
index++
64+
}
65+
return StringBuilder(String(ans)).reverse().toString()
66+
}
67+
}
68+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 1084\. Sales Analysis III
5+
6+
Easy
7+
8+
SQL Schema
9+
10+
Table: `Product`
11+
12+
+--------------+---------+
13+
| Column Name | Type |
14+
+--------------+---------+
15+
| product_id | int |
16+
| product_name | varchar |
17+
| unit_price | int |
18+
+--------------+---------+
19+
product_id is the primary key of this table.
20+
Each row of this table indicates the name and the price of each product.
21+
22+
Table: `Sales`
23+
24+
+-------------+---------+
25+
| Column Name | Type |
26+
+-------------+---------+
27+
| seller_id | int |
28+
| product_id | int |
29+
| buyer_id | int |
30+
| sale_date | date |
31+
| quantity | int |
32+
| price | int |
33+
+-------------+---------+
34+
This table has no primary key, it can have repeated rows.
35+
product_id is a foreign key to the Product table.
36+
Each row of this table contains some information about one sale.
37+
38+
Write an SQL query that reports the **products** that were **only** sold in the first quarter of `2019`. That is, between `2019-01-01` and `2019-03-31` inclusive.
39+
40+
Return the result table in **any order**.
41+
42+
The query result format is in the following example.
43+
44+
**Example 1:**
45+
46+
**Input:**
47+
48+
Product table:
49+
+------------+--------------+------------+
50+
| product_id | product_name | unit_price |
51+
+------------+--------------+------------+
52+
| 1 | S8 | 1000 |
53+
| 2 | G4 | 800 |
54+
| 3 | iPhone | 1400 |
55+
+------------+--------------+------------+
56+
57+
Sales table:
58+
+-----------+------------+----------+------------+----------+-------+
59+
| seller_id | product_id | buyer_id | sale_date | quantity | price |
60+
+-----------+------------+----------+------------+----------+-------+
61+
| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
62+
| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
63+
| 2 | 2 | 3 | 2019-06-02 | 1 | 800 |
64+
| 3 | 3 | 4 | 2019-05-13 | 2 | 2800 |
65+
+-----------+------------+----------+------------+----------+-------+
66+
67+
**Output:**
68+
69+
+-------------+--------------+
70+
| product_id | product_name |
71+
+-------------+--------------+
72+
| 1 | S8 |
73+
+-------------+--------------+
74+
75+
**Explanation:**
76+
77+
The product with id 1 was only sold in the spring of 2019.
78+
79+
The product with id 2 was sold in the spring of 2019 but was also sold after the spring of 2019.
80+
81+
The product with id 3 was sold after spring 2019.
82+
83+
We return only product 1 as it is the product that was only sold in the spring of 2019.
84+
85+
## Solution
86+
87+
```sql
88+
# Write your MySQL query statement below
89+
SELECT P.product_id, P.product_name
90+
FROM Product P
91+
INNER JOIN Sales S ON P.product_id = S.product_id
92+
GROUP BY P.product_id
93+
HAVING MIN(S.sale_date) >= '2019-01-01' AND MAX(S.sale_date) <= '2019-03-31';
94+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 1089\. Duplicate Zeros
5+
6+
Easy
7+
8+
Given a fixed-length integer array `arr`, duplicate each occurrence of zero, shifting the remaining elements to the right.
9+
10+
**Note** that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
11+
12+
**Example 1:**
13+
14+
**Input:** arr = [1,0,2,3,0,4,5,0]
15+
16+
**Output:** [1,0,0,2,3,0,0,4]
17+
18+
**Explanation:** After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
19+
20+
**Example 2:**
21+
22+
**Input:** arr = [1,2,3]
23+
24+
**Output:** [1,2,3]
25+
26+
**Explanation:** After calling your function, the input array is modified to: [1,2,3]
27+
28+
**Constraints:**
29+
30+
* <code>1 <= arr.length <= 10<sup>4</sup></code>
31+
* `0 <= arr[i] <= 9`
32+
33+
## Solution
34+
35+
```kotlin
36+
class Solution {
37+
fun duplicateZeros(arr: IntArray) {
38+
var countZero = 0
39+
for (k in arr) {
40+
if (k == 0) {
41+
countZero++
42+
}
43+
}
44+
val len = arr.size + countZero
45+
// We just need O(1) space if we scan from back
46+
// i point to the original array, j point to the new location
47+
var i = arr.size - 1
48+
var j = len - 1
49+
while (i < j) {
50+
// copy twice when hit '0'
51+
if (arr[i] == 0) {
52+
if (j < arr.size) {
53+
arr[j] = arr[i]
54+
}
55+
j--
56+
}
57+
if (j < arr.size) {
58+
arr[j] = arr[i]
59+
}
60+
i--
61+
j--
62+
}
63+
}
64+
}
65+
```

0 commit comments

Comments
 (0)