Skip to content

Commit 4569fce

Browse files
authored
Added tasks 2466, 2467, 2468, 2469.
1 parent 4718144 commit 4569fce

File tree

13 files changed

+491
-0
lines changed

13 files changed

+491
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,10 @@ implementation 'com.github.javadev:leetcode-in-java:1.17'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2469 |[Convert the Temperature](src/main/java/g2401_2500/s2469_convert_the_temperature/Solution.java)| Easy | Math | 0 | 100.00
1852+
| 2468 |[Split Message Based on Limit](src/main/java/g2401_2500/s2468_split_message_based_on_limit/Solution.java)| Hard | String, Binary_Search | 27 | 99.08
1853+
| 2467 |[Most Profitable Path in a Tree](src/main/java/g2401_2500/s2467_most_profitable_path_in_a_tree/Solution.java)| Medium | Array, Tree, Graph, Depth_First_Search, Breadth_First_Search | 20 | 100.00
1854+
| 2466 |[Count Ways To Build Good Strings](src/main/java/g2401_2500/s2466_count_ways_to_build_good_strings/Solution.java)| Medium | Dynamic_Programming | 8 | 99.59
18511855
| 2465 |[Number of Distinct Averages](src/main/java/g2401_2500/s2465_number_of_distinct_averages/Solution.java)| Easy | Array, Hash_Table, Sorting, Two_Pointers | 1 | 99.48
18521856
| 2463 |[Minimum Total Distance Traveled](src/main/java/g2401_2500/s2463_minimum_total_distance_traveled/Solution.java)| Hard | Array, Dynamic_Programming, Sorting | 2 | 100.00
18531857
| 2462 |[Total Cost to Hire K Workers](src/main/java/g2401_2500/s2462_total_cost_to_hire_k_workers/Solution.java)| Medium | Array, Two_Pointers, Heap_Priority_Queue, Simulation | 57 | 96.24
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2401_2500.s2466_count_ways_to_build_good_strings;
2+
3+
// #Medium #Dynamic_Programming #2023_01_11_Time_8_ms_(99.59%)_Space_41_MB_(98.19%)
4+
5+
public class Solution {
6+
public int countGoodStrings(int low, int high, int zero, int one) {
7+
int[] dp = new int[high + 1];
8+
dp[zero]++;
9+
dp[one]++;
10+
int ans = 0;
11+
for (int i = 0; i < high + 1; i++) {
12+
if (dp[i] != 0) {
13+
if (i + zero <= high) {
14+
dp[i + zero] += dp[i];
15+
dp[i + zero] = dp[i + zero] % 1000000007;
16+
}
17+
if (i + one <= high) {
18+
dp[i + one] += dp[i];
19+
dp[i + one] = dp[i + one] % 1000000007;
20+
}
21+
if (i >= low) {
22+
ans += dp[i];
23+
ans = ans % 1000000007;
24+
}
25+
}
26+
}
27+
return ans;
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2466\. Count Ways To Build Good Strings
2+
3+
Medium
4+
5+
Given the integers `zero`, `one`, `low`, and `high`, we can construct a string by starting with an empty string, and then at each step perform either of the following:
6+
7+
* Append the character `'0'` `zero` times.
8+
* Append the character `'1'` `one` times.
9+
10+
This can be performed any number of times.
11+
12+
A **good** string is a string constructed by the above process having a **length** between `low` and `high` (**inclusive**).
13+
14+
Return _the number of **different** good strings that can be constructed satisfying these properties._ Since the answer can be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
15+
16+
**Example 1:**
17+
18+
**Input:** low = 3, high = 3, zero = 1, one = 1
19+
20+
**Output:** 8
21+
22+
**Explanation:**
23+
24+
One possible valid good string is "011".
25+
26+
It can be constructed as follows: "" -> "0" -> "01" -> "011".
27+
28+
All binary strings from "000" to "111" are good strings in this example.
29+
30+
**Example 2:**
31+
32+
**Input:** low = 2, high = 3, zero = 1, one = 2
33+
34+
**Output:** 5
35+
36+
**Explanation:** The good strings are "00", "11", "000", "110", and "011".
37+
38+
**Constraints:**
39+
40+
* <code>1 <= low <= high <= 10<sup>5</sup></code>
41+
* `1 <= zero, one <= low`
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package g2401_2500.s2467_most_profitable_path_in_a_tree;
2+
3+
// #Medium #Array #Tree #Graph #Depth_First_Search #Breadth_First_Search
4+
// #2023_01_11_Time_20_ms_(100.00%)_Space_94_MB_(96.10%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int mostProfitablePath(int[][] edges, int bob, int[] amount) {
10+
int n = amount.length;
11+
int[][] g = packU(n, edges);
12+
int[][] pars = parents(g, 0);
13+
int[] par = pars[0];
14+
int[] ord = pars[1];
15+
int[] dep = pars[2];
16+
int u = bob;
17+
for (int i = 0; i < (dep[bob] + 1) / 2; i++) {
18+
amount[u] = 0;
19+
u = par[u];
20+
}
21+
if (dep[bob] % 2 == 0) {
22+
amount[u] /= 2;
23+
}
24+
int[] dp = new int[n];
25+
for (int i = n - 1; i >= 0; i--) {
26+
int cur = ord[i];
27+
if (g[cur].length == 1 && i > 0) {
28+
dp[cur] = amount[cur];
29+
} else {
30+
dp[cur] = Integer.MIN_VALUE / 2;
31+
for (int e : g[cur]) {
32+
if (par[cur] == e) {
33+
continue;
34+
}
35+
dp[cur] = Math.max(dp[cur], dp[e] + amount[cur]);
36+
}
37+
}
38+
}
39+
return dp[0];
40+
}
41+
42+
private int[][] parents(int[][] g, int root) {
43+
int n = g.length;
44+
int[] par = new int[n];
45+
Arrays.fill(par, -1);
46+
int[] depth = new int[n];
47+
depth[0] = 0;
48+
int[] q = new int[n];
49+
q[0] = root;
50+
int r = 1;
51+
for (int p = 0; p < r; p++) {
52+
int cur = q[p];
53+
for (int nex : g[cur]) {
54+
if (par[cur] != nex) {
55+
q[r++] = nex;
56+
par[nex] = cur;
57+
depth[nex] = depth[cur] + 1;
58+
}
59+
}
60+
}
61+
return new int[][] {par, q, depth};
62+
}
63+
64+
private int[][] packU(int n, int[][] ft) {
65+
int[][] g = new int[n][];
66+
int[] p = new int[n];
67+
for (int[] u : ft) {
68+
p[u[0]]++;
69+
p[u[1]]++;
70+
}
71+
for (int i = 0; i < n; i++) {
72+
g[i] = new int[p[i]];
73+
}
74+
for (int[] u : ft) {
75+
g[u[0]][--p[u[0]]] = u[1];
76+
g[u[1]][--p[u[1]]] = u[0];
77+
}
78+
return g;
79+
}
80+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2467\. Most Profitable Path in a Tree
2+
3+
Medium
4+
5+
There is an undirected tree with `n` nodes labeled from `0` to `n - 1`, rooted at node `0`. You are given a 2D integer array `edges` of length `n - 1` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.
6+
7+
At every node `i`, there is a gate. You are also given an array of even integers `amount`, where `amount[i]` represents:
8+
9+
* the price needed to open the gate at node `i`, if `amount[i]` is negative, or,
10+
* the cash reward obtained on opening the gate at node `i`, otherwise.
11+
12+
The game goes on as follows:
13+
14+
* Initially, Alice is at node `0` and Bob is at node `bob`.
15+
* At every second, Alice and Bob **each** move to an adjacent node. Alice moves towards some **leaf node**, while Bob moves towards node `0`.
16+
* For **every** node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
17+
* If the gate is **already open**, no price will be required, nor will there be any cash reward.
18+
* If Alice and Bob reach the node **simultaneously**, they share the price/reward for opening the gate there. In other words, if the price to open the gate is `c`, then both Alice and Bob pay `c / 2` each. Similarly, if the reward at the gate is `c`, both of them receive `c / 2` each.
19+
* If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node `0`, he stops moving. Note that these events are **independent** of each other.
20+
21+
Return _the **maximum** net income Alice can have if she travels towards the optimal leaf node._
22+
23+
**Example 1:**
24+
25+
![](https://assets.leetcode.com/uploads/2022/10/29/eg1.png)
26+
27+
**Input:** edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
28+
29+
**Output:** 6
30+
31+
**Explanation:**
32+
33+
The above diagram represents the given tree. The game goes as follows:
34+
35+
- Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes.
36+
37+
Alice's net income is now -2.
38+
39+
- Both Alice and Bob move to node 1.
40+
41+
Since they reach here simultaneously, they open the gate together and share the reward.
42+
43+
Alice's net income becomes -2 + (4 / 2) = 0.
44+
45+
- Alice moves on to node 3. Since Bob already opened its gate, Alice's income remains unchanged.
46+
47+
Bob moves on to node 0, and stops moving.
48+
49+
- Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6.
50+
51+
Now, neither Alice nor Bob can make any further moves, and the game ends.
52+
53+
It is not possible for Alice to get a higher net income.
54+
55+
**Example 2:**
56+
57+
![](https://assets.leetcode.com/uploads/2022/10/29/eg2.png)
58+
59+
**Input:** edges = [[0,1]], bob = 1, amount = [-7280,2350]
60+
61+
**Output:** -7280
62+
63+
**Explanation:**
64+
65+
Alice follows the path 0->1 whereas Bob follows the path 1->0.
66+
67+
Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280.
68+
69+
**Constraints:**
70+
71+
* <code>2 <= n <= 10<sup>5</sup></code>
72+
* `edges.length == n - 1`
73+
* `edges[i].length == 2`
74+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
75+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
76+
* `edges` represents a valid tree.
77+
* `1 <= bob < n`
78+
* `amount.length == n`
79+
* `amount[i]` is an **even** integer in the range <code>[-10<sup>4</sup>, 10<sup>4</sup>]</code>.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g2401_2500.s2468_split_message_based_on_limit;
2+
3+
// #Hard #String #Binary_Search #2023_01_11_Time_27_ms_(99.08%)_Space_50.2_MB_(94.85%)
4+
5+
@SuppressWarnings("java:S3518")
6+
public class Solution {
7+
public String[] splitMessage(String message, int limit) {
8+
int total = 0;
9+
int running = 0;
10+
int count;
11+
int totalReq;
12+
int valUsed = -1;
13+
int minLimitReq;
14+
for (int i = 1; i <= message.length(); ++i) {
15+
count = getCount(i);
16+
running += count;
17+
total = running + (count * i) + 3 * i;
18+
totalReq = total + message.length();
19+
minLimitReq = (totalReq + i - 1) / i;
20+
if (minLimitReq <= limit) {
21+
valUsed = i;
22+
break;
23+
}
24+
}
25+
if (valUsed == -1) {
26+
return new String[] {};
27+
}
28+
StringBuilder sb = new StringBuilder();
29+
int idx = 0;
30+
StringBuilder sb2 = new StringBuilder();
31+
int left;
32+
String[] result = new String[valUsed];
33+
for (int i = 1; i <= valUsed; ++i) {
34+
sb2.setLength(0);
35+
sb.setLength(0);
36+
sb2.append('<');
37+
sb2.append(i);
38+
sb2.append('/');
39+
sb2.append(valUsed);
40+
sb2.append('>');
41+
left = limit - sb2.length();
42+
while (idx < message.length() && left-- > 0) {
43+
sb.append(message.charAt(idx++));
44+
}
45+
sb.append(sb2);
46+
result[i - 1] = sb.toString();
47+
}
48+
return result;
49+
}
50+
51+
private int getCount(int val) {
52+
int result = 0;
53+
while (val != 0) {
54+
val /= 10;
55+
++result;
56+
}
57+
return result;
58+
}
59+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2468\. Split Message Based on Limit
2+
3+
Hard
4+
5+
You are given a string, `message`, and a positive integer, `limit`.
6+
7+
You must **split** `message` into one or more **parts** based on `limit`. Each resulting part should have the suffix `"<a/b>"`, where `"b"` is to be **replaced** with the total number of parts and `"a"` is to be **replaced** with the index of the part, starting from `1` and going up to `b`. Additionally, the length of each resulting part (including its suffix) should be **equal** to `limit`, except for the last part whose length can be **at most** `limit`.
8+
9+
The resulting parts should be formed such that when their suffixes are removed and they are all concatenated **in order**, they should be equal to `message`. Also, the result should contain as few parts as possible.
10+
11+
Return _the parts_ `message` _would be split into as an array of strings_. If it is impossible to split `message` as required, return _an empty array_.
12+
13+
**Example 1:**
14+
15+
**Input:** message = "this is really a very awesome message", limit = 9
16+
17+
**Output:** ["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
18+
19+
**Explanation:**
20+
21+
The first 9 parts take 3 characters each from the beginning of message.
22+
23+
The next 5 parts take 2 characters each to finish splitting message.
24+
25+
In this example, each part, including the last, has length 9.
26+
27+
It can be shown it is not possible to split message into less than 14 parts.
28+
29+
**Example 2:**
30+
31+
**Input:** message = "short message", limit = 15
32+
33+
**Output:** ["short mess<1/2>","age<2/2>"]
34+
35+
**Explanation:**
36+
37+
Under the given constraints, the string can be split into two parts:
38+
39+
- The first part comprises of the first 10 characters, and has a length 15.
40+
41+
- The next part comprises of the last 3 characters, and has a length 8.
42+
43+
**Constraints:**
44+
45+
* <code>1 <= message.length <= 10<sup>4</sup></code>
46+
* `message` consists only of lowercase English letters and `' '`.
47+
* <code>1 <= limit <= 10<sup>4</sup></code>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g2401_2500.s2469_convert_the_temperature;
2+
3+
// #Easy #Math #2023_01_11_Time_0_ms_(100.00%)_Space_40.6_MB_(87.87%)
4+
5+
public class Solution {
6+
public double[] convertTemperature(double celsius) {
7+
double kelvin = celsius + 273.15;
8+
double fahrenheit = celsius * 1.80 + 32.00;
9+
double[] arr = new double[2];
10+
arr[0] = kelvin;
11+
arr[1] = fahrenheit;
12+
return arr;
13+
}
14+
}

0 commit comments

Comments
 (0)