Skip to content

Commit 199efae

Browse files
committed
feat: add No.739,2966
1 parent eed4b10 commit 199efae

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 2966. Divide Array Into Arrays With Max Difference
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Greedy, Sorting.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given an integer array `nums` of size `n` and a positive integer `k`.
10+
11+
Divide the array into one or more arrays of size `3` satisfying the following conditions:
12+
13+
14+
15+
- **Each** element of `nums` should be in **exactly** one array.
16+
17+
- The difference between **any** two elements in one array is less than or equal to `k`.
18+
19+
20+
Return **a ****2D**** array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return **any** of them.**
21+
22+
 
23+
Example 1:
24+
25+
```
26+
Input: nums = [1,3,4,8,7,9,3,5,1], k = 2
27+
Output: [[1,1,3],[3,4,5],[7,8,9]]
28+
Explanation: We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
29+
The difference between any two elements in each array is less than or equal to 2.
30+
Note that the order of elements is not important.
31+
```
32+
33+
Example 2:
34+
35+
```
36+
Input: nums = [1,3,3,2,7,3], k = 3
37+
Output: []
38+
Explanation: It is not possible to divide the array satisfying all the conditions.
39+
```
40+
41+
 
42+
**Constraints:**
43+
44+
45+
46+
- `n == nums.length`
47+
48+
- `1 <= n <= 105`
49+
50+
- `n` is a multiple of `3`.
51+
52+
- `1 <= nums[i] <= 105`
53+
54+
- `1 <= k <= 105`
55+
56+
57+
58+
## Solution
59+
60+
```javascript
61+
/**
62+
* @param {number[]} nums
63+
* @param {number} k
64+
* @return {number[][]}
65+
*/
66+
var divideArray = function(nums, k) {
67+
nums.sort((a, b) => a - b);
68+
var res = [];
69+
for (var i = 0; i < nums.length; i += 3) {
70+
if (nums[i + 2] - nums[i] <= k) {
71+
res.push([nums[i], nums[i + 1], nums[i + 2]]);
72+
} else {
73+
return [];
74+
}
75+
}
76+
return res;
77+
};
78+
```
79+
80+
**Explain:**
81+
82+
nope.
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(n * log(n)).
87+
* Space complexity : O(n).

701-800/739. Daily Temperatures.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 739. Daily Temperatures
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Stack, Monotonic Stack.
5+
- Similar Questions: Next Greater Element I, Online Stock Span.
6+
7+
## Problem
8+
9+
Given an array of integers `temperatures` represents the daily temperatures, return **an array** `answer` **such that** `answer[i]` **is the number of days you have to wait after the** `ith` **day to get a warmer temperature**. If there is no future day for which this is possible, keep `answer[i] == 0` instead.
10+
11+
 
12+
Example 1:
13+
```
14+
Input: temperatures = [73,74,75,71,69,72,76,73]
15+
Output: [1,1,4,2,1,1,0,0]
16+
```Example 2:
17+
```
18+
Input: temperatures = [30,40,50,60]
19+
Output: [1,1,1,0]
20+
```Example 3:
21+
```
22+
Input: temperatures = [30,60,90]
23+
Output: [1,1,0]
24+
```
25+
 
26+
**Constraints:**
27+
28+
29+
30+
- `1 <= temperatures.length <= 105`
31+
32+
- `30 <= temperatures[i] <= 100`
33+
34+
35+
36+
## Solution
37+
38+
```javascript
39+
/**
40+
* @param {number[]} temperatures
41+
* @return {number[]}
42+
*/
43+
var dailyTemperatures = function(temperatures) {
44+
var stack = [];
45+
var res = Array(temperatures.length).fill(0);
46+
for (var i = 0; i < temperatures.length; i++) {
47+
while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
48+
var index = stack.pop();
49+
res[index] = i - index;
50+
}
51+
stack.push(i);
52+
}
53+
return res;
54+
};
55+
```
56+
57+
**Explain:**
58+
59+
nope.
60+
61+
**Complexity:**
62+
63+
* Time complexity : O(n).
64+
* Space complexity : O(n).

0 commit comments

Comments
 (0)