Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g1701_1800.s1748_sum_of_unique_elements;

// #Easy #Array #Hash_Table #Counting #2022_04_30_Time_2_ms_(54.08%)_Space_42_MB_(38.95%)

import java.util.HashMap;
import java.util.Map;

public class Solution {
public int sumOfUnique(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
int sum = 0;
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
sum += entry.getKey();
}
}
return sum;
}
}
36 changes: 36 additions & 0 deletions src/main/java/g1701_1800/s1748_sum_of_unique_elements/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
1748\. Sum of Unique Elements

Easy

You are given an integer array `nums`. The unique elements of an array are the elements that appear **exactly once** in the array.

Return _the **sum** of all the unique elements of_ `nums`.

**Example 1:**

**Input:** nums = [1,2,3,2]

**Output:** 4

**Explanation:** The unique elements are [1,3], and the sum is 4.

**Example 2:**

**Input:** nums = [1,1,1,1,1]

**Output:** 0

**Explanation:** There are no unique elements, and the sum is 0.

**Example 3:**

**Input:** nums = [1,2,3,4,5]

**Output:** 15

**Explanation:** The unique elements are [1,2,3,4,5], and the sum is 15.

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g1701_1800.s1749_maximum_absolute_sum_of_any_subarray;

// #Medium #Array #Dynamic_Programming #2022_04_30_Time_3_ms_(90.60%)_Space_65.8_MB_(11.86%)

