Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1a7073e

Browse files
authored
feat: add solutions to lc problem: No.2931 (doocs#1968)
No.2931.Maximum Spending After Buying Items
1 parent 561fc9f commit 1a7073e

File tree

7 files changed

+306
-6
lines changed

7 files changed

+306
-6
lines changed

solution/2900-2999/2931.Maximum Spending After Buying Items/README.md

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,137 @@
7373

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

76+
**方法一:贪心 + 优先队列**
77+
78+
根据题目描述,我们应该优先选择价值越小的物品,把价值越大的物品留到后面购买,这样才能使得总开销最大。因此,我们使用优先队列(小根堆)存储每个商店中还未购买的最小价值的物品。初始时,我们将每个商店中最右边的物品加入优先队列。
79+
80+
在每一天,我们从优先队列中取出价值最小的物品,将其加入答案,并将该物品所在商店中的上一个物品加入优先队列。我们重复上述操作,直到优先队列为空。
81+
82+
时间复杂度 $O(m \times n \times \log m)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $values$ 的行数和列数。
83+
7684
<!-- tabs:start -->
7785

7886
### **Python3**
7987

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

8290
```python
83-
91+
class Solution:
92+
def maxSpending(self, values: List[List[int]]) -> int:
93+
n = len(values[0])
94+
pq = [(row[-1], i, n - 1) for i, row in enumerate(values)]
95+
heapify(pq)
96+
ans = d = 0
97+
while pq:
98+
d += 1
99+
v, i, j = heappop(pq)
100+
ans += v * d
101+
if j:
102+
heappush(pq, (values[i][j - 1], i, j - 1))
103+
return ans
84104
```
85105

86106
### **Java**
87107

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

90110
```java
91-
111+
class Solution {
112+
public long maxSpending(int[][] values) {
113+
int m = values.length, n = values[0].length;
114+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
115+
for (int i = 0; i < m; ++i) {
116+
pq.offer(new int[] {values[i][n - 1], i, n - 1});
117+
}
118+
long ans = 0;
119+
for (int d = 1; !pq.isEmpty(); ++d) {
120+
var p = pq.poll();
121+
int v = p[0], i = p[1], j = p[2];
122+
ans += (long) v * d;
123+
if (j > 0) {
124+
pq.offer(new int[] {values[i][j - 1], i, j - 1});
125+
}
126+
}
127+
return ans;
128+
}
129+
}
92130
```
93131

94132
### **C++**
95133

96134
```cpp
97-
135+
class Solution {
136+
public:
137+
long long maxSpending(vector<vector<int>>& values) {
138+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
139+
int m = values.size(), n = values[0].size();
140+
for (int i = 0; i < m; ++i) {
141+
pq.emplace(values[i][n - 1], i, n - 1);
142+
}
143+
long long ans = 0;
144+
for (int d = 1; pq.size(); ++d) {
145+
auto [v, i, j] = pq.top();
146+
pq.pop();
147+
ans += 1LL * v * d;
148+
if (j) {
149+
pq.emplace(values[i][j - 1], i, j - 1);
150+
}
151+
}
152+
return ans;
153+
}
154+
};
98155
```
99156
100157
### **Go**
101158
102159
```go
160+
func maxSpending(values [][]int) (ans int64) {
161+
pq := hp{}
162+
n := len(values[0])
163+
for i, row := range values {
164+
heap.Push(&pq, tuple{row[n-1], i, n - 1})
165+
}
166+
for d := 1; len(pq) > 0; d++ {
167+
p := heap.Pop(&pq).(tuple)
168+
ans += int64(p.v * d)
169+
if p.j > 0 {
170+
heap.Push(&pq, tuple{values[p.i][p.j-1], p.i, p.j - 1})
171+
}
172+
}
173+
return
174+
}
175+
176+
type tuple struct{ v, i, j int }
177+
type hp []tuple
178+
179+
func (h hp) Len() int { return len(h) }
180+
func (h hp) Less(i, j int) bool { return h[i].v < h[j].v }
181+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
182+
func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
183+
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
184+
```
103185

186+
### **TypeScript**
187+
188+
```ts
189+
function maxSpending(values: number[][]): number {
190+
const m = values.length;
191+
const n = values[0].length;
192+
const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] });
193+
for (let i = 0; i < m; ++i) {
194+
pq.enqueue([values[i][n - 1], i, n - 1]);
195+
}
196+
197+
let ans = 0;
198+
for (let d = 1; !pq.isEmpty(); ++d) {
199+
const [v, i, j] = pq.dequeue()!;
200+
ans += v * d;
201+
if (j > 0) {
202+
pq.enqueue([values[i][j - 1], i, j - 1]);
203+
}
204+
}
205+
return ans;
206+
}
104207
```
105208

106209
### **...**

solution/2900-2999/2931.Maximum Spending After Buying Items/README_EN.md

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,133 @@ It can be shown that 386 is the maximum amount of money that can be spent buying
6767

6868
## Solutions
6969

70+
**Solution 1: Greedy + Priority Queue**
71+
72+
According to the problem description, we should prioritize purchasing items with smaller values and leave items with larger values to be purchased later in order to maximize the total cost. Therefore, we use a priority queue (min-heap) to store the smallest value item that has not been purchased in each store. Initially, we add the rightmost item in each store to the priority queue.
73+
74+
Each day, we take out the item with the smallest value from the priority queue, add it to the answer, and add the previous item in the store where the item is located to the priority queue. We repeat the above operation until the priority queue is empty.
75+
76+
The time complexity is $O(m \times n \times \log m)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the number of rows and columns of the array $values$, respectively.
77+
7078
<!-- tabs:start -->
7179

7280
### **Python3**
7381

7482
```python
75-
83+
class Solution:
84+
def maxSpending(self, values: List[List[int]]) -> int:
85+
n = len(values[0])
86+
pq = [(row[-1], i, n - 1) for i, row in enumerate(values)]
87+
heapify(pq)
88+
ans = d = 0
89+
while pq:
90+
d += 1
91+
v, i, j = heappop(pq)
92+
ans += v * d
93+
if j:
94+
heappush(pq, (values[i][j - 1], i, j - 1))
95+
return ans
7696
```
7797

7898
### **Java**
7999

80100
```java
81-
101+
class Solution {
102+
public long maxSpending(int[][] values) {
103+
int m = values.length, n = values[0].length;
104+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
105+
for (int i = 0; i < m; ++i) {
106+
pq.offer(new int[] {values[i][n - 1], i, n - 1});
107+
}
108+
long ans = 0;
109+
for (int d = 1; !pq.isEmpty(); ++d) {
110+
var p = pq.poll();
111+
int v = p[0], i = p[1], j = p[2];
112+
ans += (long) v * d;
113+
if (j > 0) {
114+
pq.offer(new int[] {values[i][j - 1], i, j - 1});
115+
}
116+
}
117+
return ans;
118+
}
119+
}
82120
```
83121

84122
### **C++**
85123

86124
```cpp
87-
125+
class Solution {
126+
public:
127+
long long maxSpending(vector<vector<int>>& values) {
128+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
129+
int m = values.size(), n = values[0].size();
130+
for (int i = 0; i < m; ++i) {
131+
pq.emplace(values[i][n - 1], i, n - 1);
132+
}
133+
long long ans = 0;
134+
for (int d = 1; pq.size(); ++d) {
135+
auto [v, i, j] = pq.top();
136+
pq.pop();
137+
ans += 1LL * v * d;
138+
if (j) {
139+
pq.emplace(values[i][j - 1], i, j - 1);
140+
}
141+
}
142+
return ans;
143+
}
144+
};
88145
```
89146
90147
### **Go**
91148
92149
```go
150+
func maxSpending(values [][]int) (ans int64) {
151+
pq := hp{}
152+
n := len(values[0])
153+
for i, row := range values {
154+
heap.Push(&pq, tuple{row[n-1], i, n - 1})
155+
}
156+
for d := 1; len(pq) > 0; d++ {
157+
p := heap.Pop(&pq).(tuple)
158+
ans += int64(p.v * d)
159+
if p.j > 0 {
160+
heap.Push(&pq, tuple{values[p.i][p.j-1], p.i, p.j - 1})
161+
}
162+
}
163+
return
164+
}
165+
166+
type tuple struct{ v, i, j int }
167+
type hp []tuple
168+
169+
func (h hp) Len() int { return len(h) }
170+
func (h hp) Less(i, j int) bool { return h[i].v < h[j].v }
171+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
172+
func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
173+
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
174+
```
93175

176+
### **TypeScript**
177+
178+
```ts
179+
function maxSpending(values: number[][]): number {
180+
const m = values.length;
181+
const n = values[0].length;
182+
const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] });
183+
for (let i = 0; i < m; ++i) {
184+
pq.enqueue([values[i][n - 1], i, n - 1]);
185+
}
186+
187+
let ans = 0;
188+
for (let d = 1; !pq.isEmpty(); ++d) {
189+
const [v, i, j] = pq.dequeue()!;
190+
ans += v * d;
191+
if (j > 0) {
192+
pq.enqueue([values[i][j - 1], i, j - 1]);
193+
}
194+
}
195+
return ans;
196+
}
94197
```
95198

96199
### **...**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
long long maxSpending(vector<vector<int>>& values) {
4+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq;
5+
int m = values.size(), n = values[0].size();
6+
for (int i = 0; i < m; ++i) {
7+
pq.emplace(values[i][n - 1], i, n - 1);
8+
}
9+
long long ans = 0;
10+
for (int d = 1; pq.size(); ++d) {
11+
auto [v, i, j] = pq.top();
12+
pq.pop();
13+
ans += 1LL * v * d;
14+
if (j) {
15+
pq.emplace(values[i][j - 1], i, j - 1);
16+
}
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func maxSpending(values [][]int) (ans int64) {
2+
pq := hp{}
3+
n := len(values[0])
4+
for i, row := range values {
5+
heap.Push(&pq, tuple{row[n-1], i, n - 1})
6+
}
7+
for d := 1; len(pq) > 0; d++ {
8+
p := heap.Pop(&pq).(tuple)
9+
ans += int64(p.v * d)
10+
if p.j > 0 {
11+
heap.Push(&pq, tuple{values[p.i][p.j-1], p.i, p.j - 1})
12+
}
13+
}
14+
return
15+
}
16+
17+
type tuple struct{ v, i, j int }
18+
type hp []tuple
19+
20+
func (h hp) Len() int { return len(h) }
21+
func (h hp) Less(i, j int) bool { return h[i].v < h[j].v }
22+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
23+
func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) }
24+
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public long maxSpending(int[][] values) {
3+
int m = values.length, n = values[0].length;
4+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
5+
for (int i = 0; i < m; ++i) {
6+
pq.offer(new int[] {values[i][n - 1], i, n - 1});
7+
}
8+
long ans = 0;
9+
for (int d = 1; !pq.isEmpty(); ++d) {
10+
var p = pq.poll();
11+
int v = p[0], i = p[1], j = p[2];
12+
ans += (long) v * d;
13+
if (j > 0) {
14+
pq.offer(new int[] {values[i][j - 1], i, j - 1});
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxSpending(self, values: List[List[int]]) -> int:
3+
n = len(values[0])
4+
pq = [(row[-1], i, n - 1) for i, row in enumerate(values)]
5+
heapify(pq)
6+
ans = d = 0
7+
while pq:
8+
d += 1
9+
v, i, j = heappop(pq)
10+
ans += v * d
11+
if j:
12+
heappush(pq, (values[i][j - 1], i, j - 1))
13+
return ans
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function maxSpending(values: number[][]): number {
2+
const m = values.length;
3+
const n = values[0].length;
4+
const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] });
5+
for (let i = 0; i < m; ++i) {
6+
pq.enqueue([values[i][n - 1], i, n - 1]);
7+
}
8+
9+
let ans = 0;
10+
for (let d = 1; !pq.isEmpty(); ++d) {
11+
const [v, i, j] = pq.dequeue()!;
12+
ans += v * d;
13+
if (j > 0) {
14+
pq.enqueue([values[i][j - 1], i, j - 1]);
15+
}
16+
}
17+
return ans;
18+
}

0 commit comments

Comments
 (0)