@@ -45,7 +45,6 @@ A,B,C的价格分别为¥2,¥3,¥4.
45
45
<li>你不可以购买超出待购清单的物品,即使更便宜。</li>
46
46
</ol >
47
47
48
-
49
48
## 解法
50
49
51
50
<!-- 这里可写通用的实现逻辑 -->
@@ -57,15 +56,130 @@ A,B,C的价格分别为¥2,¥3,¥4.
57
56
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
57
59
58
``` 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
61
77
```
62
78
63
79
### ** Java**
64
80
65
81
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
82
67
83
``` 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
+ ```
68
146
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
+ }
69
183
```
70
184
71
185
### ** ...**
0 commit comments