Skip to content

Commit 50276ba

Browse files
authored
Added tasks 1561, 1562, 1563, 1566, 1567.
1 parent 16f7054 commit 50276ba

File tree

15 files changed

+476
-0
lines changed

15 files changed

+476
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g1501_1600.s1561_maximum_number_of_coins_you_can_get;
2+
3+
// #Medium #Array #Math #Sorting #Greedy #Game_Theory
4+
// #2022_04_11_Time_34_ms_(69.29%)_Space_77_MB_(21.83%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int maxCoins(int[] piles) {
10+
Arrays.sort(piles);
11+
int j = 0;
12+
int coins = 0;
13+
for (int i = piles.length - 2; i > 0; i -= 2) {
14+
coins += piles[i];
15+
if (++j == piles.length / 3) {
16+
return coins;
17+
}
18+
}
19+
return coins;
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
1561\. Maximum Number of Coins You Can Get
2+
3+
Medium
4+
5+
There are `3n` piles of coins of varying size, you and your friends will take piles of coins as follows:
6+
7+
* In each step, you will choose **any** `3` piles of coins (not necessarily consecutive).
8+
* Of your choice, Alice will pick the pile with the maximum number of coins.
9+
* You will pick the next pile with the maximum number of coins.
10+
* Your friend Bob will pick the last pile.
11+
* Repeat until there are no more piles of coins.
12+
13+
Given an array of integers `piles` where `piles[i]` is the number of coins in the <code>i<sup>th</sup></code> pile.
14+
15+
Return the maximum number of coins that you can have.
16+
17+
**Example 1:**
18+
19+
**Input:** piles = [2,4,1,2,7,8]
20+
21+
**Output:** 9
22+
23+
**Explanation:** Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with **7** coins and Bob the last one.
24+
25+
Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with **2** coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9.
26+
27+
On the other hand if we choose this arrangement (1, **2**, 8), (2, **4**, 7) you only get 2 + 4 = 6 coins which is not optimal.
28+
29+
**Example 2:**
30+
31+
**Input:** piles = [2,4,5]
32+
33+
**Output:** 4
34+
35+
**Example 3:**
36+
37+
**Input:** piles = [9,8,7,6,5,1,2,3,4]
38+
39+
**Output:** 18
40+
41+
**Constraints:**
42+
43+
* <code>3 <= piles.length <= 10<sup>5</sup></code>
44+
* `piles.length % 3 == 0`
45+
* <code>1 <= piles[i] <= 10<sup>4</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1501_1600.s1562_find_latest_group_of_size_m;
2+
3+
// #Medium #Array #Binary_Search #Simulation #2022_04_11_Time_8_ms_(90.00%)_Space_84.2_MB_(53.00%)
4+
5+
public class Solution {
6+
public int findLatestStep(int[] arr, int m) {
7+
int[] lengthAtIndex = new int[arr.length + 2];
8+
int[] countOfLength = new int[arr.length + 1];
9+
int res = -1;
10+
int step = 1;
11+
for (int i : arr) {
12+
int leftLength = lengthAtIndex[i - 1];
13+
int rightLength = lengthAtIndex[i + 1];
14+
int newLength = leftLength + rightLength + 1;
15+
lengthAtIndex[i] = newLength;
16+
lengthAtIndex[i - leftLength] = newLength;
17+
lengthAtIndex[i + rightLength] = newLength;
18+
countOfLength[newLength] += 1;
19+
countOfLength[leftLength] -= 1;
20+
countOfLength[rightLength] -= 1;
21+
if (countOfLength[m] > 0) {
22+
res = step;
23+
}
24+
step++;
25+
}
26+
27+
return res;
28+
}
29+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
1562\. Find Latest Group of Size M
2+
3+
Medium
4+
5+
Given an array `arr` that represents a permutation of numbers from `1` to `n`.
6+
7+
You have a binary string of size `n` that initially has all its bits set to zero. At each step `i` (assuming both the binary string and `arr` are 1-indexed) from `1` to `n`, the bit at position `arr[i]` is set to `1`.
8+
9+
You are also given an integer `m`. Find the latest step at which there exists a group of ones of length `m`. A group of ones is a contiguous substring of `1`'s such that it cannot be extended in either direction.
10+
11+
Return _the latest step at which there exists a group of ones of length **exactly**_ `m`. _If no such group exists, return_ `-1`.
12+
13+
**Example 1:**
14+
15+
**Input:** arr = [3,5,1,2,4], m = 1
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
Step 1: "00100", groups: ["1"]
22+
23+
Step 2: "00101", groups: ["1", "1"]
24+
25+
Step 3: "10101", groups: ["1", "1", "1"]
26+
27+
Step 4: "11101", groups: ["111", "1"]
28+
29+
Step 5: "11111", groups: ["11111"]
30+
31+
The latest step at which there exists a group of size 1 is step 4.
32+
33+
**Example 2:**
34+
35+
**Input:** arr = [3,1,5,4,2], m = 2
36+
37+
**Output:** -1
38+
39+
**Explanation:**
40+
41+
Step 1: "00100", groups: ["1"]
42+
43+
Step 2: "10100", groups: ["1", "1"]
44+
45+
Step 3: "10101", groups: ["1", "1", "1"]
46+
47+
Step 4: "10111", groups: ["1", "111"]
48+
49+
Step 5: "11111", groups: ["11111"]
50+
51+
No group of size 2 exists during any step.
52+
53+
**Constraints:**
54+
55+
* `n == arr.length`
56+
* <code>1 <= m <= n <= 10<sup>5</sup></code>
57+
* `1 <= arr[i] <= n`
58+
* All integers in `arr` are **distinct**.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g1501_1600.s1563_stone_game_v;
2+
3+
// #Hard #Array #Dynamic_Programming #Math #Game_Theory
4+
// #2022_04_11_Time_36_ms_(95.56%)_Space_54.3_MB_(17.78%)
5+
6+
public class Solution {
7+
public int stoneGameV(int[] stoneValue) {
8+
int n = stoneValue.length;
9+
int[] ps = new int[n];
10+
ps[0] = stoneValue[0];
11+
for (int i = 1; i < n; i++) {
12+
ps[i] = ps[i - 1] + stoneValue[i];
13+
}
14+
return gameDP(ps, 0, n - 1, new Integer[n][n]);
15+
}
16+
17+
private int gameDP(int[] ps, int i, int j, Integer[][] dp) {
18+
if (i == j) {
19+
return 0;
20+
}
21+
if (dp[i][j] != null) {
22+
return dp[i][j];
23+
}
24+
int max = 0;
25+
for (int k = i + 1; k <= j; k++) {
26+
int l = ps[k - 1] - (i == 0 ? 0 : ps[i - 1]);
27+
int r = ps[j] - ps[k - 1];
28+
if (2 * Math.min(l, r) < max) {
29+
continue;
30+
}
31+
if (l <= r) {
32+
max = Math.max(max, l + gameDP(ps, i, k - 1, dp));
33+
}
34+
if (l >= r) {
35+
max = Math.max(max, r + gameDP(ps, k, j, dp));
36+
}
37+
}
38+
return dp[i][j] = max;
39+
}
40+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1563\. Stone Game V
2+
3+
Hard
4+
5+
There are several stones **arranged in a row**, and each stone has an associated value which is an integer given in the array `stoneValue`.
6+
7+
In each round of the game, Alice divides the row into **two non-empty rows** (i.e. left row and right row), then Bob calculates the value of each row which is the sum of the values of all the stones in this row. Bob throws away the row which has the maximum value, and Alice's score increases by the value of the remaining row. If the value of the two rows are equal, Bob lets Alice decide which row will be thrown away. The next round starts with the remaining row.
8+
9+
The game ends when there is only **one stone remaining**. Alice's is initially **zero**.
10+
11+
Return _the maximum score that Alice can obtain_.
12+
13+
**Example 1:**
14+
15+
**Input:** stoneValue = [6,2,3,4,5,5]
16+
17+
**Output:** 18
18+
19+
**Explanation:** In the first round, Alice divides the row to [6,2,3], [4,5,5]. The left row has the value 11 and the right row has value 14. Bob throws away the right row and Alice's score is now 11. In the second round Alice divides the row to [6], [2,3]. This time Bob throws away the left row and Alice's score becomes 16 (11 + 5).
20+
21+
The last round Alice has only one choice to divide the row which is [2], [3]. Bob throws away the right row and Alice's score is now 18 (16 + 2). The game ends because only one stone is remaining in the row.
22+
23+
**Example 2:**
24+
25+
**Input:** stoneValue = [7,7,7,7,7,7,7]
26+
27+
**Output:** 28
28+
29+
**Example 3:**
30+
31+
**Input:** stoneValue = [4]
32+
33+
**Output:** 0
34+
35+
**Constraints:**
36+
37+
* `1 <= stoneValue.length <= 500`
38+
* <code>1 <= stoneValue[i] <= 10<sup>6</sup></code>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1501_1600.s1566_detect_pattern_of_length_m_repeated_k_or_more_times;
2+
3+
// #Easy #Array #Enumeration #2022_04_11_Time_1_ms_(49.25%)_Space_41.7_MB_(66.67%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public boolean containsPattern(int[] arr, int m, int k) {
9+
for (int i = 0; i < arr.length - m; i++) {
10+
int[] pattern = Arrays.copyOfRange(arr, i, i + m);
11+
int times = 1;
12+
for (int j = i + m; j < arr.length; j += m) {
13+
int[] candidate = Arrays.copyOfRange(arr, j, j + m);
14+
if (Arrays.equals(pattern, candidate)) {
15+
times++;
16+
if (times == k) {
17+
return true;
18+
}
19+
} else {
20+
break;
21+
}
22+
}
23+
}
24+
return false;
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1566\. Detect Pattern of Length M Repeated K or More Times
2+
3+
Easy
4+
5+
Given an array of positive integers `arr`, find a pattern of length `m` that is repeated `k` or more times.
6+
7+
A **pattern** is a subarray (consecutive sub-sequence) that consists of one or more values, repeated multiple times **consecutively** without overlapping. A pattern is defined by its length and the number of repetitions.
8+
9+
Return `true` _if there exists a pattern of length_ `m` _that is repeated_ `k` _or more times, otherwise return_ `false`.
10+
11+
**Example 1:**
12+
13+
**Input:** arr = [1,2,4,4,4,4], m = 1, k = 3
14+
15+
**Output:** true
16+
17+
**Explanation:** The pattern **(4)** of length 1 is repeated 4 consecutive times. Notice that pattern can be repeated k or more times but not less.
18+
19+
**Example 2:**
20+
21+
**Input:** arr = [1,2,1,2,1,1,1,3], m = 2, k = 2
22+
23+
**Output:** true
24+
25+
**Explanation:** The pattern **(1,2)** of length 2 is repeated 2 consecutive times. Another valid pattern **(2,1) is** also repeated 2 times.
26+
27+
**Example 3:**
28+
29+
**Input:** arr = [1,2,1,2,1,3], m = 2, k = 3
30+
31+
**Output:** false
32+
33+
**Explanation:** The pattern (1,2) is of length 2 but is repeated only 2 times. There is no pattern of length 2 that is repeated 3 or more times.
34+
35+
**Constraints:**
36+
37+
* `2 <= arr.length <= 100`
38+
* `1 <= arr[i] <= 100`
39+
* `1 <= m <= 100`
40+
* `2 <= k <= 100`
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g1501_1600.s1567_maximum_length_of_subarray_with_positive_product;
2+
3+
// #Medium #Array #Dynamic_Programming #Greedy #Dynamic_Programming_I_Day_6
4+
// #2022_04_11_Time_4_ms_(80.86%)_Space_77.8_MB_(65.54%)
5+
6+
public class Solution {
7+
public int getMaxLen(int[] nums) {
8+
int posLen = 0;
9+
int negLen = 0;
10+
int res = 0;
11+
for (int num : nums) {
12+
if (num == 0) {
13+
posLen = 0;
14+
negLen = 0;
15+
} else if (num > 0) {
16+
posLen++;
17+
negLen = negLen == 0 ? 0 : negLen + 1;
18+
} else {
19+
int temp = posLen;
20+
posLen = negLen == 0 ? 0 : negLen + 1;
21+
negLen = temp + 1;
22+
}
23+
res = Math.max(res, posLen);
24+
}
25+
return res;
26+
}
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1567\. Maximum Length of Subarray With Positive Product
2+
3+
Medium
4+
5+
Given an array of integers `nums`, find the maximum length of a subarray where the product of all its elements is positive.
6+
7+
A subarray of an array is a consecutive sequence of zero or more values taken out of that array.
8+
9+
Return _the maximum length of a subarray with positive product_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,-2,-3,4]
14+
15+
**Output:** 4
16+
17+
**Explanation:** The array nums already has a positive product of 24.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [0,1,-2,-3,-4]
22+
23+
**Output:** 3
24+
25+
**Explanation:** The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [-1,-2,-3,0,1]
30+
31+
**Output:** 2
32+
33+
**Explanation:** The longest subarray with positive product is [-1,-2] or [-2,-3].
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)