Skip to content

Commit 653d517

Browse files
committed
feat: solve No.1846,1877
1 parent d7c1ca3 commit 653d517

2 files changed

+182
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# 1846. Maximum Element After Decreasing and Rearranging
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Greedy, Sorting.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given an array of positive integers `arr`. Perform some operations (possibly none) on `arr` so that it satisfies these conditions:
10+
11+
12+
13+
- The value of the **first** element in `arr` must be `1`.
14+
15+
- The absolute difference between any 2 adjacent elements must be **less than or equal to **`1`. In other words, `abs(arr[i] - arr[i - 1]) <= 1` for each `i` where `1 <= i < arr.length` (**0-indexed**). `abs(x)` is the absolute value of `x`.
16+
17+
18+
There are 2 types of operations that you can perform any number of times:
19+
20+
21+
22+
- **Decrease** the value of any element of `arr` to a **smaller positive integer**.
23+
24+
- **Rearrange** the elements of `arr` to be in any order.
25+
26+
27+
Return **the **maximum** possible value of an element in **`arr`** after performing the operations to satisfy the conditions**.
28+
29+
 
30+
Example 1:
31+
32+
```
33+
Input: arr = [2,2,1,2,1]
34+
Output: 2
35+
Explanation:
36+
We can satisfy the conditions by rearranging arr so it becomes [1,2,2,2,1].
37+
The largest element in arr is 2.
38+
```
39+
40+
Example 2:
41+
42+
```
43+
Input: arr = [100,1,1000]
44+
Output: 3
45+
Explanation:
46+
One possible way to satisfy the conditions is by doing the following:
47+
1. Rearrange arr so it becomes [1,100,1000].
48+
2. Decrease the value of the second element to 2.
49+
3. Decrease the value of the third element to 3.
50+
Now arr = [1,2,3], which satisfies the conditions.
51+
The largest element in arr is 3.
52+
```
53+
54+
Example 3:
55+
56+
```
57+
Input: arr = [1,2,3,4,5]
58+
Output: 5
59+
Explanation: The array already satisfies the conditions, and the largest element is 5.
60+
```
61+
62+
 
63+
**Constraints:**
64+
65+
66+
67+
- `1 <= arr.length <= 105`
68+
69+
- `1 <= arr[i] <= 109`
70+
71+
72+
73+
## Solution
74+
75+
```javascript
76+
/**
77+
* @param {number[]} arr
78+
* @return {number}
79+
*/
80+
var maximumElementAfterDecrementingAndRearranging = function(arr) {
81+
arr.sort((a, b) => a - b);
82+
arr[0] = 1;
83+
for (var i = 1 ; i < arr.length; i++) {
84+
if (arr[i] - arr[i - 1] <= 1) continue;
85+
arr[i] = arr[i - 1] + 1;
86+
}
87+
return arr[arr.length - 1];
88+
};
89+
```
90+
91+
**Explain:**
92+
93+
nope.
94+
95+
**Complexity:**
96+
97+
* Time complexity : O(n * log(n)).
98+
* Space complexity : O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 1877. Minimize Maximum Pair Sum in Array
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Two Pointers, Greedy, Sorting.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
10+
11+
12+
13+
- For example, if we have pairs `(1,5)`, `(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
14+
15+
16+
Given an array `nums` of **even** length `n`, pair up the elements of `nums` into `n / 2` pairs such that:
17+
18+
19+
20+
- Each element of `nums` is in **exactly one** pair, and
21+
22+
- The **maximum pair sum **is **minimized**.
23+
24+
25+
Return **the minimized **maximum pair sum** after optimally pairing up the elements**.
26+
27+
 
28+
Example 1:
29+
30+
```
31+
Input: nums = [3,5,2,3]
32+
Output: 7
33+
Explanation: The elements can be paired up into pairs (3,3) and (5,2).
34+
The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
35+
```
36+
37+
Example 2:
38+
39+
```
40+
Input: nums = [3,5,4,2,4,6]
41+
Output: 8
42+
Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
43+
The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
44+
```
45+
46+
 
47+
**Constraints:**
48+
49+
50+
51+
- `n == nums.length`
52+
53+
- `2 <= n <= 105`
54+
55+
- `n` is **even**.
56+
57+
- `1 <= nums[i] <= 105`
58+
59+
60+
## Solution
61+
62+
```javascript
63+
/**
64+
* @param {number[]} nums
65+
* @return {number}
66+
*/
67+
var minPairSum = function(nums) {
68+
nums.sort((a, b) => a - b);
69+
var res = 0;
70+
for (var i = 0; i < nums.length / 2; i++) {
71+
res = Math.max(res, nums[i] + nums[nums.length - i - 1]);
72+
}
73+
return res;
74+
};
75+
```
76+
77+
**Explain:**
78+
79+
nope.
80+
81+
**Complexity:**
82+
83+
* Time complexity : O(n * log(n)).
84+
* Space complexity : O(1).

0 commit comments

Comments
 (0)