Skip to content

Commit 8d9e6f5

Browse files
authored
Added tasks 1103, 1104, 1105, 1106, 1108.
1 parent d78327e commit 8d9e6f5

File tree

15 files changed

+486
-0
lines changed

15 files changed

+486
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1101_1200.s1103_distribute_candies_to_people;
2+
3+
// #Easy #Math #Simulation #2022_02_28_Time_2_ms_(49.07%)_Space_41.9_MB_(19.20%)
4+
5+
public class Solution {
6+
public int[] distributeCandies(int candies, int numPeople) {
7+
int[] candiesDistribution = new int[numPeople];
8+
int count = 1;
9+
10+
while (candies > 0) {
11+
for (int i = 0; i < numPeople; i++) {
12+
if (candies >= count) {
13+
candiesDistribution[i] += count;
14+
candies -= count;
15+
count++;
16+
} else if (candies > 0) {
17+
candiesDistribution[i] += candies;
18+
candies -= candies;
19+
}
20+
21+
if (candies == 0) {
22+
return candiesDistribution;
23+
}
24+
}
25+
}
26+
27+
return candiesDistribution;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
1103\. Distribute Candies to People
2+
3+
Easy
4+
5+
We distribute some number of `candies`, to a row of **`n = num_people`** people in the following way:
6+
7+
We then give 1 candy to the first person, 2 candies to the second person, and so on until we give `n` candies to the last person.
8+
9+
Then, we go back to the start of the row, giving `n + 1` candies to the first person, `n + 2` candies to the second person, and so on until we give `2 * n` candies to the last person.
10+
11+
This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining candies (not necessarily one more than the previous gift).
12+
13+
Return an array (of length `num_people` and sum `candies`) that represents the final distribution of candies.
14+
15+
**Example 1:**
16+
17+
**Input:** candies = 7, num\_people = 4
18+
19+
**Output:** [1,2,3,1]
20+
21+
**Explanation:**
22+
23+
On the first turn, ans[0] += 1, and the array is [1,0,0,0].
24+
25+
On the second turn, ans[1] += 2, and the array is [1,2,0,0].
26+
27+
On the third turn, ans[2] += 3, and the array is [1,2,3,0].
28+
29+
On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].
30+
31+
**Example 2:**
32+
33+
**Input:** candies = 10, num\_people = 3
34+
35+
**Output:** [5,2,3]
36+
37+
**Explanation:**
38+
39+
On the first turn, ans[0] += 1, and the array is [1,0,0].
40+
41+
On the second turn, ans[1] += 2, and the array is [1,2,0].
42+
43+
On the third turn, ans[2] += 3, and the array is [1,2,3].
44+
45+
On the fourth turn, ans[0] += 4, and the final array is [5,2,3].
46+
47+
**Constraints:**
48+
49+
* 1 <= candies <= 10^9
50+
* 1 <= num\_people <= 1000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1101_1200.s1104_path_in_zigzag_labelled_binary_tree;
2+
3+
// #Medium #Math #Tree #Binary_Tree #2022_02_28_Time_1_ms_(58.50%)_Space_41.6_MB_(28.60%)
4+
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<Integer> pathInZigZagTree(int label) {
10+
List<Integer> answer = new LinkedList<>();
11+
while (label != 0) {
12+
answer.add(0, label);
13+
int logNode = (int) (Math.log(label) / Math.log(2));
14+
int levelStart = (int) (Math.pow(2, logNode));
15+
int diff = label - levelStart;
16+
int d2 = diff / 2;
17+
int prevEnd = levelStart - 1;
18+
int prevLabel = prevEnd - d2;
19+
label = prevLabel;
20+
}
21+
return answer;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
1104\. Path In Zigzag Labelled Binary Tree
2+
3+
Medium
4+
5+
In an infinite binary tree where every node has two children, the nodes are labelled in row order.
6+
7+
In the odd numbered rows (ie., the first, third, fifth,...), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left.
8+
9+
![](https://assets.leetcode.com/uploads/2019/06/24/tree.png)
10+
11+
Given the `label` of a node in this tree, return the labels in the path from the root of the tree to the node with that `label`.
12+
13+
**Example 1:**
14+
15+
**Input:** label = 14
16+
17+
**Output:** [1,3,4,14]
18+
19+
**Example 2:**
20+
21+
**Input:** label = 26
22+
23+
**Output:** [1,2,6,10,26]
24+
25+
**Constraints:**
26+
27+
* `1 <= label <= 10^6`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1101_1200.s1105_filling_bookcase_shelves;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_02_28_Time_4_ms_(23.85%)_Space_42.7_MB_(31.80%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minHeightShelves(int[][] books, int shelfWidth) {
9+
int n = books.length;
10+
int[] dp = new int[n + 1];
11+
Arrays.fill(dp, Integer.MAX_VALUE);
12+
dp[0] = 0;
13+
for (int i = 1; i <= n; i++) {
14+
int widthLeft = shelfWidth;
15+
int maxH = 0;
16+
for (int j = i - 1; j >= 0; j--) {
17+
widthLeft -= books[j][0];
18+
maxH = Math.max(maxH, books[j][1]);
19+
if (widthLeft >= 0) {
20+
dp[i] = Math.min(dp[i], maxH + dp[j]);
21+
}
22+
}
23+
}
24+
return dp[n];
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1105\. Filling Bookcase Shelves
2+
3+
Medium
4+
5+
You are given an array `books` where <code>books[i] = [thickness<sub>i</sub>, height<sub>i</sub>]</code> indicates the thickness and height of the <code>i<sup>th</sup></code> book. You are also given an integer `shelfWidth`.
6+
7+
We want to place these books in order onto bookcase shelves that have a total width `shelfWidth`.
8+
9+
We choose some of the books to place on this shelf such that the sum of their thickness is less than or equal to `shelfWidth`, then build another level of the shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down. We repeat this process until there are no more books to place.
10+
11+
Note that at each step of the above process, the order of the books we place is the same order as the given sequence of books.
12+
13+
* For example, if we have an ordered list of `5` books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the fourth and fifth book on the last shelf.
14+
15+
Return _the minimum possible height that the total bookshelf can be after placing shelves in this manner_.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2019/06/24/shelves.png)
20+
21+
**Input:** books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelf\_width = 4
22+
23+
**Output:** 6
24+
25+
**Explanation:**
26+
27+
The sum of the heights of the 3 shelves is 1 + 3 + 2 = 6.
28+
29+
Notice that book number 2 does not have to be on the first shelf.
30+
31+
**Example 2:**
32+
33+
**Input:** books = [[1,3],[2,4],[3,2]], shelfWidth = 6
34+
35+
**Output:** 4
36+
37+
**Constraints:**
38+
39+
* `1 <= books.length <= 1000`
40+
* <code>1 <= thickness<sub>i</sub> <= shelfWidth <= 1000</code>
41+
* <code>1 <= height<sub>i</sub> <= 1000</code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package g1101_1200.s1106_parsing_a_boolean_expression;
2+
3+
// #Hard #String #Stack #Recursion #2022_02_28_Time_3_ms_(93.78%)_Space_42_MB_(70.54%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
private String source;
10+
private int index;
11+
12+
public boolean parseBoolExpr(String expression) {
13+
this.source = expression;
14+
this.index = 0;
15+
16+
return expr();
17+
}
18+
19+
private boolean expr() {
20+
boolean res = false;
21+
22+
if (match('!')) {
23+
res = not();
24+
} else if (match('&')) {
25+
res = and();
26+
} else if (match('|')) {
27+
res = or();
28+
} else {
29+
res = bool();
30+
}
31+
32+
return res;
33+
}
34+
35+
private boolean not() {
36+
consume('!', "Expect '!'");
37+
38+
return !group().get(0);
39+
}
40+
41+
private boolean or() {
42+
consume('|', "Expect '|'");
43+
44+
boolean res = false;
45+
for (boolean e : group()) {
46+
res |= e;
47+
}
48+
49+
return res;
50+
}
51+
52+
private boolean and() {
53+
consume('&', "Expect '&'");
54+
55+
boolean res = true;
56+
for (boolean e : group()) {
57+
res &= e;
58+
}
59+
60+
return res;
61+
}
62+
63+
private List<Boolean> group() {
64+
consume('(', "Expect '('");
65+
66+
List<Boolean> res = new ArrayList<>();
67+
while (!match(')')) {
68+
res.add(expr());
69+
70+
if (match(',')) {
71+
advance();
72+
}
73+
}
74+
75+
consume(')', "Expect ')'");
76+
77+
return res;
78+
}
79+
80+
private boolean bool() {
81+
boolean isTrue = match('t');
82+
advance();
83+
return isTrue;
84+
}
85+
86+
private boolean isAtEnd() {
87+
return index >= source.length();
88+
}
89+
90+
private void advance() {
91+
if (isAtEnd()) {
92+
return;
93+
}
94+
95+
index++;
96+
}
97+
98+
private char peek() {
99+
return source.charAt(index);
100+
}
101+
102+
private boolean match(char ch) {
103+
if (isAtEnd()) {
104+
return false;
105+
}
106+
107+
return peek() == ch;
108+
}
109+
110+
private void consume(char ch, String message) {
111+
if (!match(ch)) {
112+
return;
113+
}
114+
advance();
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
1106\. Parsing A Boolean Expression
2+
3+
Hard
4+
5+
Return the result of evaluating a given boolean `expression`, represented as a string.
6+
7+
An expression can either be:
8+
9+
* `"t"`, evaluating to `True`;
10+
* `"f"`, evaluating to `False`;
11+
* `"!(expr)"`, evaluating to the logical NOT of the inner expression `expr`;
12+
* `"&(expr1,expr2,...)"`, evaluating to the logical AND of 2 or more inner expressions `expr1, expr2, ...`;
13+
* `"|(expr1,expr2,...)"`, evaluating to the logical OR of 2 or more inner expressions `expr1, expr2, ...`
14+
15+
**Example 1:**
16+
17+
**Input:** expression = "!(f)"
18+
19+
**Output:** true
20+
21+
**Example 2:**
22+
23+
**Input:** expression = "|(f,t)"
24+
25+
**Output:** true
26+
27+
**Example 3:**
28+
29+
**Input:** expression = "&(t,f)"
30+
31+
**Output:** false
32+
33+
**Constraints:**
34+
35+
* <code>1 <= expression.length <= 2 * 10<sup>4</sup></code>
36+
* `expression[i]` consists of characters in `{'(', ')', '&', '|', '!', 't', 'f', ','}`.
37+
* `expression` is a valid expression representing a boolean, as given in the description.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g1101_1200.s1108_defanging_an_ip_address;
2+
3+
// #Easy #String #2022_02_28_Time_3_ms_(26.20%)_Space_42.8_MB_(5.79%)
4+
5+
public class Solution {
6+
public String defangIPaddr(String address) {
7+
return address.replaceAll("\\.", "\\[\\.\\]");
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
1108\. Defanging an IP Address
2+
3+
Easy
4+
5+
Given a valid (IPv4) IP `address`, return a defanged version of that IP address.
6+
7+
A _defanged IP address_ replaces every period `"."` with `"[.]"`.
8+
9+
**Example 1:**
10+
11+
**Input:** address = "1.1.1.1"
12+
13+
**Output:** "1[.]1[.]1[.]1"
14+
15+
**Example 2:**
16+
17+
**Input:** address = "255.100.50.0"
18+
19+
**Output:** "255[.]100[.]50[.]0"
20+
21+
**Constraints:**
22+
23+
* The given `address` is a valid IPv4 address.

0 commit comments

Comments
 (0)