Skip to content

Commit 348d3e4

Browse files
committed
feat: add solutions to lc problem: No.0638
No.0638.Shopping Offers
1 parent 633ed3e commit 348d3e4

File tree

10 files changed

+340
-9
lines changed

10 files changed

+340
-9
lines changed

solution/0100-0199/0175.Combine Two Tables/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ AddressId 是上表主键
4040
<pre>FirstName, LastName, City, State
4141
</pre>
4242

43-
4443
## 解法
4544

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

solution/0100-0199/0176.Second Highest Salary/README.md

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
+---------------------+
2727
</pre>
2828

29-
3029
## 解法
3130

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

solution/0100-0199/0177.Nth Highest Salary/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [177. 第N高的薪水](https://leetcode-cn.com/problems/nth-highest-salary)
1+
# [177. 第 N 高的薪水](https://leetcode-cn.com/problems/nth-highest-salary)
22

33
[English Version](/solution/0100-0199/0177.Nth%20Highest%20Salary/README_EN.md)
44

@@ -26,7 +26,6 @@
2626
+------------------------+
2727
</pre>
2828

29-
3029
## 解法
3130

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

solution/0100-0199/0177.Nth Highest Salary/README_EN.md

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
</pre>
3232

33-
3433
## Solutions
3534

3635
<!-- tabs:start -->

solution/0600-0699/0638.Shopping Offers/README.md

+116-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ A,B,C的价格分别为&yen;2,&yen;3,&yen;4.
4545
<li>你不可以购买超出待购清单的物品,即使更便宜。</li>
4646
</ol>
4747

48-
4948
## 解法
5049

5150
<!-- 这里可写通用的实现逻辑 -->
@@ -57,15 +56,130 @@ A,B,C的价格分别为&yen;2,&yen;3,&yen;4.
5756
<!-- 这里可写当前语言的特殊实现逻辑 -->
5857

5958
```python
60-
59+
class Solution:
60+
def shoppingOffers(self, price: List[int], special: List[List[int]], needs: List[int]) -> int:
61+
def total(price, needs):
62+
return sum(price[i] * needs[i] for i in range(len(needs)))
63+
64+
ans = total(price, needs)
65+
t = []
66+
for offer in special:
67+
t.clear()
68+
for j in range(len(needs)):
69+
if offer[j] > needs[j]:
70+
t.clear()
71+
break
72+
t.append(needs[j] - offer[j])
73+
if t:
74+
ans = min(ans, offer[-1] +
75+
self.shoppingOffers(price, special, t))
76+
return ans
6177
```
6278

6379
### **Java**
6480

6581
<!-- 这里可写当前语言的特殊实现逻辑 -->
6682

6783
```java
84+
class Solution {
85+
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
86+
int ans = total(price, needs);
87+
List<Integer> t = new ArrayList<>();
88+
for (List<Integer> offer : special) {
89+
t.clear();
90+
for (int j = 0; j < needs.size(); ++j) {
91+
if (offer.get(j) > needs.get(j)) {
92+
t.clear();
93+
break;
94+
}
95+
t.add(needs.get(j) - offer.get(j));
96+
}
97+
if (!t.isEmpty()) {
98+
ans = Math.min(ans, offer.get(offer.size() - 1) + shoppingOffers(price, special, t));
99+
}
100+
}
101+
return ans;
102+
}
103+
104+
private int total(List<Integer> price, List<Integer> needs) {
105+
int s = 0;
106+
for (int i = 0; i < price.size(); ++i) {
107+
s += price.get(i) * needs.get(i);
108+
}
109+
return s;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
120+
int ans = total(price, needs);
121+
vector<int> t;
122+
for (auto& offer : special)
123+
{
124+
t.clear();
125+
for (int j = 0; j < needs.size(); ++j)
126+
{
127+
if (offer[j] > needs[j])
128+
{
129+
t.clear();
130+
break;
131+
}
132+
t.push_back(needs[j] - offer[j]);
133+
}
134+
if (!t.empty()) ans = min(ans, offer[offer.size() - 1] + shoppingOffers(price, special, t));
135+
}
136+
return ans;
137+
}
138+
139+
int total(vector<int>& price, vector<int>& needs) {
140+
int s = 0;
141+
for (int i = 0; i < price.size(); ++i) s += price[i] * needs[i];
142+
return s;
143+
}
144+
};
145+
```
68146

147+
### **Go**
148+
149+
```go
150+
func shoppingOffers(price []int, special [][]int, needs []int) int {
151+
total := func(price, needs []int) int {
152+
s := 0
153+
for i := 0; i < len(needs); i++ {
154+
s += price[i] * needs[i]
155+
}
156+
return s
157+
}
158+
159+
min := func(a, b int) int {
160+
if a < b {
161+
return a
162+
}
163+
return b
164+
}
165+
166+
ans := total(price, needs)
167+
var t []int
168+
for _, offer := range special {
169+
t = t[:0]
170+
for j := 0; j < len(needs); j++ {
171+
if offer[j] > needs[j] {
172+
t = t[:0]
173+
break
174+
}
175+
t = append(t, needs[j]-offer[j])
176+
}
177+
if len(t) > 0 {
178+
ans = min(ans, offer[len(offer)-1]+shoppingOffers(price, special, t))
179+
}
180+
}
181+
return ans
182+
}
69183
```
70184

71185
### **...**

solution/0600-0699/0638.Shopping Offers/README_EN.md

+116-2
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,135 @@ You cannot add more items, though only $9 for 2A ,2B and 1C.
4949
<li><code>0 &lt;= special[i][j] &lt;= 50</code></li>
5050
</ul>
5151

52-
5352
## Solutions
5453

5554
<!-- tabs:start -->
5655

5756
### **Python3**
5857

5958
```python
60-
59+
class Solution:
60+
def shoppingOffers(self, price: List[int], special: List[List[int]], needs: List[int]) -> int:
61+
def total(price, needs):
62+
return sum(price[i] * needs[i] for i in range(len(needs)))
63+
64+
ans = total(price, needs)
65+
t = []
66+
for offer in special:
67+
t.clear()
68+
for j in range(len(needs)):
69+
if offer[j] > needs[j]:
70+
t.clear()
71+
break
72+
t.append(needs[j] - offer[j])
73+
if t:
74+
ans = min(ans, offer[-1] +
75+
self.shoppingOffers(price, special, t))
76+
return ans
6177
```
6278

6379
### **Java**
6480

6581
```java
82+
class Solution {
83+
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
84+
int ans = total(price, needs);
85+
List<Integer> t = new ArrayList<>();
86+
for (List<Integer> offer : special) {
87+
t.clear();
88+
for (int j = 0; j < needs.size(); ++j) {
89+
if (offer.get(j) > needs.get(j)) {
90+
t.clear();
91+
break;
92+
}
93+
t.add(needs.get(j) - offer.get(j));
94+
}
95+
if (!t.isEmpty()) {
96+
ans = Math.min(ans, offer.get(offer.size() - 1) + shoppingOffers(price, special, t));
97+
}
98+
}
99+
return ans;
100+
}
101+
102+
private int total(List<Integer> price, List<Integer> needs) {
103+
int s = 0;
104+
for (int i = 0; i < price.size(); ++i) {
105+
s += price.get(i) * needs.get(i);
106+
}
107+
return s;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
118+
int ans = total(price, needs);
119+
vector<int> t;
120+
for (auto& offer : special)
121+
{
122+
t.clear();
123+
for (int j = 0; j < needs.size(); ++j)
124+
{
125+
if (offer[j] > needs[j])
126+
{
127+
t.clear();
128+
break;
129+
}
130+
t.push_back(needs[j] - offer[j]);
131+
}
132+
if (!t.empty()) ans = min(ans, offer[offer.size() - 1] + shoppingOffers(price, special, t));
133+
}
134+
return ans;
135+
}
136+
137+
int total(vector<int>& price, vector<int>& needs) {
138+
int s = 0;
139+
for (int i = 0; i < price.size(); ++i) s += price[i] * needs[i];
140+
return s;
141+
}
142+
};
143+
```
66144

145+
### **Go**
146+
147+
```go
148+
func shoppingOffers(price []int, special [][]int, needs []int) int {
149+
total := func(price, needs []int) int {
150+
s := 0
151+
for i := 0; i < len(needs); i++ {
152+
s += price[i] * needs[i]
153+
}
154+
return s
155+
}
156+
157+
min := func(a, b int) int {
158+
if a < b {
159+
return a
160+
}
161+
return b
162+
}
163+
164+
ans := total(price, needs)
165+
var t []int
166+
for _, offer := range special {
167+
t = t[:0]
168+
for j := 0; j < len(needs); j++ {
169+
if offer[j] > needs[j] {
170+
t = t[:0]
171+
break
172+
}
173+
t = append(t, needs[j]-offer[j])
174+
}
175+
if len(t) > 0 {
176+
ans = min(ans, offer[len(offer)-1]+shoppingOffers(price, special, t))
177+
}
178+
}
179+
return ans
180+
}
67181
```
68182

69183
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int shoppingOffers(vector<int>& price, vector<vector<int>>& special, vector<int>& needs) {
4+
int ans = total(price, needs);
5+
vector<int> t;
6+
for (auto& offer : special)
7+
{
8+
t.clear();
9+
for (int j = 0; j < needs.size(); ++j)
10+
{
11+
if (offer[j] > needs[j])
12+
{
13+
t.clear();
14+
break;
15+
}
16+
t.push_back(needs[j] - offer[j]);
17+
}
18+
if (!t.empty()) ans = min(ans, offer[offer.size() - 1] + shoppingOffers(price, special, t));
19+
}
20+
return ans;
21+
}
22+
23+
int total(vector<int>& price, vector<int>& needs) {
24+
int s = 0;
25+
for (int i = 0; i < price.size(); ++i) s += price[i] * needs[i];
26+
return s;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func shoppingOffers(price []int, special [][]int, needs []int) int {
2+
total := func(price, needs []int) int {
3+
s := 0
4+
for i := 0; i < len(needs); i++ {
5+
s += price[i] * needs[i]
6+
}
7+
return s
8+
}
9+
10+
min := func(a, b int) int {
11+
if a < b {
12+
return a
13+
}
14+
return b
15+
}
16+
17+
ans := total(price, needs)
18+
var t []int
19+
for _, offer := range special {
20+
t = t[:0]
21+
for j := 0; j < len(needs); j++ {
22+
if offer[j] > needs[j] {
23+
t = t[:0]
24+
break
25+
}
26+
t = append(t, needs[j]-offer[j])
27+
}
28+
if len(t) > 0 {
29+
ans = min(ans, offer[len(offer)-1]+shoppingOffers(price, special, t))
30+
}
31+
}
32+
return ans
33+
}

0 commit comments

Comments
 (0)