public class Solution {
public int maxAbsoluteSum(int[] nums) {
int min = 0;
int max = 0;
int s = 0;
for (int num : nums) {
s += num;
min = Math.min(min, s);
max = Math.max(max, s);
}
return max - min;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
1749\. Maximum Absolute Sum of Any Subarray

Medium

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>.

Return _the **maximum** absolute sum of any **(possibly empty)** subarray of_ `nums`.

Note that `abs(x)` is defined as follows:

* If `x` is a negative integer, then `abs(x) = -x`.
* If `x` is a non-negative integer, then `abs(x) = x`.

**Example 1:**

**Input:** nums = [1,-3,2,3,-4]

**Output:** 5

**Explanation:** The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5.

**Example 2:**

**Input:** nums = [2,-5,1,-4,3,-2]

**Output:** 8

**Explanation:** The subarray [-5,1,-4] has absolute sum = abs(-5+1-4) = abs(-8) = 8.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g1701_1800.s1750_minimum_length_of_string_after_deleting_similar_ends;

// #Medium #String #Two_Pointers #2022_04_30_Time_5_ms_(68.68%)_Space_53.9_MB_(15.47%)

public class Solution {
public int minimumLength(String s) {
int i = 0;
int j = s.length() - 1;
if (s.charAt(i) == s.charAt(j)) {
while (i < j && s.charAt(i) == s.charAt(j)) {
char c = s.charAt(i);
i++;
while (c == s.charAt(i) && i < j) {
i++;
}
j--;
while (c == s.charAt(j) && i < j) {
j--;
}
}
}
return i <= j ? s.substring(i, j).length() + 1 : 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
1750\. Minimum Length of String After Deleting Similar Ends

Medium

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:

1. Pick a **non-empty** prefix from the string `s` where all the characters in the prefix are equal.
2. Pick a **non-empty** suffix from the string `s` where all the characters in this suffix are equal.
3. The prefix and the suffix should not intersect at any index.
4. The characters from the prefix and suffix must be the same.
5. Delete both the prefix and the suffix.

Return _the **minimum length** of_ `s` _after performing the above operation any number of times (possibly zero times)_.

**Example 1:**

**Input:** s = "ca"

**Output:** 2

**Explanation:** You can't remove any characters, so the string stays as is.

**Example 2:**

**Input:** s = "cabaabac"

**Output:** 0

**Explanation:** An optimal sequence of operations is:

- Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".

- Take prefix = "a" and suffix = "a" and remove them, s = "baab".

- Take prefix = "b" and suffix = "b" and remove them, s = "aa".

- Take prefix = "a" and suffix = "a" and remove them, s = "".

**Example 3:**

**Input:** s = "aabccabba"

**Output:** 3

**Explanation:** An optimal sequence of operations is:

- Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".

- Take prefix = "b" and suffix = "bb" and remove them, s = "cca".

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` only consists of characters `'a'`, `'b'`, and `'c'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package g1701_1800.s1751_maximum_number_of_events_that_can_be_attended_ii;

// #Hard #Array #Dynamic_Programming #Binary_Search
// #2022_04_30_Time_12_ms_(98.33%)_Space_84.7_MB_(92.22%)

import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;

public class Solution {
public int maxValue(int[][] events, int k) {
if (k == 1) {
Optional<int[]> value = Arrays.stream(events).max(Comparator.comparingInt(e -> e[2]));
if (value.isPresent()) {
return value.get()[2];
} else {
throw new NullPointerException();
}
}

int n = events.length;
Arrays.sort(events, Comparator.comparingInt(a -> a[0]));

int[][] memo = new int[n][k + 1];

return dfs(events, 0, k, memo);
}

private int dfs(int[][] events, int i, int k, int[][] memo) {
if (k == 0 || i >= events.length) {
return 0;
}
if (memo[i][k] > 0) {
return memo[i][k];
}

int idx = binarySearch(events, events[i][1] + 1, i + 1);
int use = events[i][2] + dfs(events, idx, k - 1, memo);

int notUse = dfs(events, i + 1, k, memo);

int res = Math.max(use, notUse);
memo[i][k] = res;

return res;
}

private int binarySearch(int[][] events, int i, int st) {

if (st >= events.length) {
return st;
}

int end = events.length - 1;
while (st < end) {
int mid = st + (end - st) / 2;
if (events[mid][0] < i) {
st = mid + 1;
} else {
end = mid;
}
}

return events[st][0] >= i ? st : st + 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
1751\. Maximum Number of Events That Can Be Attended II

Hard

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.

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.

Return _the **maximum sum** of values that you can receive by attending events._

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60048-pm.png)

**Input:** events = [[1,2,4],[3,4,3],[2,3,1]], k = 2

**Output:** 7

**Explanation:** Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60150-pm.png)

**Input:** events = [[1,2,4],[3,4,3],[2,3,10]], k = 2

**Output:** 10

**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.

**Example 3:**

**![](https://assets.leetcode.com/uploads/2021/01/10/screenshot-2021-01-11-at-60703-pm.png)**

**Input:** events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3

**Output:** 9

**Explanation:** Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.

**Constraints:**

* `1 <= k <= events.length`
* <code>1 <= k * events.length <= 10<sup>6</sup></code>
* <code>1 <= startDay<sub>i</sub> <= endDay<sub>i</sub> <= 10<sup>9</sup></code>
* <code>1 <= value<sub>i</sub> <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g1701_1800.s1752_check_if_array_is_sorted_and_rotated;

// #Easy #Array #2022_04_30_Time_3_ms_(5.38%)_Space_42.8_MB_(6.77%)

import java.util.Arrays;

public class Solution {
public boolean check(int[] nums) {
int[] copy = Arrays.copyOf(nums, nums.length);
Arrays.sort(copy);
for (int i = 1; i <= nums.length; i++) {
int[] rotated = rotate(nums, i);
if (Arrays.equals(rotated, copy)) {
return true;
}
}
return false;
}

private int[] rotate(int[] nums, int start) {
int[] rotated = new int[nums.length];
int j = 0;
for (int i = start; i < nums.length; i++, j++) {
rotated[j] = nums[i];
}
for (int i = 0; i < start; i++) {
rotated[j++] = nums[i];
}
return rotated;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
1752\. Check if Array Is Sorted and Rotated

Easy

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`.

There may be **duplicates** in the original array.

**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.

**Example 1:**

**Input:** nums = [3,4,5,1,2]

**Output:** true

**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].

**Example 2:**

**Input:** nums = [2,1,3,4]

**Output:** false

**Explanation:** There is no sorted array once rotated that can make nums.

**Example 3:**

**Input:** nums = [1,2,3]

**Output:** true

**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.

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Loading