Skip to content

Commit 219e184

Browse files
committed
feat: solve No.1475,2770
1 parent 8694ded commit 219e184

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 1475. Final Prices With a Special Discount in a Shop
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Stack, Monotonic Stack.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given an integer array `prices` where `prices[i]` is the price of the `ith` item in a shop.
10+
11+
There is a special discount for items in the shop. If you buy the `ith` item, then you will receive a discount equivalent to `prices[j]` where `j` is the minimum index such that `j > i` and `prices[j] <= prices[i]`. Otherwise, you will not receive any discount at all.
12+
13+
Return an integer array `answer` where `answer[i]` is the final price you will pay for the `ith` item of the shop, considering the special discount.
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: prices = [8,4,6,2,3]
20+
Output: [4,2,4,2,3]
21+
Explanation:
22+
For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4.
23+
For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2.
24+
For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4.
25+
For items 3 and 4 you will not receive any discount at all.
26+
```
27+
28+
Example 2:
29+
30+
```
31+
Input: prices = [1,2,3,4,5]
32+
Output: [1,2,3,4,5]
33+
Explanation: In this case, for all items, you will not receive any discount at all.
34+
```
35+
36+
Example 3:
37+
38+
```
39+
Input: prices = [10,1,1,6]
40+
Output: [9,0,1,6]
41+
```
42+
43+
 
44+
**Constraints:**
45+
46+
47+
48+
- `1 <= prices.length <= 500`
49+
50+
- `1 <= prices[i] <= 1000`
51+
52+
53+
54+
## Solution
55+
56+
```javascript
57+
/**
58+
* @param {number[]} prices
59+
* @return {number[]}
60+
*/
61+
var finalPrices = function(prices) {
62+
var res = Array.from(prices);
63+
for (var i = 0; i < prices.length; i++) {
64+
for (var j = i + 1; j < prices.length; j++) {
65+
if (prices[j] <= prices[i]) {
66+
res[i] = prices[i] - prices[j];
67+
break;
68+
}
69+
}
70+
}
71+
return res;
72+
};
73+
```
74+
75+
**Explain:**
76+
77+
nope.
78+
79+
**Complexity:**
80+
81+
* Time complexity : O(n ^ 2).
82+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# 2770. Maximum Number of Jumps to Reach the Last Index
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming.
5+
- Similar Questions: Jump Game II, Frog Jump, Jump Game III, Jump Game IV, Minimum Jumps to Reach Home, Jump Game VII.
6+
7+
## Problem
8+
9+
You are given a **0-indexed** array `nums` of `n` integers and an integer `target`.
10+
11+
You are initially positioned at index `0`. In one step, you can jump from index `i` to any index `j` such that:
12+
13+
14+
15+
- `0 <= i < j < n`
16+
17+
- `-target <= nums[j] - nums[i] <= target`
18+
19+
20+
Return **the **maximum number of jumps** you can make to reach index** `n - 1`.
21+
22+
If there is no way to reach index `n - 1`, return `-1`.
23+
24+
 
25+
Example 1:
26+
27+
```
28+
Input: nums = [1,3,6,4,1,2], target = 2
29+
Output: 3
30+
Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
31+
- Jump from index 0 to index 1.
32+
- Jump from index 1 to index 3.
33+
- Jump from index 3 to index 5.
34+
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 3 jumps. Hence, the answer is 3.
35+
```
36+
37+
Example 2:
38+
39+
```
40+
Input: nums = [1,3,6,4,1,2], target = 3
41+
Output: 5
42+
Explanation: To go from index 0 to index n - 1 with the maximum number of jumps, you can perform the following jumping sequence:
43+
- Jump from index 0 to index 1.
44+
- Jump from index 1 to index 2.
45+
- Jump from index 2 to index 3.
46+
- Jump from index 3 to index 4.
47+
- Jump from index 4 to index 5.
48+
It can be proven that there is no other jumping sequence that goes from 0 to n - 1 with more than 5 jumps. Hence, the answer is 5.
49+
```
50+
51+
Example 3:
52+
53+
```
54+
Input: nums = [1,3,6,4,1,2], target = 0
55+
Output: -1
56+
Explanation: It can be proven that there is no jumping sequence that goes from 0 to n - 1. Hence, the answer is -1.
57+
```
58+
59+
 
60+
**Constraints:**
61+
62+
63+
64+
- `2 <= nums.length == n <= 1000`
65+
66+
- `-109 <= nums[i] <= 109`
67+
68+
- `0 <= target <= 2 * 109`
69+
70+
71+
72+
## Solution
73+
74+
```javascript
75+
/**
76+
* @param {number[]} nums
77+
* @param {number} target
78+
* @return {number}
79+
*/
80+
var maximumJumps = function(nums, target) {
81+
var dp = Array(nums.length);
82+
for (var i = nums.length - 1; i >= 0; i--) {
83+
dp[i] = i === nums.length - 1 ? 0 : -1;
84+
for (var j = i + 1; j < nums.length; j++) {
85+
if (Math.abs(nums[j] - nums[i]) <= target && dp[j] !== -1) {
86+
dp[i] = Math.max(dp[i], 1 + dp[j]);
87+
}
88+
}
89+
}
90+
return dp[0];
91+
};
92+
```
93+
94+
**Explain:**
95+
96+
Bottom-up dynamic programming.
97+
98+
**Complexity:**
99+
100+
* Time complexity : O(n ^ 2).
101+
* Space complexity : O(n).

0 commit comments

Comments
 (0)