Skip to content

Commit ca5cef3

Browse files
committed
feat: add solutions to lc problem: No.1774
No.1774.Closest Dessert Cost
1 parent 03d8307 commit ca5cef3

File tree

11 files changed

+462
-16
lines changed

11 files changed

+462
-16
lines changed

solution/1700-1799/1755.Closest Subsequence Sum/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060

6161
时间复杂度 $O(n\times 2^{n/2})$。
6262

63+
相似题目:[1774. 最接近目标价格的甜点成本](/solution/1700-1799/1774.Closest%20Dessert%20Cost/README.md)
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**

solution/1700-1799/1773.Count Items Matching a Rule/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252

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

55-
**方法一:直接模拟**
55+
**方法一:计数模拟**
5656

57-
由于 `ruleKey` 只可能是 `"type"``"color"``"name"`,我们可以直接取 `ruleKey` 的第一个字符 $x$ 来确定 `item` 的下标 $i$。然后遍历 `items` 数组,统计 `item[i] == ruleValue` 的个数即可。
57+
由于 `ruleKey` 只可能是 `"type"``"color"``"name"`,我们可以直接取 `ruleKey` 的第一个字符来确定 `item` 的下标 $i$。然后遍历 `items` 数组,统计 `item[i] == ruleValue` 的个数即可。
5858

5959
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为 `items` 的长度。
6060

