Skip to content

Commit 6ff5f4c

Browse files
authored
feat: add solutions to lc problem: No.2144 (#1494)
No.2144.Minimum Cost of Buying Candies With Discount
1 parent 001dcc6 commit 6ff5f4c

File tree

10 files changed

+103
-83
lines changed

10 files changed

+103
-83
lines changed

solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README.md

+32-22
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:贪心**
65+
66+
我们可以先将糖果按照价格从高到低排序,然后每三个糖果中取两个糖果,这样可以保证免费获得的糖果价格最高,从而使得总开销最小。
67+
68+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是糖果数。
69+
6470
<!-- tabs:start -->
6571

6672
### **Python3**
@@ -70,13 +76,8 @@
7076
```python
7177
class Solution:
7278
def minimumCost(self, cost: List[int]) -> int:
73-
cost.sort()
74-
ans, n = 0, len(cost)
75-
for i in range(n - 1, -1, -3):
76-
ans += cost[i]
77-
if i >= 1:
78-
ans += cost[i - 1]
79-
return ans
79+
cost.sort(reverse=True)
80+
return sum(cost) - sum(cost[2::3])
8081
```
8182

8283
### **Java**
@@ -87,10 +88,10 @@ class Solution:
8788
class Solution {
8889
public int minimumCost(int[] cost) {
8990
Arrays.sort(cost);
90-
int ans = 0, n = cost.length;
91-
for (int i = n - 1; i >= 0; i -= 3) {
91+
int ans = 0;
92+
for (int i = cost.length - 1; i >= 0; i -= 3) {
9293
ans += cost[i];
93-
if (i >= 1) {
94+
if (i > 0) {
9495
ans += cost[i - 1];
9596
}
9697
}
@@ -105,11 +106,13 @@ class Solution {
105106
class Solution {
106107
public:
107108
int minimumCost(vector<int>& cost) {
108-
sort(cost.begin(), cost.end());
109-
int ans = 0, n = cost.size();
110-
for (int i = n - 1; i >= 0; i -= 3) {
109+
sort(cost.rbegin(), cost.rend());
110+
int ans = 0;
111+
for (int i = 0; i < cost.size(); i += 3) {
111112
ans += cost[i];
112-
if (i >= 1) ans += cost[i - 1];
113+
if (i < cost.size() - 1) {
114+
ans += cost[i + 1];
115+
}
113116
}
114117
return ans;
115118
}
@@ -119,25 +122,32 @@ public:
119122
### **Go**
120123
121124
```go
122-
func minimumCost(cost []int) int {
125+
func minimumCost(cost []int) (ans int) {
123126
sort.Ints(cost)
124-
ans, n := 0, len(cost)
125-
for i := n - 1; i >= 0; i -= 3 {
127+
for i := len(cost) - 1; i >= 0; i -= 3 {
126128
ans += cost[i]
127-
if i >= 1 {
129+
if i > 0 {
128130
ans += cost[i-1]
129131
}
130132
}
131-
return ans
133+
return
132134
}
133135
```
134136

135137
### **TypeScript**
136138

137-
<!-- 这里可写当前语言的特殊实现逻辑 -->
138-
139139
```ts
140-
140+
function minimumCost(cost: number[]): number {
141+
cost.sort((a, b) => a - b);
142+
let ans = 0;
143+
for (let i = cost.length - 1; i >= 0; i -= 3) {
144+
ans += cost[i];
145+
if (i) {
146+
ans += cost[i - 1];
147+
}
148+
}
149+
return ans;
150+
}
141151
```
142152

143153
### **...**

solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README_EN.md

+26-20
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,8 @@ Hence, the minimum cost to buy all candies is 5 + 5 = 10.
6565
```python
6666
class Solution:
6767
def minimumCost(self, cost: List[int]) -> int:
68-
cost.sort()
69-
ans, n = 0, len(cost)
70-
for i in range(n - 1, -1, -3):
71-
ans += cost[i]
72-
if i >= 1:
73-
ans += cost[i - 1]
74-
return ans
68+
cost.sort(reverse=True)
69+
return sum(cost) - sum(cost[2::3])
7570
```
7671

7772
### **Java**
@@ -80,10 +75,10 @@ class Solution:
8075
class Solution {
8176
public int minimumCost(int[] cost) {
8277
Arrays.sort(cost);
83-
int ans = 0, n = cost.length;
84-
for (int i = n - 1; i >= 0; i -= 3) {
78+
int ans = 0;
79+
for (int i = cost.length - 1; i >= 0; i -= 3) {
8580
ans += cost[i];
86-
if (i >= 1) {
81+
if (i > 0) {
8782
ans += cost[i - 1];
8883
}
8984
}
@@ -98,11 +93,13 @@ class Solution {
9893
class Solution {
9994
public:
10095
int minimumCost(vector<int>& cost) {
101-
sort(cost.begin(), cost.end());
102-
int ans = 0, n = cost.size();
103-
for (int i = n - 1; i >= 0; i -= 3) {
96+
sort(cost.rbegin(), cost.rend());
97+
int ans = 0;
98+
for (int i = 0; i < cost.size(); i += 3) {
10499
ans += cost[i];
105-
if (i >= 1) ans += cost[i - 1];
100+
if (i < cost.size() - 1) {
101+
ans += cost[i + 1];
102+
}
106103
}
107104
return ans;
108105
}
@@ -112,23 +109,32 @@ public:
112109
### **Go**
113110
114111
```go
115-
func minimumCost(cost []int) int {
112+
func minimumCost(cost []int) (ans int) {
116113
sort.Ints(cost)
117-
ans, n := 0, len(cost)
118-
for i := n - 1; i >= 0; i -= 3 {
114+
for i := len(cost) - 1; i >= 0; i -= 3 {
119115
ans += cost[i]
120-
if i >= 1 {
116+
if i > 0 {
121117
ans += cost[i-1]
122118
}
123119
}
124-
return ans
120+
return
125121
}
126122
```
127123

128124
### **TypeScript**
129125

130126
```ts
131-
127+
function minimumCost(cost: number[]): number {
128+
cost.sort((a, b) => a - b);
129+
let ans = 0;
130+
for (let i = cost.length - 1; i >= 0; i -= 3) {
131+
ans += cost[i];
132+
if (i) {
133+
ans += cost[i - 1];
134+
}
135+
}
136+
return ans;
137+
}
132138
```
133139

134140
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
class Solution {
2-
public:
3-
int minimumCost(vector<int>& cost) {
4-
sort(cost.begin(), cost.end());
5-
int ans = 0, n = cost.size();
6-
for (int i = n - 1; i >= 0; i -= 3) {
7-
ans += cost[i];
8-
if (i >= 1) ans += cost[i - 1];
9-
}
10-
return ans;
11-
}
1+
class Solution {
2+
public:
3+
int minimumCost(vector<int>& cost) {
4+
sort(cost.rbegin(), cost.rend());
5+
int ans = 0;
6+
for (int i = 0; i < cost.size(); i += 3) {
7+
ans += cost[i];
8+
if (i < cost.size() - 1) {
9+
ans += cost[i + 1];
10+
}
11+
}
12+
return ans;
13+
}
1214
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
func minimumCost(cost []int) int {
1+
func minimumCost(cost []int) (ans int) {
22
sort.Ints(cost)
3-
ans, n := 0, len(cost)
4-
for i := n - 1; i >= 0; i -= 3 {
3+
for i := len(cost) - 1; i >= 0; i -= 3 {
54
ans += cost[i]
6-
if i >= 1 {
5+
if i > 0 {
76
ans += cost[i-1]
87
}
98
}
10-
return ans
9+
return
1110
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
class Solution {
2-
public int minimumCost(int[] cost) {
3-
Arrays.sort(cost);
4-
int ans = 0, n = cost.length;
5-
for (int i = n - 1; i >= 0; i -= 3) {
6-
ans += cost[i];
7-
if (i >= 1) {
8-
ans += cost[i - 1];
9-
}
10-
}
11-
return ans;
12-
}
1+
class Solution {
2+
public int minimumCost(int[] cost) {
3+
Arrays.sort(cost);
4+
int ans = 0;
5+
for (int i = cost.length - 1; i >= 0; i -= 3) {
6+
ans += cost[i];
7+
if (i > 0) {
8+
ans += cost[i - 1];
9+
}
10+
}
11+
return ans;
12+
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
class Solution:
2-
def minimumCost(self, cost: List[int]) -> int:
3-
cost.sort()
4-
ans, n = 0, len(cost)
5-
for i in range(n - 1, -1, -3):
6-
ans += cost[i]
7-
if i >= 1:
8-
ans += cost[i - 1]
9-
return ans
1+
class Solution:
2+
def minimumCost(self, cost: List[int]) -> int:
3+
cost.sort(reverse=True)
4+
return sum(cost) - sum(cost[2::3])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minimumCost(cost: number[]): number {
2+
cost.sort((a, b) => a - b);
3+
let ans = 0;
4+
for (let i = cost.length - 1; i >= 0; i -= 3) {
5+
ans += cost[i];
6+
if (i) {
7+
ans += cost[i - 1];
8+
}
9+
}
10+
return ans;
11+
}

solution/2100-2199/2145.Count the Hidden Sequences/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212

1313
<ul>
1414
<li>比方说,<code>differences = [1, -3, 4]</code>&nbsp;,<code>lower = 1</code>&nbsp;,<code>upper = 6</code>&nbsp;,那么隐藏数组是一个长度为 <code>4</code>&nbsp;且所有值都在&nbsp;<code>1</code>&nbsp;和&nbsp;<code>6</code>&nbsp;(包含两者)之间的数组。
15-
1615
<ul>
1716
<li><code>[3, 4, 1, 5]</code> 和&nbsp;<code>[4, 5, 2, 6]</code>&nbsp;都是符合要求的隐藏数组。</li>
1817
<li><code>[5, 6, 3, 7]</code>&nbsp;不符合要求,因为它包含大于 <code>6</code>&nbsp;的元素。</li>
1918
<li><code>[1, 2, 3, 4]</code>&nbsp;不符合要求,因为相邻元素的差值不符合给定数据。</li>
2019
</ul>
2120
</li>
22-
2321
</ul>
2422

2523
<p>请你返回 <strong>符合</strong>&nbsp;要求的隐藏数组的数目。如果没有符合要求的隐藏数组,请返回 <code>0</code>&nbsp;。</p>

solution/2100-2199/2145.Count the Hidden Sequences/README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010

1111
<ul>
1212
<li>For example, given <code>differences = [1, -3, 4]</code>, <code>lower = 1</code>, <code>upper = 6</code>, the hidden sequence is a sequence of length <code>4</code> whose elements are in between <code>1</code> and <code>6</code> (<strong>inclusive</strong>).
13-
1413
<ul>
1514
<li><code>[3, 4, 1, 5]</code> and <code>[4, 5, 2, 6]</code> are possible hidden sequences.</li>
1615
<li><code>[5, 6, 3, 7]</code> is not possible since it contains an element greater than <code>6</code>.</li>
1716
<li><code>[1, 2, 3, 4]</code> is not possible since the differences are not correct.</li>
1817
</ul>
1918
</li>
20-
2119
</ul>
2220

2321
<p>Return <em>the number of <strong>possible</strong> hidden sequences there are.</em> If there are no possible sequences, return <code>0</code>.</p>

solution/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
2111,
7070
2117,
7171
2140,
72+
2144,
7273
2178,
7374
2241,
7475
2288,

0 commit comments

Comments
 (0)