Skip to content

Commit 358f91d

Browse files
committed
feat: add solutions to lc problem: No.2611
No.2611.Mice and Cheese
1 parent ad16109 commit 358f91d

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

solution/2600-2699/2611.Mice and Cheese/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555

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

58+
**方法一:贪心 + 排序**
59+
60+
我们可以先将所有奶酪分给第二只老鼠,接下来,考虑将其中 $k$ 块奶酪分给第一只老鼠,那么我们应该如何选择这 $k$ 块奶酪呢?显然,将第 $i$ 块奶酪从第二只老鼠分给第一只老鼠,得分的变化量为 $reward1[i] - reward2[i]$,我们希望这个变化量尽可能大,这样才能使得总得分最大。
61+
62+
因此,我们将奶酪按照 `reward1[i] - reward2[i]` 从大到小排序,前 $k$ 块奶酪由第一只老鼠吃掉,剩下的奶酪由第二只老鼠吃掉,即可得到最大得分。
63+
64+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为奶酪的数量。
65+
5866
<!-- tabs:start -->
5967

6068
### **Python3**
@@ -70,6 +78,15 @@ class Solution:
7078
return sum(reward1[i] for i in idx[:k]) + sum(reward2[i] for i in idx[k:])
7179
```
7280

81+
```python
82+
class Solution:
83+
def miceAndCheese(self, reward1: List[int], reward2: List[int], k: int) -> int:
84+
for i, x in enumerate(reward2):
85+
reward1[i] -= x
86+
reward1.sort(reverse=True)
87+
return sum(reward2) + sum(reward1[:k])
88+
```
89+
7390
### **Java**
7491

7592
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -95,6 +112,24 @@ class Solution {
95112
}
96113
```
97114

115+
```java
116+
class Solution {
117+
public int miceAndCheese(int[] reward1, int[] reward2, int k) {
118+
int ans = 0;
119+
int n = reward1.length;
120+
for (int i = 0; i < n; ++i) {
121+
ans += reward2[i];
122+
reward1[i] -= reward2[i];
123+
}
124+
Arrays.sort(reward1);
125+
for (int i = 0; i < k; ++i) {
126+
ans += reward1[n - i - 1];
127+
}
128+
return ans;
129+
}
130+
}
131+
```
132+
98133
### **C++**
99134

100135
```cpp
@@ -117,6 +152,23 @@ public:
117152
};
118153
```
119154
155+
```cpp
156+
class Solution {
157+
public:
158+
int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {
159+
int n = reward1.size();
160+
int ans = 0;
161+
for (int i = 0; i < n; ++i) {
162+
ans += reward2[i];
163+
reward1[i] -= reward2[i];
164+
}
165+
sort(reward1.rbegin(), reward1.rend());
166+
ans += accumulate(reward1.begin(), reward1.begin() + k, 0);
167+
return ans;
168+
}
169+
};
170+
```
171+
120172
### **Go**
121173

122174
```go
@@ -140,6 +192,21 @@ func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) {
140192
}
141193
```
142194

195+
```go
196+
func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) {
197+
for i, x := range reward2 {
198+
ans += x
199+
reward1[i] -= x
200+
}
201+
sort.Ints(reward1)
202+
n := len(reward1)
203+
for i := 0; i < k; i++ {
204+
ans += reward1[n-i-1]
205+
}
206+
return
207+
}
208+
```
209+
143210
### **...**
144211

145212
```

solution/2600-2699/2611.Mice and Cheese/README_EN.md

+59
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ class Solution:
6262
return sum(reward1[i] for i in idx[:k]) + sum(reward2[i] for i in idx[k:])
6363
```
6464

65+
```python
66+
class Solution:
67+
def miceAndCheese(self, reward1: List[int], reward2: List[int], k: int) -> int:
68+
for i, x in enumerate(reward2):
69+
reward1[i] -= x
70+
reward1.sort(reverse=True)
71+
return sum(reward2) + sum(reward1[:k])
72+
```
73+
6574
### **Java**
6675

6776
```java
@@ -85,6 +94,24 @@ class Solution {
8594
}
8695
```
8796

97+
```java
98+
class Solution {
99+
public int miceAndCheese(int[] reward1, int[] reward2, int k) {
100+
int ans = 0;
101+
int n = reward1.length;
102+
for (int i = 0; i < n; ++i) {
103+
ans += reward2[i];
104+
reward1[i] -= reward2[i];
105+
}
106+
Arrays.sort(reward1);
107+
for (int i = 0; i < k; ++i) {
108+
ans += reward1[n - i - 1];
109+
}
110+
return ans;
111+
}
112+
}
113+
```
114+
88115
### **C++**
89116

90117
```cpp
@@ -107,6 +134,23 @@ public:
107134
};
108135
```
109136
137+
```cpp
138+
class Solution {
139+
public:
140+
int miceAndCheese(vector<int>& reward1, vector<int>& reward2, int k) {
141+
int n = reward1.size();
142+
int ans = 0;
143+
for (int i = 0; i < n; ++i) {
144+
ans += reward2[i];
145+
reward1[i] -= reward2[i];
146+
}
147+
sort(reward1.rbegin(), reward1.rend());
148+
ans += accumulate(reward1.begin(), reward1.begin() + k, 0);
149+
return ans;
150+
}
151+
};
152+
```
153+
110154
### **Go**
111155

112156
```go
@@ -130,6 +174,21 @@ func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) {
130174
}
131175
```
132176

177+
```go
178+
func miceAndCheese(reward1 []int, reward2 []int, k int) (ans int) {
179+
for i, x := range reward2 {
180+
ans += x
181+
reward1[i] -= x
182+
}
183+
sort.Ints(reward1)
184+
n := len(reward1)
185+
for i := 0; i < k; i++ {
186+
ans += reward1[n-i-1]
187+
}
188+
return
189+
}
190+
```
191+
133192
### **...**
134193

135194
```

0 commit comments

Comments
 (0)