@@ -78,8 +78,7 @@ class Solution:
7878
```java
7979
class Solution {
8080
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
81-
char x = ruleKey.charAt(0);
82-
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
81+
int i = ruleKey.charAt(0) == 't' ? 0 : (ruleKey.charAt(0) == 'c' ? 1 : 2);
8382
int ans = 0;
8483
for (var v : items) {
8584
if (v.get(i).equals(ruleValue)) {
@@ -97,8 +96,7 @@ class Solution {
9796
class Solution {
9897
public:
9998
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
100-
char x = ruleKey[0];
101-
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
99+
int i = ruleKey[0] == 't' ? 0 : (ruleKey[0] == 'c' ? 1 : 2);
102100
return count_if(items.begin(), items.end(), [&](auto& v) { return v[i] == ruleValue; });
103101
}
104102
};

solution/1700-1799/1773.Count Items Matching a Rule/README_EN.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ class Solution:
6060
```java
6161
class Solution {
6262
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
63-
char x = ruleKey.charAt(0);
64-
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
63+
int i = ruleKey.charAt(0) == 't' ? 0 : (ruleKey.charAt(0) == 'c' ? 1 : 2);
6564
int ans = 0;
6665
for (var v : items) {
6766
if (v.get(i).equals(ruleValue)) {
@@ -79,8 +78,7 @@ class Solution {
7978
class Solution {
8079
public:
8180
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
82-
char x = ruleKey[0];
83-
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
81+
int i = ruleKey[0] == 't' ? 0 : (ruleKey[0] == 'c' ? 1 : 2);
8482
return count_if(items.begin(), items.end(), [&](auto& v) { return v[i] == ruleValue; });
8583
}
8684
};
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution {
22
public:
33
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) {
4-
char x = ruleKey[0];
5-
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
4+
int i = ruleKey[0] == 't' ? 0 : (ruleKey[0] == 'c' ? 1 : 2);
65
return count_if(items.begin(), items.end(), [&](auto& v) { return v[i] == ruleValue; });
76
}
87
};

solution/1700-1799/1773.Count Items Matching a Rule/Solution.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution {
22
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
3-
char x = ruleKey.charAt(0);
4-
int i = x == 't' ? 0 : (x == 'c' ? 1 : 2);
3+
int i = ruleKey.charAt(0) == 't' ? 0 : (ruleKey.charAt(0) == 'c' ? 1 : 2);
54
int ans = 0;
65
for (var v : items) {
76
if (v.get(i).equals(ruleValue)) {

solution/1700-1799/1774.Closest Dessert Cost/README.md

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,180 @@
8484

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

87+
**方法一:枚举子集和 + 排序 + 二分查找**
88+
89+
每种类型的配料最多可以选两份,因此,我们可以复制一份配料,然后利用 `DFS` 枚举子集之和。在实现上,我们可以只枚举一半的配料,然后在另一半配料中利用二分查找找到最接近的配料。
90+
91+
时间复杂度 $O(n\times 2^m \times \log {2^m})$。
92+
93+
相似题目:[1755. 最接近目标值的子序列和](/solution/1700-1799/1755.Closest%20Subsequence%20Sum/README.md)
94+
8795
<!-- tabs:start -->
8896

8997
### **Python3**
9098

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

93101
```python
94-
102+
class Solution:
103+
def closestCost(self, baseCosts: List[int], toppingCosts: List[int], target: int) -> int:
104+
def dfs(i, t):
105+
if i >= len(toppingCosts):
106+
arr.append(t)
107+
return
108+
dfs(i + 1, t)
109+
dfs(i + 1, t + toppingCosts[i])
110+
111+
arr = []
112+
dfs(0, 0)
113+
arr.sort()
114+
d = ans = inf
115+
for x in baseCosts:
116+
for y in arr:
117+
i = bisect_left(arr, target - x - y)
118+
for j in (i, i - 1):
119+
if 0 <= j < len(arr):
120+
t = abs(x + y + arr[j] - target)
121+
if d > t or (d == t and ans > x + y + arr[j]):
122+
d = t
123+
ans = x + y + arr[j]
124+
return ans
95125
```
96126

97127
### **Java**
98128

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

101131
```java
132+
class Solution {
133+
private List<Integer> arr = new ArrayList<>();
134+
private int[] ts;
135+
private int inf = 0x3f3f3f3f;
136+
137+
public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
138+
ts = toppingCosts;
139+
dfs(0, 0);
140+
Collections.sort(arr);
141+
int d = inf, ans = inf;
142+
for (int x : baseCosts) {
143+
for (int y : arr) {
144+
int i = search(target - x - y);
145+
for (int j : new int[] {i, i - 1}) {
146+
if (j >= 0 && j < arr.size()) {
147+
int t = Math.abs(x + y + arr.get(j) - target);
148+
if (d > t || (d == t && ans > x + y + arr.get(j))) {
149+
d = t;
150+
ans = x + y + arr.get(j);
151+
}
152+
}
153+
}
154+
}
155+
}
156+
return ans;
157+
}
158+
159+
private int search(int x) {
160+
int left = 0, right = arr.size();
161+
while (left < right) {
162+
int mid = (left + right) >> 1;
163+
if (arr.get(mid) >= x) {
164+
right = mid;
165+
} else {
166+
left = mid + 1;
167+
}
168+
}
169+
return left;
170+
}
171+
172+
private void dfs(int i, int t) {
173+
if (i >= ts.length) {
174+
arr.add(t);
175+
return;
176+
}
177+
dfs(i + 1, t);
178+
dfs(i + 1, t + ts[i]);
179+
}
180+
}
181+
```
182+
183+
### **C++**
184+
185+
```cpp
186+
class Solution {
187+
public:
188+
const int inf = 0x3f3f3f3f;
189+
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
190+
vector<int> arr;
191+
function<void(int, int)> dfs = [&](int i, int t) {
192+
if (i >= toppingCosts.size()) {
193+
arr.push_back(t);
194+
return;
195+
}
196+
dfs(i + 1, t);
197+
dfs(i + 1, t + toppingCosts[i]);
198+
};
199+
dfs(0, 0);
200+
sort(arr.begin(), arr.end());
201+
int d = inf, ans = inf;
202+
for (int x : baseCosts) {
203+
for (int y : arr) {
204+
int i = lower_bound(arr.begin(), arr.end(), target - x - y) - arr.begin();
205+
for (int j = i - 1; j < i + 1; ++j) {
206+
if (j >= 0 && j < arr.size()) {
207+
int t = abs(x + y + arr[j] - target);
208+
if (d > t || (d == t && ans > x + y + arr[j])) {
209+
d = t;
210+
ans = x + y + arr[j];
211+
}
212+
}
213+
}
214+
}
215+
}
216+
return ans;
217+
}
218+
};
219+
```
102220
221+
### **Go**
222+
223+
```go
224+
func closestCost(baseCosts []int, toppingCosts []int, target int) int {
225+
arr := []int{}
226+
var dfs func(int, int)
227+
dfs = func(i, t int) {
228+
if i >= len(toppingCosts) {
229+
arr = append(arr, t)
230+
return
231+
}
232+
dfs(i+1, t)
233+
dfs(i+1, t+toppingCosts[i])
234+
}
235+
dfs(0, 0)
236+
sort.Ints(arr)
237+
ans, d := math.MaxInt32, math.MaxInt32
238+
for _, x := range baseCosts {
239+
for _, y := range arr {
240+
i := sort.Search(len(arr), func(i int) bool { return arr[i] >= target-x-y })
241+
for j := i - 1; j < i+1; j++ {
242+
if j >= 0 && j < len(arr) {
243+
t := abs(x + y + arr[j] - target)
244+
if d > t || (d == t && ans > x+y+arr[j]) {
245+
d = t
246+
ans = x + y + arr[j]
247+
}
248+
}
249+
}
250+
}
251+
}
252+
return ans
253+
}
254+
255+
func abs(x int) int {
256+
if x < 0 {
257+
return -x
258+
}
259+
return x
260+
}
103261
```
104262

105263
### **...**

0 commit comments

Comments
 (0)