Skip to content

Commit ef006a1

Browse files
committed
feat: solve No.1561,1685,2870
1 parent cad7a21 commit ef006a1

3 files changed

+262
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# 1561. Maximum Number of Coins You Can Get
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Math, Greedy, Sorting, Game Theory.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
There are `3n` piles of coins of varying size, you and your friends will take piles of coins as follows:
10+
11+
12+
13+
- In each step, you will choose **any **`3` piles of coins (not necessarily consecutive).
14+
15+
- Of your choice, Alice will pick the pile with the maximum number of coins.
16+
17+
- You will pick the next pile with the maximum number of coins.
18+
19+
- Your friend Bob will pick the last pile.
20+
21+
- Repeat until there are no more piles of coins.
22+
23+
24+
Given an array of integers `piles` where `piles[i]` is the number of coins in the `ith` pile.
25+
26+
Return the maximum number of coins that you can have.
27+
28+
 
29+
Example 1:
30+
31+
```
32+
Input: piles = [2,4,1,2,7,8]
33+
Output: 9
34+
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.
35+
Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one.
36+
The maximum number of coins which you can have are: 7 + 2 = 9.
37+
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.
38+
```
39+
40+
Example 2:
41+
42+
```
43+
Input: piles = [2,4,5]
44+
Output: 4
45+
```
46+
47+
Example 3:
48+
49+
```
50+
Input: piles = [9,8,7,6,5,1,2,3,4]
51+
Output: 18
52+
```
53+
54+
 
55+
**Constraints:**
56+
57+
58+
59+
- `3 <= piles.length <= 105`
60+
61+
- `piles.length % 3 == 0`
62+
63+
- `1 <= piles[i] <= 104`
64+
65+
66+
67+
## Solution
68+
69+
```javascript
70+
/**
71+
* @param {number[]} piles
72+
* @return {number}
73+
*/
74+
var maxCoins = function(piles) {
75+
piles.sort((a, b) => a - b);
76+
var res = 0;
77+
for (var i = 0; i < piles.length / 3; i++) {
78+
res += piles[piles.length - (i * 2) - 1 - 1];
79+
}
80+
return res;
81+
};
82+
```
83+
84+
**Explain:**
85+
86+
nope.
87+
88+
**Complexity:**
89+
90+
* Time complexity : O(n * log(n)).
91+
* Space complexity : O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 1685. Sum of Absolute Differences in a Sorted Array
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Math, Prefix Sum.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given an integer array `nums` sorted in **non-decreasing** order.
10+
11+
Build and return **an integer array **`result`** with the same length as **`nums`** such that **`result[i]`** is equal to the **summation of absolute differences** between **`nums[i]`** and all the other elements in the array.**
12+
13+
In other words, `result[i]` is equal to `sum(|nums[i]-nums[j]|)` where `0 <= j < nums.length` and `j != i` (**0-indexed**).
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: nums = [2,3,5]
20+
Output: [4,3,5]
21+
Explanation: Assuming the arrays are 0-indexed, then
22+
result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,
23+
result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,
24+
result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.
25+
```
26+
27+
Example 2:
28+
29+
```
30+
Input: nums = [1,4,6,8,10]
31+
Output: [24,15,13,15,21]
32+
```
33+
34+
 
35+
**Constraints:**
36+
37+
38+
39+
- `2 <= nums.length <= 105`
40+
41+
- `1 <= nums[i] <= nums[i + 1] <= 104`
42+
43+
44+
45+
## Solution
46+
47+
```javascript
48+
/**
49+
* @param {number[]} nums
50+
* @return {number[]}
51+
*/
52+
var getSumAbsoluteDifferences = function(nums) {
53+
var diffLeft = Array(nums.length);
54+
for (var i = 0; i < nums.length; i++) {
55+
if (i === 0) {
56+
diffLeft[i] = 0;
57+
} else {
58+
diffLeft[i] = diffLeft[i - 1] + (nums[i] - nums[i - 1]) * i;
59+
}
60+
}
61+
var diffRight = Array(nums.length);
62+
for (var j = nums.length - 1; j >= 0; j--) {
63+
if (j === nums.length - 1) {
64+
diffRight[j] = 0;
65+
} else {
66+
diffRight[j] = diffRight[j + 1] + (nums[j + 1] - nums[j]) * (nums.length - 1 - j);
67+
}
68+
}
69+
var diff = Array(nums.length);
70+
for (var k = 0; k < nums.length; k++) {
71+
diff[k] = diffLeft[k] + diffRight[k];
72+
}
73+
return diff;
74+
};
75+
```
76+
77+
**Explain:**
78+
79+
nope.
80+
81+
**Complexity:**
82+
83+
* Time complexity : O(n).
84+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 2870. Minimum Number of Operations to Make Array Empty
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Hash Table, Greedy, Counting.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given a **0-indexed** array `nums` consisting of positive integers.
10+
11+
There are two types of operations that you can apply on the array **any** number of times:
12+
13+
14+
15+
- Choose **two** elements with **equal** values and **delete** them from the array.
16+
17+
- Choose **three** elements with **equal** values and **delete** them from the array.
18+
19+
20+
Return **the **minimum** number of operations required to make the array empty, or **`-1`** if it is not possible**.
21+
22+
 
23+
Example 1:
24+
25+
```
26+
Input: nums = [2,3,3,2,2,4,2,3,4]
27+
Output: 4
28+
Explanation: We can apply the following operations to make the array empty:
29+
- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
30+
- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
31+
- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
32+
- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
33+
It can be shown that we cannot make the array empty in less than 4 operations.
34+
```
35+
36+
Example 2:
37+
38+
```
39+
Input: nums = [2,1,2,2,3,3]
40+
Output: -1
41+
Explanation: It is impossible to empty the array.
42+
```
43+
44+
 
45+
**Constraints:**
46+
47+
48+
49+
- `2 <= nums.length <= 105`
50+
51+
- `1 <= nums[i] <= 106`
52+
53+
54+
55+
## Solution
56+
57+
```javascript
58+
/**
59+
* @param {number[]} nums
60+
* @return {number}
61+
*/
62+
var minOperations = function(nums) {
63+
var map = {};
64+
for (var i = 0; i < nums.length; i++) {
65+
var num = nums[i];
66+
map[num] = map[num] || 0;
67+
map[num] += 1;
68+
}
69+
var keys = Object.keys(map);
70+
var res = 0;
71+
for (var j = 0; j < keys.length; j++) {
72+
var num = keys[j];
73+
if (map[num] === 1) return -1;
74+
res += Math.ceil(map[num] / 3);
75+
}
76+
return res;
77+
};
78+
```
79+
80+
**Explain:**
81+
82+
nope.
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(n).
87+
* Space complexity : O(n).

0 commit comments

Comments
 (0)