Skip to content

Commit e62556f

Browse files
authored
Added tasks 2106, 2108, 2109, 2110, 2111.
1 parent 5d9f6f8 commit e62556f

File tree

15 files changed

+547
-0
lines changed

15 files changed

+547
-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.s2106_maximum_fruits_harvested_after_at_most_k_steps;
2+
3+
// #Hard #Array #Binary_Search #Prefix_Sum #Sliding_Window
4+
// #2022_05_31_Time_27_ms_(48.57%)_Space_128.7_MB_(44.76%)
5+
6+
public class Solution {
7+
public int maxTotalFruits(int[][] fruits, int startPos, int k) {
8+
int res = 0;
9+
for (int left = 0, right = 0, sum = 0; right < fruits.length; right++) {
10+
sum += fruits[right][1];
11+
while (left <= right && !isValidRange(fruits[left][0], fruits[right][0], startPos, k)) {
12+
sum -= fruits[left++][1];
13+
}
14+
res = Math.max(sum, res);
15+
}
16+
17+
return res;
18+
}
19+
20+
private boolean isValidRange(int leftPos, int rightPos, int startPos, int k) {
21+
if (rightPos <= startPos) {
22+
return startPos - leftPos <= k;
23+
} else if (leftPos >= startPos) {
24+
return rightPos - startPos <= k;
25+
} else {
26+
int left = startPos - leftPos;
27+
int right = rightPos - startPos;
28+
return left <= right ? left * 2 + right <= k : right * 2 + left <= k;
29+
}
30+
}
31+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2106\. Maximum Fruits Harvested After at Most K Steps
2+
3+
Hard
4+
5+
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array `fruits` where <code>fruits[i] = [position<sub>i</sub>, amount<sub>i</sub>]</code> depicts <code>amount<sub>i</sub></code> fruits at the position <code>position<sub>i</sub></code>. `fruits` is already **sorted** by <code>position<sub>i</sub></code> in **ascending order**, and each <code>position<sub>i</sub></code> is **unique**.
6+
7+
You are also given an integer `startPos` and an integer `k`. Initially, you are at the position `startPos`. From any position, you can either walk to the **left or right**. It takes **one step** to move **one unit** on the x-axis, and you can walk **at most** `k` steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.
8+
9+
Return _the **maximum total number** of fruits you can harvest_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/11/21/1.png)
14+
15+
**Input:** fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
16+
17+
**Output:** 9
18+
19+
**Explanation:** The optimal way is to:
20+
21+
- Move right to position 6 and harvest 3 fruits
22+
23+
- Move right to position 8 and harvest 6 fruits
24+
25+
You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/11/21/2.png)
30+
31+
**Input:** fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
32+
33+
**Output:** 14
34+
35+
**Explanation:** You can move at most k = 4 steps, so you cannot reach position 0 nor 10. The optimal way is to:
36+
37+
- Harvest the 7 fruits at the starting position 5
38+
39+
- Move left to position 4 and harvest 1 fruit
40+
41+
- Move right to position 6 and harvest 2 fruits
42+
43+
- Move right to position 7 and harvest 4 fruits
44+
45+
You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
46+
47+
**Example 3:**
48+
49+
![](https://assets.leetcode.com/uploads/2021/11/21/3.png)
50+
51+
**Input:** fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
52+
53+
**Output:** 0
54+
55+
**Explanation:** You can move at most k = 2 steps and cannot reach any position with fruits.
56+
57+
**Constraints:**
58+
59+
* <code>1 <= fruits.length <= 10<sup>5</sup></code>
60+
* `fruits[i].length == 2`
61+
* <code>0 <= startPos, position<sub>i</sub> <= 2 * 10<sup>5</sup></code>
62+
* <code>position<sub>i-1</sub> < position<sub>i</sub></code> for any `i > 0` (**0-indexed**)
63+
* <code>1 <= amount<sub>i</sub> <= 10<sup>4</sup></code>
64+
* <code>0 <= k <= 2 * 10<sup>5</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2101_2200.s2108_find_first_palindromic_string_in_the_array;
2+
3+
// #Easy #Array #String #Two_Pointers #2022_05_31_Time_3_ms_(84.92%)_Space_50.1_MB_(69.85%)
4+
5+
public class Solution {
6+
public static boolean isPalindrome(String s) {
7+
int len = s.length();
8+
for (int i = 0, j = len - 1; i <= len / 2 && j >= len / 2; i++, j--) {
9+
if (s.charAt(i) != s.charAt(j)) {
10+
return false;
11+
}
12+
}
13+
return true;
14+
}
15+
16+
public String firstPalindrome(String[] words) {
17+
for (String word : words) {
18+
if (isPalindrome(word)) {
19+
return word;
20+
}
21+
}
22+
return "";
23+
}
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2108\. Find First Palindromic String in the Array
2+
3+
Easy
4+
5+
Given an array of strings `words`, return _the first **palindromic** string in the array_. If there is no such string, return _an **empty string**_ `""`.
6+
7+
A string is **palindromic** if it reads the same forward and backward.
8+
9+
**Example 1:**
10+
11+
**Input:** words = ["abc","car","ada","racecar","cool"]
12+
13+
**Output:** "ada"
14+
15+
**Explanation:** The first string that is palindromic is "ada". Note that "racecar" is also palindromic, but it is not the first.
16+
17+
**Example 2:**
18+
19+
**Input:** words = ["notapalindrome","racecar"]
20+
21+
**Output:** "racecar"
22+
23+
**Explanation:** The first and only string that is palindromic is "racecar".
24+
25+
**Example 3:**
26+
27+
**Input:** words = ["def","ghi"]
28+
29+
**Output:** ""
30+
31+
**Explanation:** There are no palindromic strings, so the empty string is returned.
32+
33+
**Constraints:**
34+
35+
* `1 <= words.length <= 100`
36+
* `1 <= words[i].length <= 100`
37+
* `words[i]` consists only of lowercase English letters.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2101_2200.s2109_adding_spaces_to_a_string;
2+
3+
// #Medium #Array #String #Simulation #2022_05_31_Time_24_ms_(89.33%)_Space_94.5_MB_(48.03%)
4+
5+
public class Solution {
6+
public String addSpaces(String string, int[] spaces) {
7+
char[] stringChars = new char[string.length() + spaces.length];
8+
for (int i = 0; i < spaces.length; i++) {
9+
stringChars[spaces[i] + i] = ' ';
10+
}
11+
12+
int equivalentIndex = -1;
13+
int i = 0;
14+
while (i < string.length()) {
15+
equivalentIndex++;
16+
if (stringChars[equivalentIndex] == ' ') {
17+
i--;
18+
} else {
19+
stringChars[equivalentIndex] = string.charAt(i);
20+
}
21+
i++;
22+
}
23+
return new String(stringChars);
24+
}
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2109\. Adding Spaces to a String
2+
3+
Medium
4+
5+
You are given a **0-indexed** string `s` and a **0-indexed** integer array `spaces` that describes the indices in the original string where spaces will be added. Each space should be inserted **before** the character at the given index.
6+
7+
* For example, given `s = "EnjoyYourCoffee"` and `spaces = [5, 9]`, we place spaces before `'Y'` and `'C'`, which are at indices `5` and `9` respectively. Thus, we obtain `"Enjoy **Y**our **C**offee"`.
8+
9+
Return _the modified string **after** the spaces have been added._
10+
11+
**Example 1:**
12+
13+
**Input:** s = "LeetcodeHelpsMeLearn", spaces = [8,13,15]
14+
15+
**Output:** "Leetcode Helps Me Learn"
16+
17+
**Explanation:**
18+
19+
The indices 8, 13, and 15 correspond to the underlined characters in "Leetcode**H**elps**M**e**L**earn".
20+
21+
We then place spaces before those characters.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "icodeinpython", spaces = [1,5,7,9]
26+
27+
**Output:** "i code in py thon"
28+
29+
**Explanation:**
30+
31+
The indices 1, 5, 7, and 9 correspond to the underlined characters in "i**c**ode**i**n**p**y**t**hon".
32+
33+
We then place spaces before those characters.
34+
35+
**Example 3:**
36+
37+
**Input:** s = "spacing", spaces = [0,1,2,3,4,5,6]
38+
39+
**Output:** " s p a c i n g"
40+
41+
**Explanation:** We are also able to place spaces before the first character of the string.
42+
43+
**Constraints:**
44+
45+
* <code>1 <= s.length <= 3 * 10<sup>5</sup></code>
46+
* `s` consists only of lowercase and uppercase English letters.
47+
* <code>1 <= spaces.length <= 3 * 10<sup>5</sup></code>
48+
* `0 <= spaces[i] <= s.length - 1`
49+
* All the values of `spaces` are **strictly increasing**.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2101_2200.s2110_number_of_smooth_descent_periods_of_a_stock;
2+
3+
// #Medium #Array #Dynamic_Programming #Math #2022_05_31_Time_3_ms_(77.27%)_Space_95.2_MB_(34.45%)
4+
5+
public class Solution {
6+
public long getDescentPeriods(int[] prices) {
7+
long descendantCount = 0;
8+
int previousCounts = 0;
9+
for (int i = 0; i < prices.length - 1; i++) {
10+
if (prices[i] - prices[i + 1] == 1) {
11+
descendantCount++;
12+
if (previousCounts > 0) {
13+
descendantCount += previousCounts;
14+
}
15+
previousCounts++;
16+
} else {
17+
previousCounts = 0;
18+
}
19+
}
20+
descendantCount += prices.length;
21+
return descendantCount;
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2110\. Number of Smooth Descent Periods of a Stock
2+
3+
Medium
4+
5+
You are given an integer array `prices` representing the daily price history of a stock, where `prices[i]` is the stock price on the <code>i<sup>th</sup></code> day.
6+
7+
A **smooth descent period** of a stock consists of **one or more contiguous** days such that the price on each day is **lower** than the price on the **preceding day** by **exactly** `1`. The first day of the period is exempted from this rule.
8+
9+
Return _the number of **smooth descent periods**_.
10+
11+
**Example 1:**
12+
13+
**Input:** prices = [3,2,1,4]
14+
15+
**Output:** 7
16+
17+
**Explanation:** There are 7 smooth descent periods:
18+
19+
[3], [2], [1], [4], [3,2], [2,1], and [3,2,1]
20+
21+
Note that a period with one day is a smooth descent period by the definition.
22+
23+
**Example 2:**
24+
25+
**Input:** prices = [8,6,7,7]
26+
27+
**Output:** 4
28+
29+
**Explanation:** There are 4 smooth descent periods: [8], [6], [7], and [7]
30+
31+
Note that [8,6] is not a smooth descent period as 8 - 6 ≠ 1.
32+
33+
**Example 3:**
34+
35+
**Input:** prices = [1]
36+
37+
**Output:** 1
38+
39+
**Explanation:** There is 1 smooth descent period: [1]
40+
41+
**Constraints:**
42+
43+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
44+
* <code>1 <= prices[i] <= 10<sup>5</sup></code>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g2101_2200.s2111_minimum_operations_to_make_the_array_k_increasing;
2+
3+
// #Hard #Array #Binary_Search #2022_05_31_Time_97_ms_(22.90%)_Space_123.6_MB_(54.96%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public int kIncreasing(int[] a, int k) {
10+
int n = a.length;
11+
int res = 0;
12+
for (int s = 0; s < k; s++) {
13+
List<Integer> dp = new ArrayList<>();
14+
for (int i = s; i < n; i += k) {
15+
if (!bsearch(dp, a[i])) {
16+
dp.add(a[i]);
17+
}
18+
}
19+
res += dp.size();
20+
}
21+
return n - res;
22+
}
23+
24+
private boolean bsearch(List<Integer> dp, int target) {
25+
if (dp.isEmpty()) {
26+
return false;
27+
}
28+
int lo = 0;
29+
int hi = dp.size() - 1;
30+
while (lo < hi) {
31+
int mid = lo + (hi - lo) / 2;
32+
if (dp.get(mid) <= target) {
33+
lo = mid + 1;
34+
} else {
35+
hi = mid;
36+
}
37+
}
38+
39+
if (dp.get(lo) > target) {
40+
dp.set(lo, target);
41+
return true;
42+
}
43+
return false;
44+
}
45+
}

0 commit comments

Comments
 (0)