Skip to content

Commit e2a632d

Browse files
authored
feat: add solutions to lc problems: No.4014~4016 (#5343)
1 parent bc1f1fb commit e2a632d

21 files changed

Lines changed: 1827 additions & 20 deletions

File tree

solution/4000-4099/4014.Minimum Total Price After Applying Discounts/README.md

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,32 +101,146 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/4000-4099/4014.Mi
101101

102102
<!-- solution:start -->
103103

104-
### 方法一
104+
### 方法一:贪心 + 排序
105+
106+
为了最小化总价,我们需要最大化折扣节省的总金额。若把折扣 $d$ 应用于价格为 $p$ 的商品,节省的金额为 $p \times d / 100$。根据排序不等式,把较大的折扣用在价格较高的商品上,可以使节省的总金额最大。
107+
108+
因此,我们将 $\textit{prices}$ 和 $\textit{discounts}$ 都按升序排序,然后用双指针从两个数组的末尾开始,依次把当前最大的折扣应用到当前最贵的商品上,并累加折后价格。当折扣用完后,剩余的商品按原价累加即可。
109+
110+
时间复杂度 $O(n \times \log n + m \times \log m)$,空间复杂度 $O(\log n + \log m)$。其中 $n$ 和 $m$ 分别是数组 $\textit{prices}$ 和 $\textit{discounts}$ 的长度。
105111

106112
<!-- tabs:start -->
107113

108114
#### Python3
109115

110116
```python
111-
117+
class Solution:
118+
def minPrice(self, prices: list[int], discounts: list[int]) -> float:
119+
prices.sort()
120+
discounts.sort()
121+
i, j = len(prices) - 1, len(discounts) - 1
122+
ans = 0
123+
while i >= 0 and j >= 0:
124+
ans += prices[i] * (100 - discounts[j]) / 100
125+
i -= 1
126+
j -= 1
127+
while i >= 0:
128+
ans += prices[i]
129+
i -= 1
130+
return ans
112131
```
113132

114133
#### Java
115134

116135
```java
117-
136+
class Solution {
137+
public double minPrice(int[] prices, int[] discounts) {
138+
Arrays.sort(prices);
139+
Arrays.sort(discounts);
140+
141+
int i = prices.length - 1;
142+
int j = discounts.length - 1;
143+
144+
double ans = 0;
145+
146+
while (i >= 0 && j >= 0) {
147+
ans += prices[i] * (100 - discounts[j]) / 100.0;
148+
i--;
149+
j--;
150+
}
151+
152+
while (i >= 0) {
153+
ans += prices[i];
154+
i--;
155+
}
156+
157+
return ans;
158+
}
159+
}
118160
```
119161

120162
#### C++
121163

122164
```cpp
123-
165+
class Solution {
166+
public:
167+
double minPrice(vector<int>& prices, vector<int>& discounts) {
168+
sort(prices.begin(), prices.end());
169+
sort(discounts.begin(), discounts.end());
170+
171+
int i = prices.size() - 1;
172+
int j = discounts.size() - 1;
173+
174+
double ans = 0;
175+
176+
while (i >= 0 && j >= 0) {
177+
ans += prices[i] * (100 - discounts[j]) / 100.0;
178+
i--;
179+
j--;
180+
}
181+
182+
while (i >= 0) {
183+
ans += prices[i];
184+
i--;
185+
}
186+
187+
return ans;
188+
}
189+
};
124190
```
125191

126192
#### Go
127193

128194
```go
195+
func minPrice(prices []int, discounts []int) float64 {
196+
sort.Ints(prices)
197+
sort.Ints(discounts)
198+
199+
i := len(prices) - 1
200+
j := len(discounts) - 1
201+
202+
var ans float64
203+
204+
for i >= 0 && j >= 0 {
205+
ans += float64(prices[i]) * float64(100-discounts[j]) / 100.0
206+
i--
207+
j--
208+
}
209+
210+
for i >= 0 {
211+
ans += float64(prices[i])
212+
i--
213+
}
214+
215+
return ans
216+
}
217+
```
218+
219+
#### TypeScript
220+
221+
```ts
222+
function minPrice(prices: number[], discounts: number[]): number {
223+
prices.sort((a, b) => a - b);
224+
discounts.sort((a, b) => a - b);
225+
226+
let i = prices.length - 1;
227+
let j = discounts.length - 1;
228+
229+
let ans = 0;
230+
231+
while (i >= 0 && j >= 0) {
232+
ans += (prices[i] * (100 - discounts[j])) / 100;
233+
i--;
234+
j--;
235+
}
236+
237+
while (i >= 0) {
238+
ans += prices[i];
239+
i--;
240+
}
129241

242+
return ans;
243+
}
130244
```
131245

132246
<!-- tabs:end -->

solution/4000-4099/4014.Minimum Total Price After Applying Discounts/README_EN.md

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,146 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/4000-4099/4014.Mi
9999

100100
<!-- solution:start -->
101101

102-
### Solution 1
102+
### Solution 1: Greedy + Sorting
103+
104+
To minimize the total price, we need to maximize the total amount saved by discounts. Applying a discount $d$ to an item with price $p$ saves $p \times d / 100$. By the rearrangement inequality, applying larger discounts to more expensive items maximizes the total savings.
105+
106+
Therefore, we sort both $\textit{prices}$ and $\textit{discounts}$ in ascending order, then use two pointers starting from the ends of both arrays, repeatedly applying the current largest discount to the current most expensive item and accumulating the discounted price. Once all discounts are used up, the remaining items are added at their original prices.
107+
108+
The time complexity is $O(n \times \log n + m \times \log m)$, and the space complexity is $O(\log n + \log m)$. Here, $n$ and $m$ are the lengths of the arrays $\textit{prices}$ and $\textit{discounts}$, respectively.
103109

104110
<!-- tabs:start -->
105111

106112
#### Python3
107113

108114
```python
109-
115+
class Solution:
116+
def minPrice(self, prices: list[int], discounts: list[int]) -> float:
117+
prices.sort()
118+
discounts.sort()
119+
i, j = len(prices) - 1, len(discounts) - 1
120+
ans = 0
121+
while i >= 0 and j >= 0:
122+
ans += prices[i] * (100 - discounts[j]) / 100
123+
i -= 1
124+
j -= 1
125+
while i >= 0:
126+
ans += prices[i]
127+
i -= 1
128+
return ans
110129
```
111130

112131
#### Java
113132

114133
```java
115-
134+
class Solution {
135+
public double minPrice(int[] prices, int[] discounts) {
136+
Arrays.sort(prices);
137+
Arrays.sort(discounts);
138+
139+
int i = prices.length - 1;
140+
int j = discounts.length - 1;
141+
142+
double ans = 0;
143+
144+
while (i >= 0 && j >= 0) {
145+
ans += prices[i] * (100 - discounts[j]) / 100.0;
146+
i--;
147+
j--;
148+
}
149+
150+
while (i >= 0) {
151+
ans += prices[i];
152+
i--;
153+
}
154+
155+
return ans;
156+
}
157+
}
116158
```
117159

118160
#### C++
119161

120162
```cpp
121-
163+
class Solution {
164+
public:
165+
double minPrice(vector<int>& prices, vector<int>& discounts) {
166+
sort(prices.begin(), prices.end());
167+
sort(discounts.begin(), discounts.end());
168+
169+
int i = prices.size() - 1;
170+
int j = discounts.size() - 1;
171+
172+
double ans = 0;
173+
174+
while (i >= 0 && j >= 0) {
175+
ans += prices[i] * (100 - discounts[j]) / 100.0;
176+
i--;
177+
j--;
178+
}
179+
180+
while (i >= 0) {
181+
ans += prices[i];
182+
i--;
183+
}
184+
185+
return ans;
186+
}
187+
};
122188
```
123189

124190
#### Go
125191

126192
```go
193+
func minPrice(prices []int, discounts []int) float64 {
194+
sort.Ints(prices)
195+
sort.Ints(discounts)
196+
197+
i := len(prices) - 1
198+
j := len(discounts) - 1
199+
200+
var ans float64
201+
202+
for i >= 0 && j >= 0 {
203+
ans += float64(prices[i]) * float64(100-discounts[j]) / 100.0
204+
i--
205+
j--
206+
}
207+
208+
for i >= 0 {
209+
ans += float64(prices[i])
210+
i--
211+
}
212+
213+
return ans
214+
}
215+
```
216+
217+
#### TypeScript
218+
219+
```ts
220+
function minPrice(prices: number[], discounts: number[]): number {
221+
prices.sort((a, b) => a - b);
222+
discounts.sort((a, b) => a - b);
223+
224+
let i = prices.length - 1;
225+
let j = discounts.length - 1;
226+
227+
let ans = 0;
228+
229+
while (i >= 0 && j >= 0) {
230+
ans += (prices[i] * (100 - discounts[j])) / 100;
231+
i--;
232+
j--;
233+
}
234+
235+
while (i >= 0) {
236+
ans += prices[i];
237+
i--;
238+
}
127239

240+
return ans;
241+
}
128242
```
129243

130244
<!-- tabs:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
double minPrice(vector<int>& prices, vector<int>& discounts) {
4+
sort(prices.begin(), prices.end());
5+
sort(discounts.begin(), discounts.end());
6+
7+
int i = prices.size() - 1;
8+
int j = discounts.size() - 1;
9+
10+
double ans = 0;
11+
12+
while (i >= 0 && j >= 0) {
13+
ans += prices[i] * (100 - discounts[j]) / 100.0;
14+
i--;
15+
j--;
16+
}
17+
18+
while (i >= 0) {
19+
ans += prices[i];
20+
i--;
21+
}
22+
23+
return ans;
24+
}
25+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func minPrice(prices []int, discounts []int) float64 {
2+
sort.Ints(prices)
3+
sort.Ints(discounts)
4+
5+
i := len(prices) - 1
6+
j := len(discounts) - 1
7+
8+
var ans float64
9+
10+
for i >= 0 && j >= 0 {
11+
ans += float64(prices[i]) * float64(100-discounts[j]) / 100.0
12+
i--
13+
j--
14+
}
15+
16+
for i >= 0 {
17+
ans += float64(prices[i])
18+
i--
19+
}
20+
21+
return ans
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public double minPrice(int[] prices, int[] discounts) {
3+
Arrays.sort(prices);
4+
Arrays.sort(discounts);
5+
6+
int i = prices.length - 1;
7+
int j = discounts.length - 1;
8+
9+
double ans = 0;
10+
11+
while (i >= 0 && j >= 0) {
12+
ans += prices[i] * (100 - discounts[j]) / 100.0;
13+
i--;
14+
j--;
15+
}
16+
17+
while (i >= 0) {
18+
ans += prices[i];
19+
i--;
20+
}
21+
22+
return ans;
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def minPrice(self, prices: list[int], discounts: list[int]) -> float:
3+
prices.sort()
4+
discounts.sort()
5+
i, j = len(prices) - 1, len(discounts) - 1
6+
ans = 0
7+
while i >= 0 and j >= 0:
8+
ans += prices[i] * (100 - discounts[j]) / 100
9+
i -= 1
10+
j -= 1
11+
while i >= 0:
12+
ans += prices[i]
13+
i -= 1
14+
return ans

0 commit comments

Comments
 (0)