Skip to content

Commit dc76c6d

Browse files
authored
Added tasks 2006, 2007, 2008, 2009.
1 parent 5edd05c commit dc76c6d

File tree

12 files changed

+438
-0
lines changed

12 files changed

+438
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2001_2100.s2006_count_number_of_pairs_with_absolute_difference_k;
2+
3+
// #Easy #Array #Hash_Table #Counting #2022_05_23_Time_8_ms_(58.60%)_Space_42.2_MB_(74.50%)
4+
5+
public class Solution {
6+
public int countKDifference(int[] nums, int k) {
7+
int pairs = 0;
8+
for (int i = 0; i < nums.length - 1; i++) {
9+
for (int j = i + 1; j < nums.length; j++) {
10+
if (Math.abs(nums[i] - nums[j]) == k) {
11+
pairs++;
12+
}
13+
}
14+
}
15+
return pairs;
16+
}
17+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2006\. Count Number of Pairs With Absolute Difference K
2+
3+
Easy
4+
5+
Given an integer array `nums` and an integer `k`, return _the number of pairs_ `(i, j)` _where_ `i < j` _such that_ `|nums[i] - nums[j]| == k`.
6+
7+
The value of `|x|` is defined as:
8+
9+
* `x` if `x >= 0`.
10+
* `-x` if `x < 0`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,2,2,1], k = 1
15+
16+
**Output:** 4
17+
18+
**Explanation:** The pairs with an absolute difference of 1 are:
19+
20+
- [**1**,**2**,2,1]
21+
22+
- [**1**,2,**2**,1]
23+
24+
- [1,**2**,2,**1**]
25+
26+
- [1,2,**2**,**1**]
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [1,3], k = 3
31+
32+
**Output:** 0
33+
34+
**Explanation:** There are no pairs with an absolute difference of 3.
35+
36+
**Example 3:**
37+
38+
**Input:** nums = [3,2,1,5,4], k = 2
39+
40+
**Output:** 3
41+
42+
**Explanation:** The pairs with an absolute difference of 2 are:
43+
44+
- [**3**,2,**1**,5,4]
45+
46+
- [**3**,2,1,**5**,4]
47+
48+
- [3,**2**,1,5,**4**]
49+
50+
**Constraints:**
51+
52+
* `1 <= nums.length <= 200`
53+
* `1 <= nums[i] <= 100`
54+
* `1 <= k <= 99`
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g2001_2100.s2007_find_original_array_from_doubled_array;
2+
3+
// #Medium #Array #Hash_Table #Sorting #Greedy
4+
// #2022_05_23_Time_41_ms_(96.48%)_Space_134.5_MB_(42.43%)
5+
6+
public class Solution {
7+
public int[] findOriginalArray(int[] changed) {
8+
if (changed.length % 2 == 1) {
9+
return new int[0];
10+
}
11+
int[] a = new int[100001];
12+
for (int j : changed) {
13+
a[j]++;
14+
}
15+
if (a[0] % 2 == 1) {
16+
return new int[0];
17+
}
18+
int[] ans = new int[changed.length / 2];
19+
int p = 0;
20+
if (a[0] > 0) {
21+
a[0] /= 2;
22+
while (a[0] > 0) {
23+
ans[p++] = 0;
24+
a[0]--;
25+
}
26+
}
27+
for (int i = 1; i <= 100001 / 2; i++) {
28+
if (a[i] == 0) {
29+
continue;
30+
}
31+
int tmp = i * 2;
32+
if (a[tmp] >= a[i]) {
33+
a[tmp] = a[tmp] - a[i];
34+
while (a[i] > 0) {
35+
ans[p++] = i;
36+
a[i]--;
37+
}
38+
} else {
39+
40+
return new int[0];
41+
}
42+
}
43+
for (int i = 1; i < a.length; i++) {
44+
if (a[i] != 0) {
45+
return new int[0];
46+
}
47+
}
48+
49+
return ans;
50+
}
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2007\. Find Original Array From Doubled Array
2+
3+
Medium
4+
5+
An integer array `original` is transformed into a **doubled** array `changed` by appending **twice the value** of every element in `original`, and then randomly **shuffling** the resulting array.
6+
7+
Given an array `changed`, return `original` _if_ `changed` _is a **doubled** array. If_ `changed` _is not a **doubled** array, return an empty array. The elements in_ `original` _may be returned in **any** order_.
8+
9+
**Example 1:**
10+
11+
**Input:** changed = [1,3,4,2,6,8]
12+
13+
**Output:** [1,3,4]
14+
15+
**Explanation:** One possible original array could be [1,3,4]:
16+
17+
- Twice the value of 1 is 1 \* 2 = 2.
18+
19+
- Twice the value of 3 is 3 \* 2 = 6.
20+
21+
- Twice the value of 4 is 4 \* 2 = 8.
22+
23+
Other original arrays could be [4,3,1] or [3,1,4].
24+
25+
**Example 2:**
26+
27+
**Input:** changed = [6,3,0,1]
28+
29+
**Output:** []
30+
31+
**Explanation:** changed is not a doubled array.
32+
33+
**Example 3:**
34+
35+
**Input:** changed = [1]
36+
37+
**Output:** []
38+
39+
**Explanation:** changed is not a doubled array.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= changed.length <= 10<sup>5</sup></code>
44+
* <code>0 <= changed[i] <= 10<sup>5</sup></code>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g2001_2100.s2008_maximum_earnings_from_taxi;
2+
3+
// #Medium #Array #Dynamic_Programming #Sorting #Binary_Search
4+
// #2022_05_23_Time_116_ms_(55.59%)_Space_135.7_MB_(5.00%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class Solution {
13+
public long maxTaxiEarnings(int n, int[][] rides) {
14+
if (rides.length == 1) {
15+
return calculateEarnings(rides[0]);
16+
}
17+
18+
Map<Integer, List<int[]>> map = new HashMap<>();
19+
for (int[] ride : rides) {
20+
map.compute(ride[1], (k, v) -> (v == null) ? new ArrayList<>() : v).add(ride);
21+
}
22+
23+
long[] maximisedEarnings = new long[n + 1];
24+
Arrays.fill(maximisedEarnings, 0);
25+
for (int i = 1; i < maximisedEarnings.length; i++) {
26+
maximisedEarnings[i] = maximisedEarnings[i - 1];
27+
List<int[]> passengers = map.get(i);
28+
if (passengers != null) {
29+
for (int[] passenger : passengers) {
30+
final int earning = calculateEarnings(passenger);
31+
maximisedEarnings[i] =
32+
Math.max(
33+
maximisedEarnings[i],
34+
maximisedEarnings[passenger[0]] + earning);
35+
}
36+
}
37+
}
38+
return maximisedEarnings[n];
39+
}
40+
41+
private int calculateEarnings(final int[] currentRide) {
42+
return currentRide[1] - currentRide[0] + currentRide[2];
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2008\. Maximum Earnings From Taxi
2+
3+
Medium
4+
5+
There are `n` points on a road you are driving your taxi on. The `n` points on the road are labeled from `1` to `n` in the direction you are going, and you want to drive from point `1` to point `n` to make money by picking up passengers. You cannot change the direction of the taxi.
6+
7+
The passengers are represented by a **0-indexed** 2D integer array `rides`, where <code>rides[i] = [start<sub>i</sub>, end<sub>i</sub>, tip<sub>i</sub>]</code> denotes the <code>i<sup>th</sup></code> passenger requesting a ride from point <code>start<sub>i</sub></code> to point <code>end<sub>i</sub></code> who is willing to give a <code>tip<sub>i</sub></code> dollar tip.
8+
9+
For **each** passenger `i` you pick up, you **earn** <code>end<sub>i</sub> - start<sub>i</sub> + tip<sub>i</sub></code> dollars. You may only drive **at most one** passenger at a time.
10+
11+
Given `n` and `rides`, return _the **maximum** number of dollars you can earn by picking up the passengers optimally._
12+
13+
**Note:** You may drop off a passenger and pick up a different passenger at the same point.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 5, rides = [[2,5,4],[1,5,1]]
18+
19+
**Output:** 7
20+
21+
**Explanation:** We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.
22+
23+
**Example 2:**
24+
25+
**Input:** n = 20, rides = [[1,6,1],[3,10,2],[10,12,3],[11,12,2],[12,15,2],[13,18,1]]
26+
27+
**Output:** 20
28+
29+
**Explanation:** We will pick up the following passengers:
30+
31+
- Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars.
32+
33+
- Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars.
34+
35+
- Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars.
36+
37+
We earn 9 + 5 + 6 = 20 dollars in total.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= n <= 10<sup>5</sup></code>
42+
* <code>1 <= rides.length <= 3 * 10<sup>4</sup></code>
43+
* `rides[i].length == 3`
44+
* <code>1 <= start<sub>i</sub> < end<sub>i</sub> <= n</code>
45+
* <code>1 <= tip<sub>i</sub> <= 10<sup>5</sup></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2001_2100.s2009_minimum_number_of_operations_to_make_array_continuous;
2+
3+
// #Hard #Array #Binary_Search #2022_05_24_Time_57_ms_(72.43%)_Space_78.2_MB_(65.41%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minOperations(int[] nums) {
9+
Arrays.sort(nums);
10+
int n = nums.length;
11+
int duplicates = 0;
12+
int maxContinuous = 0;
13+
int start = 0;
14+
int end = 0;
15+
while (end < n) {
16+
if (end > 0 && nums[end] == nums[end - 1]) {
17+
duplicates++;
18+
}
19+
while (nums[start] + n <= nums[end]) {
20+
start++;
21+
if (nums[start] == nums[start - 1]) {
22+
duplicates--;
23+
}
24+
}
25+
maxContinuous = Math.max(maxContinuous, end - start + 1 - duplicates);
26+
end++;
27+
}
28+
return n - maxContinuous;
29+
}
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2009\. Minimum Number of Operations to Make Array Continuous
2+
3+
Hard
4+
5+
You are given an integer array `nums`. In one operation, you can replace **any** element in `nums` with **any** integer.
6+
7+
`nums` is considered **continuous** if both of the following conditions are fulfilled:
8+
9+
* All elements in `nums` are **unique**.
10+
* The difference between the **maximum** element and the **minimum** element in `nums` equals `nums.length - 1`.
11+
12+
For example, `nums = [4, 2, 5, 3]` is **continuous**, but `nums = [1, 2, 3, 5, 6]` is **not continuous**.
13+
14+
Return _the **minimum** number of operations to make_ `nums` **_continuous_**.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [4,2,5,3]
19+
20+
**Output:** 0
21+
22+
**Explanation:** nums is already continuous.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,2,3,5,6]
27+
28+
**Output:** 1
29+
30+
**Explanation:** One possible solution is to change the last element to 4. The resulting array is [1,2,3,5,4], which is continuous.
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [1,10,100,1000]
35+
36+
**Output:** 3
37+
38+
**Explanation:** One possible solution is to:
39+
40+
- Change the second element to 2.
41+
42+
- Change the third element to 3.
43+
44+
- Change the fourth element to 4.
45+
46+
The resulting array is [1,2,3,4], which is continuous.
47+
48+
**Constraints:**
49+
50+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
51+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2006_count_number_of_pairs_with_absolute_difference_k;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countKDifference() {
11+
assertThat(new Solution().countKDifference(new int[] {1, 2, 2, 1}, 1), equalTo(4));
12+
}
13+
14+
@Test
15+
void countKDifference2() {
16+
assertThat(new Solution().countKDifference(new int[] {1, 3}, 3), equalTo(0));
17+
}
18+
19+
@Test
20+
void countKDifference3() {
21+
assertThat(new Solution().countKDifference(new int[] {3, 2, 1, 5, 4}, 2), equalTo(3));
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2001_2100.s2007_find_original_array_from_doubled_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void findOriginalArray() {
11+
assertThat(
12+
new Solution().findOriginalArray(new int[] {1, 3, 4, 2, 6, 8}),
13+
equalTo(new int[] {1, 3, 4}));
14+
}
15+
16+
@Test
17+
void findOriginalArray2() {
18+
assertThat(new Solution().findOriginalArray(new int[] {6, 3, 0, 1}), equalTo(new int[] {}));
19+
}
20+
21+
@Test
22+
void findOriginalArray3() {
23+
assertThat(new Solution().findOriginalArray(new int[] {1}), equalTo(new int[] {}));
24+
}
25+
}

0 commit comments

Comments
 (0)