Skip to content

Commit 8682b79

Browse files
authored
Added tasks 2138, 2139, 2140, 2141.
1 parent 33b0816 commit 8682b79

File tree

12 files changed

+419
-0
lines changed

12 files changed

+419
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2101_2200.s2138_divide_a_string_into_groups_of_size_k;
2+
3+
// #Easy #String #Simulation #2022_06_05_Time_2_ms_(70.97%)_Space_42.6_MB_(65.21%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public String[] divideString(String s, int k, char fill) {
10+
String[] ans = new String[(s.length() % k != 0) ? (s.length() / k) + 1 : s.length() / k];
11+
int t = k;
12+
List<String> str = new ArrayList<>();
13+
StringBuilder sb = new StringBuilder();
14+
for (int i = 0; i < s.length(); i++) {
15+
if (t > 0) {
16+
sb.append(s.charAt(i));
17+
t--;
18+
} else {
19+
t = k;
20+
str.add(sb.toString());
21+
sb.setLength(0);
22+
i--;
23+
}
24+
}
25+
if (t > 0) {
26+
while (t-- > 0) {
27+
sb.append(fill);
28+
}
29+
}
30+
str.add(sb.toString());
31+
for (int i = 0; i < str.size(); i++) {
32+
ans[i] = str.get(i);
33+
}
34+
return ans;
35+
}
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2138\. Divide a String Into Groups of Size k
2+
3+
Easy
4+
5+
A string `s` can be partitioned into groups of size `k` using the following procedure:
6+
7+
* The first group consists of the first `k` characters of the string, the second group consists of the next `k` characters of the string, and so on. Each character can be a part of **exactly one** group.
8+
* For the last group, if the string **does not** have `k` characters remaining, a character `fill` is used to complete the group.
9+
10+
Note that the partition is done so that after removing the `fill` character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be `s`.
11+
12+
Given the string `s`, the size of each group `k` and the character `fill`, return _a string array denoting the **composition of every group**_ `s` _has been divided into, using the above procedure_.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "abcdefghi", k = 3, fill = "x"
17+
18+
**Output:** ["abc","def","ghi"]
19+
20+
**Explanation:**
21+
22+
The first 3 characters "abc" form the first group.
23+
24+
The next 3 characters "def" form the second group.
25+
26+
The last 3 characters "ghi" form the third group.
27+
28+
Since all groups can be completely filled by characters from the string, we do not need to use fill.
29+
30+
Thus, the groups formed are "abc", "def", and "ghi".
31+
32+
**Example 2:**
33+
34+
**Input:** s = "abcdefghij", k = 3, fill = "x"
35+
36+
**Output:** ["abc","def","ghi","jxx"]
37+
38+
**Explanation:**
39+
40+
Similar to the previous example, we are forming the first three groups "abc", "def", and "ghi".
41+
42+
For the last group, we can only use the character 'j' from the string.
43+
44+
To complete this group, we add 'x' twice. Thus, the 4 groups formed are "abc", "def", "ghi", and "jxx".
45+
46+
**Constraints:**
47+
48+
* `1 <= s.length <= 100`
49+
* `s` consists of lowercase English letters only.
50+
* `1 <= k <= 100`
51+
* `fill` is a lowercase English letter.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2101_2200.s2139_minimum_moves_to_reach_target_score;
2+
3+
// #Medium #Math #Greedy #2022_06_05_Time_1_ms_(37.95%)_Space_40.8_MB_(55.49%)
4+
5+
public class Solution {
6+
public int minMoves(int target, int maxDoubles) {
7+
int count = 0;
8+
while (target > 1) {
9+
if (maxDoubles > 0 && target % 2 == 0) {
10+
maxDoubles--;
11+
target = target / 2;
12+
13+
} else {
14+
if (maxDoubles == 0) {
15+
count = count + target - 1;
16+
return count;
17+
} else {
18+
target = target - 1;
19+
}
20+
}
21+
22+
count++;
23+
}
24+
return count;
25+
}
26+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2139\. Minimum Moves to Reach Target Score
2+
3+
Medium
4+
5+
You are playing a game with integers. You start with the integer `1` and you want to reach the integer `target`.
6+
7+
In one move, you can either:
8+
9+
* **Increment** the current integer by one (i.e., `x = x + 1`).
10+
* **Double** the current integer (i.e., `x = 2 * x`).
11+
12+
You can use the **increment** operation **any** number of times, however, you can only use the **double** operation **at most** `maxDoubles` times.
13+
14+
Given the two integers `target` and `maxDoubles`, return _the minimum number of moves needed to reach_ `target` _starting with_ `1`.
15+
16+
**Example 1:**
17+
18+
**Input:** target = 5, maxDoubles = 0
19+
20+
**Output:** 4
21+
22+
**Explanation:** Keep incrementing by 1 until you reach target.
23+
24+
**Example 2:**
25+
26+
**Input:** target = 19, maxDoubles = 2
27+
28+
**Output:** 7
29+
30+
**Explanation:** Initially, x = 1
31+
32+
Increment 3 times so x = 4
33+
34+
Double once so x = 8
35+
36+
Increment once so x = 9
37+
38+
Double again so x = 18
39+
40+
Increment once so x = 19
41+
42+
**Example 3:**
43+
44+
**Input:** target = 10, maxDoubles = 4
45+
46+
**Output:** 4
47+
48+
**Explanation:** Initially, x = 1
49+
50+
Increment once so x = 2
51+
52+
Double once so x = 4
53+
54+
Increment once so x = 5
55+
56+
Double again so x = 10
57+
58+
**Constraints:**
59+
60+
* <code>1 <= target <= 10<sup>9</sup></code>
61+
* `0 <= maxDoubles <= 100`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2101_2200.s2140_solving_questions_with_brainpower;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_06_05_Time_5_ms_(98.77%)_Space_92.6_MB_(94.67%)
4+
5+
public class Solution {
6+
public long mostPoints(int[][] questions) {
7+
int n = questions.length;
8+
long[] memo = new long[n];
9+
memo[n - 1] = questions[n - 1][0];
10+
for (int i = n - 2; i >= 0; i--) {
11+
if (i + questions[i][1] + 1 < n) {
12+
memo[i] = Math.max(memo[i + 1], questions[i][0] + memo[i + questions[i][1] + 1]);
13+
} else {
14+
memo[i] = Math.max(memo[i + 1], questions[i][0]);
15+
}
16+
}
17+
return memo[0];
18+
}
19+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2140\. Solving Questions With Brainpower
2+
3+
Medium
4+
5+
You are given a **0-indexed** 2D integer array `questions` where <code>questions[i] = [points<sub>i</sub>, brainpower<sub>i</sub>]</code>.
6+
7+
The array describes the questions of an exam, where you have to process the questions **in order** (i.e., starting from question `0`) and make a decision whether to **solve** or **skip** each question. Solving question `i` will **earn** you <code>points<sub>i</sub></code> points but you will be **unable** to solve each of the next <code>brainpower<sub>i</sub></code> questions. If you skip question `i`, you get to make the decision on the next question.
8+
9+
* For example, given `questions = [[3, 2], [4, 3], [4, 4], [2, 5]]`:
10+
* If question `0` is solved, you will earn `3` points but you will be unable to solve questions `1` and `2`.
11+
* If instead, question `0` is skipped and question `1` is solved, you will earn `4` points but you will be unable to solve questions `2` and `3`.
12+
13+
Return _the **maximum** points you can earn for the exam_.
14+
15+
**Example 1:**
16+
17+
**Input:** questions = [[3,2],[4,3],[4,4],[2,5]]
18+
19+
**Output:** 5
20+
21+
**Explanation:** The maximum points can be earned by solving questions 0 and 3.
22+
23+
- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
24+
25+
- Unable to solve questions 1 and 2
26+
27+
- Solve question 3: Earn 2 points
28+
29+
Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.
30+
31+
**Example 2:**
32+
33+
**Input:** questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
34+
35+
**Output:** 7
36+
37+
**Explanation:** The maximum points can be earned by solving questions 1 and 4.
38+
39+
- Skip question 0
40+
41+
- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
42+
43+
- Unable to solve questions 2 and 3
44+
45+
- Solve question 4: Earn 5 points
46+
47+
Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= questions.length <= 10<sup>5</sup></code>
52+
* `questions[i].length == 2`
53+
* <code>1 <= points<sub>i</sub>, brainpower<sub>i</sub> <= 10<sup>5</sup></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g2101_2200.s2141_maximum_running_time_of_n_computers;
2+
3+
// #Hard #Array #Sorting #Greedy #Binary_Search
4+
// #2022_06_05_Time_24_ms_(70.39%)_Space_83.1_MB_(5.83%)
5+
6+
public class Solution {
7+
private boolean isPossibeToRun(int n, int[] batteries, long avgTime) {
8+
long duration = 0;
9+
for (long ele : batteries) {
10+
duration += Math.min(ele, avgTime);
11+
}
12+
return avgTime * n <= duration;
13+
}
14+
15+
public long maxRunTime(int n, int[] batteries) {
16+
long startTime = 0;
17+
long sum = 0;
18+
long ans = 0;
19+
for (long ele : batteries) {
20+
sum += ele;
21+
}
22+
long endTime = sum;
23+
while (startTime <= endTime) {
24+
long avgTime = (startTime + endTime) / 2;
25+
if (isPossibeToRun(n, batteries, avgTime)) {
26+
ans = avgTime;
27+
startTime = avgTime + 1;
28+
} else {
29+
endTime = avgTime - 1;
30+
}
31+
}
32+
return ans;
33+
}
34+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2141\. Maximum Running Time of N Computers
2+
3+
Hard
4+
5+
You have `n` computers. You are given the integer `n` and a **0-indexed** integer array `batteries` where the <code>i<sup>th</sup></code> battery can **run** a computer for `batteries[i]` minutes. You are interested in running **all** `n` computers **simultaneously** using the given batteries.
6+
7+
Initially, you can insert **at most one battery** into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery **any number of times**. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.
8+
9+
Note that the batteries cannot be recharged.
10+
11+
Return _the **maximum** number of minutes you can run all the_ `n` _computers simultaneously._
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2022/01/06/example1-fit.png)
16+
17+
**Input:** n = 2, batteries = [3,3,3]
18+
19+
**Output:** 4
20+
21+
**Explanation:**
22+
23+
Initially, insert battery 0 into the first computer and battery 1 into the second computer.
24+
25+
After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute.
26+
27+
At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead.
28+
29+
By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running.
30+
31+
We can run the two computers simultaneously for at most 4 minutes, so we return 4.
32+
33+
**Example 2:**
34+
35+
![](https://assets.leetcode.com/uploads/2022/01/06/example2.png)
36+
37+
**Input:** n = 2, batteries = [1,1,1,1]
38+
39+
**Output:** 2
40+
41+
**Explanation:**
42+
43+
Initially, insert battery 0 into the first computer and battery 2 into the second computer.
44+
45+
After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer.
46+
47+
After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running.
48+
49+
We can run the two computers simultaneously for at most 2 minutes, so we return 2.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= n <= batteries.length <= 10<sup>5</sup></code>
54+
* <code>1 <= batteries[i] <= 10<sup>9</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2101_2200.s2138_divide_a_string_into_groups_of_size_k;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void divideString() {
11+
assertThat(
12+
new Solution().divideString("abcdefghi", 3, 'x'),
13+
equalTo(new String[] {"abc", "def", "ghi"}));
14+
}
15+
16+
@Test
17+
void divideString2() {
18+
assertThat(
19+
new Solution().divideString("abcdefghij", 3, 'x'),
20+
equalTo(new String[] {"abc", "def", "ghi", "jxx"}));
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2101_2200.s2139_minimum_moves_to_reach_target_score;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minMoves() {
11+
assertThat(new Solution().minMoves(5, 0), equalTo(4));
12+
}
13+
14+
@Test
15+
void minMoves2() {
16+
assertThat(new Solution().minMoves(19, 2), equalTo(7));
17+
}
18+
19+
@Test
20+
void minMoves3() {
21+
assertThat(new Solution().minMoves(10, 4), equalTo(4));
22+
}
23+
}

0 commit comments

Comments
 (0)