Skip to content

Commit f6ef05b

Browse files
committed
feat: update solutions to lc problems: No.1774,1799
* No.1774.Closest Dessert Cost * No.1799.Maximize Score After N Operations
1 parent 13ee900 commit f6ef05b

File tree

14 files changed

+140
-119
lines changed

14 files changed

+140
-119
lines changed

solution/0800-0899/0895.Maximum Frequency Stack/Solution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class FreqStack:
2-
32
def __init__(self):
43
self.cnt = defaultdict(int)
54
self.d = defaultdict(list)

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
**方法一:枚举子集和 + 排序 + 二分查找**
8888

89-
每种类型的配料最多可以选两份,因此,我们可以复制一份配料,然后利用 `DFS` 枚举子集之和。在实现上,我们可以只枚举一半的配料,然后在另一半配料中利用二分查找找到最接近的配料
89+
每种类型的配料最多可以选两份,因此,我们可以复制一份配料,然后利用 `DFS` 枚举子集之和。在实现上,我们可以只枚举一半的配料的所有子集和,然后在另一半配料子集和中,利用二分查找找到最接近的配料
9090

9191
时间复杂度 $O(n\times 2^m \times \log {2^m})$。
9292

@@ -112,8 +112,12 @@ class Solution:
112112
dfs(0, 0)
113113
arr.sort()
114114
d = ans = inf
115+
116+
# 选择一种冰激淋基料
115117
for x in baseCosts:
118+
# 枚举子集和
116119
for y in arr:
120+
# 二分查找
117121
i = bisect_left(arr, target - x - y)
118122
for j in (i, i - 1):
119123
if 0 <= j < len(arr):
@@ -132,15 +136,19 @@ class Solution:
132136
class Solution {
133137
private List<Integer> arr = new ArrayList<>();
134138
private int[] ts;
135-
private int inf = 0x3f3f3f3f;
139+
private int inf = 1 << 30;
136140

137141
public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
138142
ts = toppingCosts;
139143
dfs(0, 0);
140144
Collections.sort(arr);
141145
int d = inf, ans = inf;
146+
147+
// 选择一种冰激淋基料
142148
for (int x : baseCosts) {
149+
// 枚举子集和
143150
for (int y : arr) {
151+
// 二分查找
144152
int i = search(target - x - y);
145153
for (int j : new int[] {i, i - 1}) {
146154
if (j >= 0 && j < arr.size()) {
@@ -185,7 +193,7 @@ class Solution {
185193
```cpp
186194
class Solution {
187195
public:
188-
const int inf = 0x3f3f3f3f;
196+
const int inf = INT_MAX;
189197
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
190198
vector<int> arr;
191199
function<void(int, int)> dfs = [&](int i, int t) {
@@ -199,8 +207,11 @@ public:
199207
dfs(0, 0);
200208
sort(arr.begin(), arr.end());
201209
int d = inf, ans = inf;
210+
// 选择一种冰激淋基料
202211
for (int x : baseCosts) {
212+
// 枚举子集和
203213
for (int y : arr) {
214+
// 二分查找
204215
int i = lower_bound(arr.begin(), arr.end(), target - x - y) - arr.begin();
205216
for (int j = i - 1; j < i + 1; ++j) {
206217
if (j >= 0 && j < arr.size()) {
@@ -234,9 +245,13 @@ func closestCost(baseCosts []int, toppingCosts []int, target int) int {
234245
}
235246
dfs(0, 0)
236247
sort.Ints(arr)
237-
ans, d := math.MaxInt32, math.MaxInt32
248+
const inf = 1 << 30
249+
ans, d := inf, inf
250+
// 选择一种冰激淋基料
238251
for _, x := range baseCosts {
252+
// 枚举子集和
239253
for _, y := range arr {
254+
// 二分查找
240255
i := sort.Search(len(arr), func(i int) bool { return arr[i] >= target-x-y })
241256
for j := i - 1; j < i+1; j++ {
242257
if j >= 0 && j < len(arr) {

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

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Solution:
107107
class Solution {
108108
private List<Integer> arr = new ArrayList<>();
109109
private int[] ts;
110-
private int inf = 0x3f3f3f3f;
110+
private int inf = 1 << 30;
111111

112112
public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
113113
ts = toppingCosts;
@@ -160,7 +160,7 @@ class Solution {
160160
```cpp
161161
class Solution {
162162
public:
163-
const int inf = 0x3f3f3f3f;
163+
const int inf = INT_MAX;
164164
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
165165
vector<int> arr;
166166
function<void(int, int)> dfs = [&](int i, int t) {
@@ -197,41 +197,42 @@ public:
197197
198198
```go
199199
func closestCost(baseCosts []int, toppingCosts []int, target int) int {
200-
arr := []int{}
201-
var dfs func(int, int)
202-
dfs = func(i, t int) {
203-
if i >= len(toppingCosts) {
204-
arr = append(arr, t)
205-
return
206-
}
207-
dfs(i+1, t)
208-
dfs(i+1, t+toppingCosts[i])
209-
}
210-
dfs(0, 0)
211-
sort.Ints(arr)
212-
ans, d := math.MaxInt32, math.MaxInt32
213-
for _, x := range baseCosts {
214-
for _, y := range arr {
215-
i := sort.Search(len(arr), func(i int) bool { return arr[i] >= target-x-y })
216-
for j := i - 1; j < i+1; j++ {
217-
if j >= 0 && j < len(arr) {
218-
t := abs(x + y + arr[j] - target)
219-
if d > t || (d == t && ans > x+y+arr[j]) {
220-
d = t
221-
ans = x + y + arr[j]
222-
}
223-
}
224-
}
225-
}
226-
}
227-
return ans
200+
arr := []int{}
201+
var dfs func(int, int)
202+
dfs = func(i, t int) {
203+
if i >= len(toppingCosts) {
204+
arr = append(arr, t)
205+
return
206+
}
207+
dfs(i + 1, t)
208+
dfs(i + 1, t + toppingCosts[i])
209+
}
210+
dfs(0, 0)
211+
sort.Ints(arr)
212+
const inf = 1 << 30
213+
ans, d := inf, inf
214+
for _, x := range baseCosts {
215+
for _, y := range arr {
216+
i := sort.Search(len(arr), func(i int) bool { return arr[i] >= target - x - y })
217+
for j := i - 1; j < i + 1; j++ {
218+
if j >= 0 && j < len(arr) {
219+
t := abs(x + y + arr[j] - target)
220+
if d > t || (d == t && ans > x + y + arr[j]) {
221+
d = t
222+
ans = x + y + arr[j]
223+
}
224+
}
225+
}
226+
}
227+
}
228+
return ans
228229
}
229230
230231
func abs(x int) int {
231-
if x < 0 {
232-
return -x
233-
}
234-
return x
232+
if x < 0 {
233+
return -x
234+
}
235+
return x
235236
}
236237
```
237238

solution/1700-1799/1774.Closest Dessert Cost/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution {
22
public:
3-
const int inf = 0x3f3f3f3f;
3+
const int inf = INT_MAX;
44
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
55
vector<int> arr;
66
function<void(int, int)> dfs = [&](int i, int t) {

solution/1700-1799/1774.Closest Dessert Cost/Solution.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ func closestCost(baseCosts []int, toppingCosts []int, target int) int {
1111
}
1212
dfs(0, 0)
1313
sort.Ints(arr)
14-
ans, d := math.MaxInt32, math.MaxInt32
14+
const inf = 1 << 30
15+
ans, d := inf, inf
1516
for _, x := range baseCosts {
1617
for _, y := range arr {
1718
i := sort.Search(len(arr), func(i int) bool { return arr[i] >= target-x-y })

solution/1700-1799/1774.Closest Dessert Cost/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
private List<Integer> arr = new ArrayList<>();
33
private int[] ts;
4-
private int inf = 0x3f3f3f3f;
4+
private int inf = 1 << 30;
55

66
public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
77
ts = toppingCosts;

solution/1700-1799/1797.Design Authentication Manager/README_EN.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,18 @@ class AuthenticationManager {
9494
public AuthenticationManager(int timeToLive) {
9595
t = timeToLive;
9696
}
97-
97+
9898
public void generate(String tokenId, int currentTime) {
9999
d.put(tokenId, currentTime + t);
100100
}
101-
101+
102102
public void renew(String tokenId, int currentTime) {
103103
if (d.getOrDefault(tokenId, 0) <= currentTime) {
104104
return;
105105
}
106106
generate(tokenId, currentTime);
107107
}
108-
108+
109109
public int countUnexpiredTokens(int currentTime) {
110110
int ans = 0;
111111
for (int exp : d.values()) {
@@ -134,16 +134,16 @@ public:
134134
AuthenticationManager(int timeToLive) {
135135
t = timeToLive;
136136
}
137-
137+
138138
void generate(string tokenId, int currentTime) {
139139
d[tokenId] = currentTime + t;
140140
}
141-
141+
142142
void renew(string tokenId, int currentTime) {
143143
if (d[tokenId] <= currentTime) return;
144144
generate(tokenId, currentTime);
145145
}
146-
146+
147147
int countUnexpiredTokens(int currentTime) {
148148
int ans = 0;
149149
for (auto& [_, v] : d) ans += v > currentTime;

solution/1700-1799/1797.Design Authentication Manager/Solution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class AuthenticationManager:
2-
32
def __init__(self, timeToLive: int):
43
self.t = timeToLive
54
self.d = defaultdict(int)

0 commit comments

Comments
 (0)