Skip to content

Commit e2fe5e7

Browse files
committed
feat: solve No.714,2742
1 parent 5dd0f98 commit e2fe5e7

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

2701-2800/2742. Painting the Walls.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# 2742. Painting the Walls
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Dynamic Programming.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given two **0-indexed** integer arrays, `cost` and `time`, of size `n` representing the costs and the time taken to paint `n` different walls respectively. There are two painters available:
10+
11+
12+
13+
- A** paid painter** that paints the `ith` wall in `time[i]` units of time and takes `cost[i]` units of money.
14+
15+
- A** free painter** that paints **any** wall in `1` unit of time at a cost of `0`. But the free painter can only be used if the paid painter is already **occupied**.
16+
17+
18+
Return **the minimum amount of money required to paint the **`n`** walls.**
19+
20+
 
21+
Example 1:
22+
23+
```
24+
Input: cost = [1,2,3,2], time = [1,2,3,2]
25+
Output: 3
26+
Explanation: The walls at index 0 and 1 will be painted by the paid painter, and it will take 3 units of time; meanwhile, the free painter will paint the walls at index 2 and 3, free of cost in 2 units of time. Thus, the total cost is 1 + 2 = 3.
27+
```
28+
29+
Example 2:
30+
31+
```
32+
Input: cost = [2,3,4,2], time = [1,1,1,1]
33+
Output: 4
34+
Explanation: The walls at index 0 and 3 will be painted by the paid painter, and it will take 2 units of time; meanwhile, the free painter will paint the walls at index 1 and 2, free of cost in 2 units of time. Thus, the total cost is 2 + 2 = 4.
35+
```
36+
37+
 
38+
**Constraints:**
39+
40+
41+
42+
- `1 <= cost.length <= 500`
43+
44+
- `cost.length == time.length`
45+
46+
- `1 <= cost[i] <= 106`
47+
48+
- `1 <= time[i] <= 500`
49+
50+
51+
52+
## Solution
53+
54+
```javascript
55+
/**
56+
* @param {number[]} cost
57+
* @param {number[]} time
58+
* @return {number}
59+
*/
60+
var paintWalls = function(cost, time) {
61+
var dp = Array(cost.length).fill(0).map(() => Array(cost.length + 1));
62+
return helper(cost, time, 0, cost.length, dp);
63+
};
64+
65+
var helper = function(cost, time, i, remains, dp) {
66+
if (remains <= 0) return 0;
67+
if (i === cost.length) return Number.MAX_SAFE_INTEGER;
68+
if (dp[i][remains] !== undefined) return dp[i][remains];
69+
var paintByPaidPainter = cost[i] + helper(cost, time, i + 1, remains - time[i] - 1, dp);
70+
var paintByFreePainter = helper(cost, time, i + 1, remains, dp);
71+
dp[i][remains] = Math.min(paintByPaidPainter, paintByFreePainter);
72+
return dp[i][remains];
73+
};
74+
```
75+
76+
**Explain:**
77+
78+
Top down dp.
79+
80+
**Complexity:**
81+
82+
* Time complexity : O(n ^ 2).
83+
* Space complexity : O(n ^ 2).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 714. Best Time to Buy and Sell Stock with Transaction Fee
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming, Greedy.
5+
- Similar Questions: Best Time to Buy and Sell Stock II.
6+
7+
## Problem
8+
9+
You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day, and an integer `fee` representing a transaction fee.
10+
11+
Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.
12+
13+
**Note:**
14+
15+
16+
17+
- You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
18+
19+
- The transaction fee is only charged once for each stock purchase and sale.
20+
21+
22+
 
23+
Example 1:
24+
25+
```
26+
Input: prices = [1,3,2,8,4,9], fee = 2
27+
Output: 8
28+
Explanation: The maximum profit can be achieved by:
29+
- Buying at prices[0] = 1
30+
- Selling at prices[3] = 8
31+
- Buying at prices[4] = 4
32+
- Selling at prices[5] = 9
33+
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
34+
```
35+
36+
Example 2:
37+
38+
```
39+
Input: prices = [1,3,7,5,10,3], fee = 3
40+
Output: 6
41+
```
42+
43+
 
44+
**Constraints:**
45+
46+
47+
48+
- `1 <= prices.length <= 5 * 104`
49+
50+
- `1 <= prices[i] < 5 * 104`
51+
52+
- `0 <= fee < 5 * 104`
53+
54+
55+
56+
## Solution
57+
58+
```javascript
59+
/**
60+
* @param {number[]} prices
61+
* @param {number} fee
62+
* @return {number}
63+
*/
64+
var maxProfit = function(prices, fee) {
65+
var hasStock = Number.MIN_SAFE_INTEGER;
66+
var hasNoStock = 0;
67+
for (var i = 0; i < prices.length; i++) {
68+
var a = Math.max(hasStock, hasNoStock - prices[i]);
69+
var b = Math.max(hasNoStock, hasStock + prices[i] - fee);
70+
hasStock = a;
71+
hasNoStock = b;
72+
}
73+
return Math.max(hasStock, hasNoStock);
74+
};
75+
```
76+
77+
**Explain:**
78+
79+
nope.
80+
81+
**Complexity:**
82+
83+
* Time complexity : O(n).
84+
* Space complexity : O(1).

0 commit comments

Comments
 (0)