Skip to content

Commit eb3523a

Browse files
authored
Added tasks 1748, 1749, 1750, 1751, 1752.
1 parent c2234d2 commit eb3523a

File tree

15 files changed

+484
-0
lines changed

15 files changed

+484
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1701_1800.s1748_sum_of_unique_elements;
2+
3+
// #Easy #Array #Hash_Table #Counting #2022_04_30_Time_2_ms_(54.08%)_Space_42_MB_(38.95%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int sumOfUnique(int[] nums) {
10+
Map<Integer, Integer> map = new HashMap<>();
11+
int sum = 0;
12+
for (int num : nums) {
13+
map.put(num, map.getOrDefault(num, 0) + 1);
14+
}
15+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
16+
if (entry.getValue() == 1) {
17+
sum += entry.getKey();
18+
}
19+
}
20+
return sum;
21+
}
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1748\. Sum of Unique Elements
2+
3+
Easy
4+
5+
You are given an integer array `nums`. The unique elements of an array are the elements that appear **exactly once** in the array.
6+
7+
Return _the **sum** of all the unique elements of_ `nums`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3,2]
12+
13+
**Output:** 4
14+
15+
**Explanation:** The unique elements are [1,3], and the sum is 4.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,1,1,1,1]
20+
21+
**Output:** 0
22+
23+
**Explanation:** There are no unique elements, and the sum is 0.
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [1,2,3,4,5]
28+
29+
**Output:** 15
30+
31+
**Explanation:** The unique elements are [1,2,3,4,5], and the sum is 15.
32+
33+
**Constraints:**
34+
35+
* `1 <= nums.length <= 100`
36+
* `1 <= nums[i] <= 100`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g1701_1800.s1749_maximum_absolute_sum_of_any_subarray;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_04_30_Time_3_ms_(90.60%)_Space_65.8_MB_(11.86%)
4+
5+
public class Solution {
6+
public int maxAbsoluteSum(int[] nums) {
7+
int min = 0;
8+
int max = 0;
9+
int s = 0;
10+
for (int num : nums) {
11+
s += num;
12+
min = Math.min(min, s);
13+
max = Math.max(max, s);
14+
}
15+
return max - min;
16+
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
1749\. Maximum Absolute Sum of Any Subarray
2+
3+
Medium
4+
5+
You are given an integer array `nums`. The **absolute sum** of a subarray <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> is <code>abs(nums<sub>l</sub> + nums<sub>l+1</sub> + ... + nums<sub>r-1</sub> + nums<sub>r</sub>)</code>.
6+
7+
Return _the **maximum** absolute sum of any **(possibly empty)** subarray of_ `nums`.
8+
9+
Note that `abs(x)` is defined as follows:
10+
11+
* If `x` is a negative integer, then `abs(x) = -x`.
12+
* If `x` is a non-negative integer, then `abs(x) = x`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,-3,2,3,-4]
17+
18+
**Output:** 5
19+
20+
**Explanation:** The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [2,-5,1,-4,3,-2]
25+
26+
**Output:** 8
27+
28+
**Explanation:** The subarray [-5,1,-4] has absolute sum = abs(-5+1-4) = abs(-8) = 8.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
33+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g1701_1800.s1750_minimum_length_of_string_after_deleting_similar_ends;
2+
3+
// #Medium #String #Two_Pointers #2022_04_30_Time_5_ms_(68.68%)_Space_53.9_MB_(15.47%)
4+
5+
public class Solution {
6+
public int minimumLength(String s) {
7+
int i = 0;
8+
int j = s.length() - 1;
9+
if (s.charAt(i) == s.charAt(j)) {
10+
while (i < j && s.charAt(i) == s.charAt(j)) {
11+
char c = s.charAt(i);
12+
i++;
13+
while (c == s.charAt(i) && i < j) {
14+
i++;
15+
}
16+
j--;
17+
while (c == s.charAt(j) && i < j) {
18+
j--;
19+
}
20+
}
21+
}
22+
return i <= j ? s.substring(i, j).length() + 1 : 0;
23+
}
24+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
1750\. Minimum Length of String After Deleting Similar Ends
2+
3+
Medium
4+
5+
Given a string `s` consisting only of characters `'a'`, `'b'`, and `'c'`. You are asked to apply the following algorithm on the string any number of times:
6+
7+
1. Pick a **non-empty** prefix from the string `s` where all the characters in the prefix are equal.
8+
2. Pick a **non-empty** suffix from the string `s` where all the characters in this suffix are equal.
9+
3. The prefix and the suffix should not intersect at any index.
10+
4. The characters from the prefix and suffix must be the same.
11+
5. Delete both the prefix and the suffix.
12+
13+
Return _the **minimum length** of_ `s` _after performing the above operation any number of times (possibly zero times)_.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "ca"
18+
19+
**Output:** 2
20+
21+
**Explanation:** You can't remove any characters, so the string stays as is.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "cabaabac"
26+
27+
**Output:** 0
28+
29+
**Explanation:** An optimal sequence of operations is:
30+
31+
- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
32+
33+
- Take prefix = "a" and suffix = "a" and remove them, s = "baab".
34+
35+
- Take prefix = "b" and suffix = "b" and remove them, s = "aa".
36+
37+
- Take prefix = "a" and suffix = "a" and remove them, s = "".
38+
39+
**Example 3:**
40+
41+
**Input:** s = "aabccabba"
42+
43+
**Output:** 3
44+
45+
**Explanation:** An optimal sequence of operations is:
46+
47+
- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
48+
49+
- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
50+
51+
**Constraints:**
52+
53+
* <code>1 <= s.length <= 10<sup>5</sup></code>
54+
* `s` only consists of characters `'a'`, `'b'`, and `'c'`.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g1701_1800.s1751_maximum_number_of_events_that_can_be_attended_ii;
2+
3+
// #Hard #Array #Dynamic_Programming #Binary_Search
4+
// #2022_04_30_Time_12_ms_(98.33%)_Space_84.7_MB_(92.22%)
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.Optional;
9+
10+
public class Solution {
11+
public int maxValue(int[][] events, int k) {
12+
if (k == 1) {
13+
Optional<int[]> value = Arrays.stream(events).max(Comparator.comparingInt(e -> e[2]));
14+
if (value.isPresent()) {
15+
return value.get()[2];
16+
} else {
17+
throw new NullPointerException();
18+
}
19+
}
20+
21+
int n = events.length;
22+
Arrays.sort(events, Comparator.comparingInt(a -> a[0]));
23+
24+
int[][] memo = new int[n][k + 1];
25+
26+
return dfs(events, 0, k, memo);
27+
}
28+
29+
private int dfs(int[][] events, int i, int k, int[][] memo) {
30+
if (k == 0 || i >= events.length) {
31+
return 0;
32+
}
33+
if (memo[i][k] > 0) {
34+
return memo[i][k];
35+
}
36+
37+
int idx = binarySearch(events, events[i][1] + 1, i + 1);
38+
int use = events[i][2] + dfs(events, idx, k - 1, memo);
39+
40+
int notUse = dfs(events, i + 1, k, memo);
41+
42+
int res = Math.max(use, notUse);
43+
memo[i][k] = res;
44+
45+
return res;
46+
}
47+
48+
private int binarySearch(int[][] events, int i, int st) {
49+
50+
if (st >= events.length) {
51+
return st;
52+
}
53+
54+
int end = events.length - 1;
55+
while (st < end) {
56+
int mid = st + (end - st) / 2;
57+
if (events[mid][0] < i) {
58+
st = mid + 1;
59+
} else {
60+
end = mid;
61+
}
62+
}
63+
64+
return events[st][0] >= i ? st : st + 1;
65+
}
66+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
1751\. Maximum Number of Events That Can Be Attended II
2+
3+
Hard
4+
5+
You are given an array of `events` where <code>events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>, value<sub>i</sub>]</code>. The <code>i<sup>th</sup></code> event starts at <code>startDay<sub>i</sub></code> and ends at <code>endDay<sub>i</sub></code>, and if you attend this event, you will receive a value of <code>value<sub>i</sub></code>. You are also given an integer `k` which represents the maximum number of events you can attend.
6+
7+
You can only attend one event at a time. If you choose to attend an event, you must attend the **entire** event. Note that the end day is **inclusive**: that is, you cannot attend two events where one of them starts and the other ends on the same day.
8+
9+
Return _the **maximum sum** of values that you can receive by attending events._
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60048-pm.png)
14+
15+
**Input:** events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
16+
17+
**Output:** 7
18+
19+
**Explanation:** Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60150-pm.png)
24+
25+
**Input:** events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
26+
27+
**Output:** 10
28+
29+
**Explanation:** Choose event 2 for a total value of 10. Notice that you cannot attend any other event as they overlap, and that you do **not** have to attend k events.
30+
31+
**Example 3:**
32+
33+
**![](https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60703-pm.png)**
34+
35+
**Input:** events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
36+
37+
**Output:** 9
38+
39+
**Explanation:** Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.
40+
41+
**Constraints:**
42+
43+
* `1 <= k <= events.length`
44+
* <code>1 <= k * events.length <= 10<sup>6</sup></code>
45+
* <code>1 <= startDay<sub>i</sub> <= endDay<sub>i</sub> <= 10<sup>9</sup></code>
46+
* <code>1 <= value<sub>i</sub> <= 10<sup>6</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1701_1800.s1752_check_if_array_is_sorted_and_rotated;
2+
3+
// #Easy #Array #2022_04_30_Time_3_ms_(5.38%)_Space_42.8_MB_(6.77%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public boolean check(int[] nums) {
9+
int[] copy = Arrays.copyOf(nums, nums.length);
10+
Arrays.sort(copy);
11+
for (int i = 1; i <= nums.length; i++) {
12+
int[] rotated = rotate(nums, i);
13+
if (Arrays.equals(rotated, copy)) {
14+
return true;
15+
}
16+
}
17+
return false;
18+
}
19+
20+
private int[] rotate(int[] nums, int start) {
21+
int[] rotated = new int[nums.length];
22+
int j = 0;
23+
for (int i = start; i < nums.length; i++, j++) {
24+
rotated[j] = nums[i];
25+
}
26+
for (int i = 0; i < start; i++) {
27+
rotated[j++] = nums[i];
28+
}
29+
return rotated;
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1752\. Check if Array Is Sorted and Rotated
2+
3+
Easy
4+
5+
Given an array `nums`, return `true` _if the array was originally sorted in non-decreasing order, then rotated **some** number of positions (including zero)_. Otherwise, return `false`.
6+
7+
There may be **duplicates** in the original array.
8+
9+
**Note:** An array `A` rotated by `x` positions results in an array `B` of the same length such that `A[i] == B[(i+x) % A.length]`, where `%` is the modulo operation.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,4,5,1,2]
14+
15+
**Output:** true
16+
17+
**Explanation:** [1,2,3,4,5] is the original sorted array. You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [2,1,3,4]
22+
23+
**Output:** false
24+
25+
**Explanation:** There is no sorted array once rotated that can make nums.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [1,2,3]
30+
31+
**Output:** true
32+
33+
**Explanation:** [1,2,3] is the original sorted array. You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 100`
38+
* `1 <= nums[i] <= 100`

0 commit comments

Comments
 (0)