Skip to content

Commit 9c9deb9

Browse files
Added tasks 2161, 2162.
1 parent 90e7b25 commit 9c9deb9

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2101_2200.s2161_partition_array_according_to_given_pivot;
2+
3+
// #Medium #Array #Two_Pointers #Simulation #2022_06_04_Time_7_ms_(72.76%)_Space_168.9_MB_(26.82%)
4+
5+
public class Solution {
6+
public int[] pivotArray(int[] nums, int pivot) {
7+
int[] ans = new int[nums.length];
8+
int point = 0;
9+
int equal = 0;
10+
for (int i : nums) {
11+
if (i < pivot) {
12+
ans[point] = i;
13+
++point;
14+
} else if (i == pivot) {
15+
++equal;
16+
}
17+
}
18+
while (equal > 0) {
19+
ans[point] = pivot;
20+
++point;
21+
--equal;
22+
}
23+
for (int i : nums) {
24+
if (i > pivot) {
25+
ans[point] = i;
26+
++point;
27+
}
28+
}
29+
return ans;
30+
}
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2161\. Partition Array According to Given Pivot
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` and an integer `pivot`. Rearrange `nums` such that the following conditions are satisfied:
6+
7+
* Every element less than `pivot` appears **before** every element greater than `pivot`.
8+
* Every element equal to `pivot` appears **in between** the elements less than and greater than `pivot`.
9+
* The **relative order** of the elements less than `pivot` and the elements greater than `pivot` is maintained.
10+
* More formally, consider every <code>p<sub>i</sub></code>, <code>p<sub>j</sub></code> where <code>p<sub>i</sub></code> is the new position of the <code>i<sup>th</sup></code> element and <code>p<sub>j</sub></code> is the new position of the <code>j<sup>th</sup></code> element. For elements less than `pivot`, if `i < j` and `nums[i] < pivot` and `nums[j] < pivot`, then <code>p<sub>i</sub> < p<sub>j</sub></code>. Similarly for elements greater than `pivot`, if `i < j` and `nums[i] > pivot` and `nums[j] > pivot`, then <code>p<sub>i</sub> < p<sub>j</sub></code>.
11+
12+
Return `nums` _after the rearrangement._
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [9,12,5,10,14,3,10], pivot = 10
17+
18+
**Output:** [9,5,3,10,10,12,14]
19+
20+
**Explanation:** The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
21+
22+
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
23+
24+
The relative ordering of the elements less than and greater than pivot is also maintained.
25+
26+
[9, 5, 3] and [12, 14] are the respective orderings.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [-3,4,3,2], pivot = 2
31+
32+
**Output:** [-3,2,4,3]
33+
34+
**Explanation:** The element -3 is less than the pivot so it is on the left side of the array.
35+
36+
elements 4 and 3 are greater than the pivot so they are on the right side of the array.
37+
38+
The relative ordering of the elements less than and greater than pivot is also maintained.
39+
40+
[-3] and [4, 3] are the respective orderings.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
45+
* <code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code>
46+
* `pivot` equals to an element of `nums`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2101_2200.s2162_minimum_cost_to_set_cooking_time;
2+
3+
// #Medium #Math #Enumeration #2022_06_04_Time_1_ms_(95.82%)_Space_40.8_MB_(69.87%)
4+
5+
public class Solution {
6+
public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
7+
int mins = targetSeconds / 60;
8+
int secs = targetSeconds % 60;
9+
return Math.min(
10+
cost(mins, secs, startAt, moveCost, pushCost),
11+
cost(mins - 1, secs + 60, startAt, moveCost, pushCost));
12+
}
13+
14+
private int cost(int mins, int secs, int startAt, int moveCost, int pushCost) {
15+
if (mins > 99 || secs > 99 || mins < 0 || secs < 0) {
16+
return Integer.MAX_VALUE;
17+
}
18+
String s = Integer.toString(mins * 100 + secs);
19+
char curr = (char) (startAt + '0');
20+
int res = 0;
21+
for (int i = 0; i < s.length(); i++) {
22+
if (s.charAt(i) == curr) {
23+
res += pushCost;
24+
} else {
25+
res += pushCost + moveCost;
26+
curr = s.charAt(i);
27+
}
28+
}
29+
return res;
30+
}
31+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2162\. Minimum Cost to Set Cooking Time
2+
3+
Medium
4+
5+
A generic microwave supports cooking times for:
6+
7+
* at least `1` second.
8+
* at most `99` minutes and `99` seconds.
9+
10+
To set the cooking time, you push **at most four digits**. The microwave normalizes what you push as four digits by **prepending zeroes**. It interprets the **first** two digits as the minutes and the **last** two digits as the seconds. It then **adds** them up as the cooking time. For example,
11+
12+
* You push `9` `5` `4` (three digits). It is normalized as `0954` and interpreted as `9` minutes and `54` seconds.
13+
* You push `0` `0` `0` `8` (four digits). It is interpreted as `0` minutes and `8` seconds.
14+
* You push `8` `0` `9` `0`. It is interpreted as `80` minutes and `90` seconds.
15+
* You push `8` `1` `3` `0`. It is interpreted as `81` minutes and `30` seconds.
16+
17+
You are given integers `startAt`, `moveCost`, `pushCost`, and `targetSeconds`. **Initially**, your finger is on the digit `startAt`. Moving the finger above **any specific digit** costs `moveCost` units of fatigue. Pushing the digit below the finger **once** costs `pushCost` units of fatigue.
18+
19+
There can be multiple ways to set the microwave to cook for `targetSeconds` seconds but you are interested in the way with the minimum cost.
20+
21+
Return _the **minimum cost** to set_ `targetSeconds` _seconds of cooking time_.
22+
23+
Remember that one minute consists of `60` seconds.
24+
25+
**Example 1:**
26+
27+
![](https://assets.leetcode.com/uploads/2021/12/30/1.png)
28+
29+
**Input:** startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600
30+
31+
**Output:** 6
32+
33+
**Explanation:** The following are the possible ways to set the cooking time.
34+
35+
- 1 0 0 0, interpreted as 10 minutes and 0 seconds.
36+
37+
The finger is already on digit 1, pushes 1 (with cost 1), moves to 0 (with cost 2), pushes 0 (with cost 1), pushes 0 (with cost 1), and pushes 0 (with cost 1).
38+
39+
The cost is: 1 + 2 + 1 + 1 + 1 = 6. This is the minimum cost.
40+
41+
- 0 9 6 0, interpreted as 9 minutes and 60 seconds. That is also 600 seconds.
42+
43+
The finger moves to 0 (with cost 2), pushes 0 (with cost 1), moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).
44+
45+
The cost is: 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12.
46+
47+
- 9 6 0, normalized as 0960 and interpreted as 9 minutes and 60 seconds.
48+
49+
The finger moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).
50+
51+
The cost is: 2 + 1 + 2 + 1 + 2 + 1 = 9.
52+
53+
**Example 2:**
54+
55+
![](https://assets.leetcode.com/uploads/2021/12/30/2.png)
56+
57+
**Input:** startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76
58+
59+
**Output:** 6
60+
61+
**Explanation:** The optimal way is to push two digits: 7 6, interpreted as 76 seconds.
62+
63+
The finger moves to 7 (with cost 1), pushes 7 (with cost 2), moves to 6 (with cost 1), and pushes 6 (with cost 2).
64+
65+
The total cost is: 1 + 2 + 1 + 2 = 6
66+
67+
Note other possible ways are 0076, 076, 0116, and 116, but none of them produces the minimum cost.
68+
69+
**Constraints:**
70+
71+
* `0 <= startAt <= 9`
72+
* <code>1 <= moveCost, pushCost <= 10<sup>5</sup></code>
73+
* `1 <= targetSeconds <= 6039`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2101_2200.s2161_partition_array_according_to_given_pivot;
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 pivotArray() {
11+
assertThat(
12+
new Solution().pivotArray(new int[] {9, 12, 5, 10, 14, 3, 10}, 10),
13+
equalTo(new int[] {9, 5, 3, 10, 10, 12, 14}));
14+
}
15+
16+
@Test
17+
void pivotArray2() {
18+
assertThat(
19+
new Solution().pivotArray(new int[] {-3, 4, 3, 2}, 2),
20+
equalTo(new int[] {-3, 2, 4, 3}));
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2162_minimum_cost_to_set_cooking_time;
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 minCostSetTime() {
11+
assertThat(new Solution().minCostSetTime(1, 2, 1, 600), equalTo(6));
12+
}
13+
14+
@Test
15+
void minCostSetTime2() {
16+
assertThat(new Solution().minCostSetTime(0, 1, 2, 76), equalTo(6));
17+
}
18+
}

0 commit comments

Comments
 (0)