Skip to content

Commit e762de6

Browse files
committed
feat: solve No.1359,1774
1 parent 4055545 commit e762de6

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 1359. Count All Valid Pickup and Delivery Options
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Math, Dynamic Programming, Combinatorics.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given `n` orders, each order consist in pickup and delivery services. 
10+
11+
Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). 
12+
13+
Since the answer may be too large, return it modulo 10^9 + 7.
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: n = 1
20+
Output: 1
21+
Explanation: Unique order (P1, D1), Delivery 1 always is after of Pickup 1.
22+
```
23+
24+
Example 2:
25+
26+
```
27+
Input: n = 2
28+
Output: 6
29+
Explanation: All possible orders:
30+
(P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1) and (P2,D2,P1,D1).
31+
This is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery 2.
32+
```
33+
34+
Example 3:
35+
36+
```
37+
Input: n = 3
38+
Output: 90
39+
```
40+
41+
 
42+
**Constraints:**
43+
44+
45+
46+
- `1 <= n <= 500`
47+
48+
49+
50+
## Solution
51+
52+
```javascript
53+
/**
54+
* @param {number} n
55+
* @return {number}
56+
*/
57+
var countOrders = function(n) {
58+
return helper(n, 0, 0, {});
59+
};
60+
61+
var helper = function(n, x, y, dp) {
62+
if (x === n && y === n) return 1;
63+
var mod = Math.pow(10, 9) + 7;
64+
var key = `${x}-${y}`;
65+
if (dp[key] === undefined) {
66+
var choosePickup = x < n ? ((n - x) * helper(n, x + 1, y, dp) % mod) : 0;
67+
var chooseDelivery = y < n && x > y ? ((x - y) * helper(n, x, y + 1, dp) % mod) : 0;
68+
dp[key] = (choosePickup + chooseDelivery) % mod;
69+
}
70+
return dp[key];
71+
};
72+
```
73+
74+
**Explain:**
75+
76+
nope.
77+
78+
**Complexity:**
79+
80+
* Time complexity : O(n).
81+
* Space complexity : O(n).
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# 1774. Closest Dessert Cost
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming, Backtracking.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You would like to make dessert and are preparing to buy the ingredients. You have `n` ice cream base flavors and `m` types of toppings to choose from. You must follow these rules when making your dessert:
10+
11+
12+
13+
- There must be **exactly one** ice cream base.
14+
15+
- You can add **one or more** types of topping or have no toppings at all.
16+
17+
- There are **at most two** of **each type** of topping.
18+
19+
20+
You are given three inputs:
21+
22+
23+
24+
- `baseCosts`, an integer array of length `n`, where each `baseCosts[i]` represents the price of the `ith` ice cream base flavor.
25+
26+
- `toppingCosts`, an integer array of length `m`, where each `toppingCosts[i]` is the price of **one** of the `ith` topping.
27+
28+
- `target`, an integer representing your target price for dessert.
29+
30+
31+
You want to make a dessert with a total cost as close to `target` as possible.
32+
33+
Return **the closest possible cost of the dessert to **`target`. If there are multiple, return **the **lower** one.**
34+
35+
 
36+
Example 1:
37+
38+
```
39+
Input: baseCosts = [1,7], toppingCosts = [3,4], target = 10
40+
Output: 10
41+
Explanation: Consider the following combination (all 0-indexed):
42+
- Choose base 1: cost 7
43+
- Take 1 of topping 0: cost 1 x 3 = 3
44+
- Take 0 of topping 1: cost 0 x 4 = 0
45+
Total: 7 + 3 + 0 = 10.
46+
```
47+
48+
Example 2:
49+
50+
```
51+
Input: baseCosts = [2,3], toppingCosts = [4,5,100], target = 18
52+
Output: 17
53+
Explanation: Consider the following combination (all 0-indexed):
54+
- Choose base 1: cost 3
55+
- Take 1 of topping 0: cost 1 x 4 = 4
56+
- Take 2 of topping 1: cost 2 x 5 = 10
57+
- Take 0 of topping 2: cost 0 x 100 = 0
58+
Total: 3 + 4 + 10 + 0 = 17. You cannot make a dessert with a total cost of 18.
59+
```
60+
61+
Example 3:
62+
63+
```
64+
Input: baseCosts = [3,10], toppingCosts = [2,5], target = 9
65+
Output: 8
66+
Explanation: It is possible to make desserts with cost 8 and 10. Return 8 as it is the lower cost.
67+
```
68+
69+
 
70+
**Constraints:**
71+
72+
73+
74+
- `n == baseCosts.length`
75+
76+
- `m == toppingCosts.length`
77+
78+
- `1 <= n, m <= 10`
79+
80+
- `1 <= baseCosts[i], toppingCosts[i] <= 104`
81+
82+
- `1 <= target <= 104`
83+
84+
85+
86+
## Solution
87+
88+
```javascript
89+
/**
90+
* @param {number[]} baseCosts
91+
* @param {number[]} toppingCosts
92+
* @param {number} target
93+
* @return {number}
94+
*/
95+
var closestCost = function(baseCosts, toppingCosts, target) {
96+
var res = Number.MAX_SAFE_INTEGER;
97+
for (var i = 0; i < baseCosts.length; i++) {
98+
res = closest(target, res, baseCosts[i] + helper(toppingCosts, target - baseCosts[i], 0));
99+
}
100+
return res;
101+
};
102+
103+
var helper = function(toppingCosts, target, i) {
104+
if (i === toppingCosts.length) return 0;
105+
if (target <= 0) return 0;
106+
var res = Number.MAX_SAFE_INTEGER;
107+
res = closest(target, res, helper(toppingCosts, target, i + 1));
108+
res = closest(target, res, toppingCosts[i] + helper(toppingCosts, target - toppingCosts[i], i + 1));
109+
res = closest(target, res, toppingCosts[i] * 2 + helper(toppingCosts, target - toppingCosts[i] * 2, i + 1));
110+
return res;
111+
};
112+
113+
var closest = function(target, num1, num2) {
114+
var diff1 = Math.abs(num1 - target);
115+
var diff2 = Math.abs(num2 - target);
116+
if (diff1 === diff2) return Math.min(num1, num2);
117+
return diff1 < diff2 ? num1 : num2;
118+
};
119+
```
120+
121+
**Explain:**
122+
123+
nope.
124+
125+
**Complexity:**
126+
127+
* Time complexity : O(n * m ^ 3).
128+
* Space complexity : O(m).

0 commit comments

Comments
 (0)