Skip to content

Commit 650de0c

Browse files
authored
Added tasks 1547, 1550, 1551, 1552, 1553.
1 parent ebeb0a6 commit 650de0c

File tree

15 files changed

+401
-0
lines changed

15 files changed

+401
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1501_1600.s1547_minimum_cost_to_cut_a_stick;
2+
3+
// #Hard #Array #Dynamic_Programming #2022_04_11_Time_6_ms_(100.00%)_Space_41.9_MB_(90.56%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minCost(int n, int[] cuts) {
9+
Arrays.sort(cuts);
10+
int m = cuts.length;
11+
int[][] dp = new int[m + 1][m + 1];
12+
13+
for (int i = 1; i <= m; i++) {
14+
for (int j = 0; j <= m - i; j++) {
15+
int k = j + i;
16+
int min = Integer.MAX_VALUE;
17+
for (int p = j; p < k; p++) {
18+
min = Math.min(min, dp[j][p] + dp[p + 1][k]);
19+
}
20+
int len = (k == m ? n : cuts[k]) - (j == 0 ? 0 : cuts[j - 1]);
21+
dp[j][k] = min + len;
22+
}
23+
}
24+
return dp[0][m];
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1547\. Minimum Cost to Cut a Stick
2+
3+
Hard
4+
5+
Given a wooden stick of length `n` units. The stick is labelled from `0` to `n`. For example, a stick of length **6** is labelled as follows:
6+
7+
![](https://assets.leetcode.com/uploads/2020/07/21/statement.jpg)
8+
9+
Given an integer array `cuts` where `cuts[i]` denotes a position you should perform a cut at.
10+
11+
You should perform the cuts in order, you can change the order of the cuts as you wish.
12+
13+
The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.
14+
15+
Return _the minimum total cost_ of the cuts.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2020/07/23/e1.jpg)
20+
21+
**Input:** n = 7, cuts = [1,3,4,5]
22+
23+
**Output:** 16
24+
25+
**Explanation:** Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario: ![](https://assets.leetcode.com/uploads/2020/07/21/e11.jpg) The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16).
26+
27+
**Example 2:**
28+
29+
**Input:** n = 9, cuts = [5,6,1,4,2]
30+
31+
**Output:** 22
32+
33+
**Explanation:** If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.
34+
35+
**Constraints:**
36+
37+
* <code>2 <= n <= 10<sup>6</sup></code>
38+
* `1 <= cuts.length <= min(n - 1, 100)`
39+
* `1 <= cuts[i] <= n - 1`
40+
* All the integers in `cuts` array are **distinct**.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g1501_1600.s1550_three_consecutive_odds;
2+
3+
// #Easy #Array #2022_04_11_Time_0_ms_(100.00%)_Space_43.3_MB_(21.06%)
4+
5+
public class Solution {
6+
public boolean threeConsecutiveOdds(int[] arr) {
7+
for (int i = 0; i < arr.length - 2; i++) {
8+
if (arr[i] % 2 == 1 && arr[i + 1] % 2 == 1 && arr[i + 2] % 2 == 1) {
9+
return true;
10+
}
11+
}
12+
return false;
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
1550\. Three Consecutive Odds
2+
3+
Easy
4+
5+
Given an integer array `arr`, return `true` if there are three consecutive odd numbers in the array. Otherwise, return `false`.
6+
7+
**Example 1:**
8+
9+
**Input:** arr = [2,6,4,1]
10+
11+
**Output:** false
12+
13+
**Explanation:** There are no three consecutive odds.
14+
15+
**Example 2:**
16+
17+
**Input:** arr = [1,2,34,3,4,5,7,23,12]
18+
19+
**Output:** true
20+
21+
**Explanation:** [5,7,23] are three consecutive odds.
22+
23+
**Constraints:**
24+
25+
* `1 <= arr.length <= 1000`
26+
* `1 <= arr[i] <= 1000`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g1501_1600.s1551_minimum_operations_to_make_array_equal;
2+
3+
// #Medium #Math #2022_04_11_Time_7_ms_(12.93%)_Space_40.9_MB_(68.22%)
4+
5+
public class Solution {
6+
public int minOperations(int n) {
7+
int min = 1;
8+
int max = 2 * (n - 1) + 1;
9+
int equalNumber = (max + min) / 2;
10+
int ops = 0;
11+
for (int i = 0; i < n / 2; i++) {
12+
ops += equalNumber - (2 * i + 1);
13+
}
14+
return ops;
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
1551\. Minimum Operations to Make Array Equal
2+
3+
Medium
4+
5+
You have an array `arr` of length `n` where `arr[i] = (2 * i) + 1` for all valid values of `i` (i.e., `0 <= i < n`).
6+
7+
In one operation, you can select two indices `x` and `y` where `0 <= x, y < n` and subtract `1` from `arr[x]` and add `1` to `arr[y]` (i.e., perform `arr[x] -=1` and `arr[y] += 1`). The goal is to make all the elements of the array **equal**. It is **guaranteed** that all the elements of the array can be made equal using some operations.
8+
9+
Given an integer `n`, the length of the array, return _the minimum number of operations_ needed to make all the elements of arr equal.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 3
14+
15+
**Output:** 2
16+
17+
**Explanation:** arr = [1, 3, 5] First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4] In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
18+
19+
**Example 2:**
20+
21+
**Input:** n = 6
22+
23+
**Output:** 9
24+
25+
**Constraints:**
26+
27+
* <code>1 <= n <= 10<sup>4</sup></code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g1501_1600.s1552_magnetic_force_between_two_balls;
2+
3+
// #Medium #Array #Sorting #Binary_Search #2022_04_11_Time_39_ms_(99.65%)_Space_56.9_MB_(90.25%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxDistance(int[] position, int m) {
9+
Arrays.sort(position);
10+
return binarySearch(position, m);
11+
}
12+
13+
private int binarySearch(int[] arr, int m) {
14+
int low = 0;
15+
int n = arr.length;
16+
int high = arr[n - 1];
17+
int max = -1;
18+
while (low <= high) {
19+
int mid = low + (high - low) / 2;
20+
if (check(arr, mid, m)) {
21+
if (max < mid) {
22+
max = mid;
23+
}
24+
low = mid + 1;
25+
} else {
26+
high = mid - 1;
27+
}
28+
}
29+
return max;
30+
}
31+
32+
private boolean check(int[] arr, int mid, int m) {
33+
int pos = arr[0];
34+
int magnet = 1;
35+
for (int i = 1; i < arr.length; i++) {
36+
if (arr[i] - pos >= mid) {
37+
pos = arr[i];
38+
magnet += 1;
39+
if (magnet == m) {
40+
return true;
41+
}
42+
}
43+
}
44+
return false;
45+
}
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
1552\. Magnetic Force Between Two Balls
2+
3+
Medium
4+
5+
In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has `n` empty baskets, the <code>i<sup>th</sup></code> basket is at `position[i]`, Morty has `m` balls and needs to distribute the balls into the baskets such that the **minimum magnetic force** between any two balls is **maximum**.
6+
7+
Rick stated that magnetic force between two different balls at positions `x` and `y` is `|x - y|`.
8+
9+
Given the integer array `position` and the integer `m`. Return _the required force_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/08/11/q3v1.jpg)
14+
15+
**Input:** position = [1,2,3,4,7], m = 3
16+
17+
**Output:** 3
18+
19+
**Explanation:** Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
20+
21+
**Example 2:**
22+
23+
**Input:** position = [5,4,3,2,1,1000000000], m = 2
24+
25+
**Output:** 999999999
26+
27+
**Explanation:** We can use baskets 1 and 1000000000.
28+
29+
**Constraints:**
30+
31+
* `n == position.length`
32+
* <code>2 <= n <= 10<sup>5</sup></code>
33+
* <code>1 <= position[i] <= 10<sup>9</sup></code>
34+
* All integers in `position` are **distinct**.
35+
* `2 <= m <= position.length`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1501_1600.s1553_minimum_number_of_days_to_eat_n_oranges;
2+
3+
// #Hard #Dynamic_Programming #Memoization #2022_04_11_Time_5_ms_(91.90%)_Space_43.5_MB_(31.90%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
10+
public int minDays(int n) {
11+
return eat(n, new HashMap<>());
12+
}
13+
14+
private int eat(int n, Map<Integer, Integer> cache) {
15+
if (n <= 1) {
16+
return n;
17+
}
18+
Integer cached = cache.get(n);
19+
if (cached != null) {
20+
return cached;
21+
}
22+
int result = Math.min(n % 2 + eat(n / 2, cache), n % 3 + eat(n / 3, cache)) + 1;
23+
cache.put(n, result);
24+
return result;
25+
}
26+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
1553\. Minimum Number of Days to Eat N Oranges
2+
3+
Hard
4+
5+
There are `n` oranges in the kitchen and you decided to eat some of these oranges every day as follows:
6+
7+
* Eat one orange.
8+
* If the number of remaining oranges `n` is divisible by `2` then you can eat `n / 2` oranges.
9+
* If the number of remaining oranges `n` is divisible by `3` then you can eat `2 * (n / 3)` oranges.
10+
11+
You can only choose one of the actions per day.
12+
13+
Given the integer `n`, return _the minimum number of days to eat_ `n` _oranges_.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 10
18+
19+
**Output:** 4
20+
21+
**Explanation:** You have 10 oranges.
22+
23+
Day 1: Eat 1 orange, 10 - 1 = 9.
24+
25+
Day 2: Eat 6 oranges, 9 - 2\*(9/3) = 9 - 6 = 3. (Since 9 is divisible by 3)
26+
27+
Day 3: Eat 2 oranges, 3 - 2\*(3/3) = 3 - 2 = 1.
28+
29+
Day 4: Eat the last orange 1 - 1 = 0.
30+
31+
You need at least 4 days to eat the 10 oranges.
32+
33+
**Example 2:**
34+
35+
**Input:** n = 6
36+
37+
**Output:** 3
38+
39+
**Explanation:** You have 6 oranges.
40+
41+
Day 1: Eat 3 oranges, 6 - 6/2 = 6 - 3 = 3. (Since 6 is divisible by 2).
42+
43+
Day 2: Eat 2 oranges, 3 - 2\*(3/3) = 3 - 2 = 1. (Since 3 is divisible by 3)
44+
45+
Day 3: Eat the last orange 1 - 1 = 0.
46+
47+
You need at least 3 days to eat the 6 oranges.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= n <= 2 * 10<sup>9</sup></code>

0 commit comments

Comments
 (0)