Skip to content

Commit 8555b7c

Browse files
authored
Added tasks 330, 331.
1 parent 9090e3f commit 8555b7c

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0301_0400.s0330_patching_array;
2+
3+
public class Solution {
4+
public int minPatches(int[] nums, int n) {
5+
int res = 0;
6+
long sum = 0;
7+
int i = 0;
8+
while (sum < n) {
9+
// required number
10+
long req = sum + 1;
11+
if (i < nums.length && nums[i] <= req) {
12+
sum += nums[i++];
13+
} else {
14+
sum += req;
15+
res++;
16+
}
17+
}
18+
return res;
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
330\. Patching Array
2+
3+
Hard
4+
5+
Given a sorted integer array `nums` and an integer `n`, add/patch elements to the array such that any number in the range `[1, n]` inclusive can be formed by the sum of some elements in the array.
6+
7+
Return _the minimum number of patches required_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = \[1,3\], n = 6
12+
13+
**Output:** 1
14+
15+
**Explanation:**
16+
17+
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
18+
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
19+
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
20+
So we only need 1 patch.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = \[1,5,10\], n = 20
25+
26+
**Output:** 2
27+
28+
**Explanation:** The two patches can be \[2, 4\].
29+
30+
**Example 3:**
31+
32+
**Input:** nums = \[1,2,2\], n = 5
33+
34+
**Output:** 0
35+
36+
**Constraints:**
37+
38+
* `1 <= nums.length <= 1000`
39+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
40+
* `nums` is sorted in **ascending order**.
41+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0301_0400.s0331_verify_preorder_serialization_of_a_binary_tree;
2+
3+
public class Solution {
4+
public boolean isValidSerialization(String preorder) {
5+
int count = 1;
6+
int length = preorder.length();
7+
for (int i = 1; i <= length; i++) {
8+
if (i == length || preorder.charAt(i) == ',') {
9+
--count;
10+
if (count < 0) {
11+
return false;
12+
}
13+
count += preorder.charAt(i - 1) == '#' ? 0 : 2;
14+
}
15+
}
16+
return count == 0;
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
331\. Verify Preorder Serialization of a Binary Tree
2+
3+
Medium
4+
5+
One way to serialize a binary tree is to use **preorder traversal**. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as `'#'`.
6+
7+
![](https://assets.leetcode.com/uploads/2021/03/12/pre-tree.jpg)
8+
9+
For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,#"`, where `'#'` represents a null node.
10+
11+
Given a string of comma-separated values `preorder`, return `true` if it is a correct preorder traversal serialization of a binary tree.
12+
13+
It is **guaranteed** that each comma-separated value in the string must be either an integer or a character `'#'` representing null pointer.
14+
15+
You may assume that the input format is always valid.
16+
17+
* For example, it could never contain two consecutive commas, such as `"1,,3"`.
18+
19+
**Note: **You are not allowed to reconstruct the tree.
20+
21+
**Example 1:**
22+
23+
**Input:** preorder = "9,3,4,#,#,1,#,#,2,#,6,#,#"
24+
25+
**Output:** true
26+
27+
**Example 2:**
28+
29+
**Input:** preorder = "1,#"
30+
31+
**Output:** false
32+
33+
**Example 3:**
34+
35+
**Input:** preorder = "9,#,#,1"
36+
37+
**Output:** false
38+
39+
**Constraints:**
40+
41+
* <code>1 <= preorder.length <= 10<sup>4</sup></code>
42+
* `preorder` consist of integers in the range `[0, 100]` and `'#'` separated by commas `','`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g0301_0400.s0330_patching_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void minPatches() {
11+
assertThat(new Solution().minPatches(new int[] {1, 3}, 6), equalTo(1));
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g0301_0400.s0331_verify_preorder_serialization_of_a_binary_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void minPatches() {
11+
assertThat(new Solution().isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"), equalTo(true));
12+
}
13+
}

0 commit comments

Comments
 (0)