-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add solutions to lc problem: No.2931 #1968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
solution/2900-2999/2931.Maximum Spending After Buying Items/Solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution { | ||
public: | ||
long long maxSpending(vector<vector<int>>& values) { | ||
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq; | ||
int m = values.size(), n = values[0].size(); | ||
for (int i = 0; i < m; ++i) { | ||
pq.emplace(values[i][n - 1], i, n - 1); | ||
} | ||
long long ans = 0; | ||
for (int d = 1; pq.size(); ++d) { | ||
auto [v, i, j] = pq.top(); | ||
pq.pop(); | ||
ans += 1LL * v * d; | ||
if (j) { | ||
pq.emplace(values[i][j - 1], i, j - 1); | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
24 changes: 24 additions & 0 deletions
solution/2900-2999/2931.Maximum Spending After Buying Items/Solution.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
func maxSpending(values [][]int) (ans int64) { | ||
pq := hp{} | ||
n := len(values[0]) | ||
for i, row := range values { | ||
heap.Push(&pq, tuple{row[n-1], i, n - 1}) | ||
} | ||
for d := 1; len(pq) > 0; d++ { | ||
p := heap.Pop(&pq).(tuple) | ||
ans += int64(p.v * d) | ||
if p.j > 0 { | ||
heap.Push(&pq, tuple{values[p.i][p.j-1], p.i, p.j - 1}) | ||
} | ||
} | ||
return | ||
} | ||
|
||
type tuple struct{ v, i, j int } | ||
type hp []tuple | ||
|
||
func (h hp) Len() int { return len(h) } | ||
func (h hp) Less(i, j int) bool { return h[i].v < h[j].v } | ||
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } | ||
func (h *hp) Push(v any) { *h = append(*h, v.(tuple)) } | ||
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } |
19 changes: 19 additions & 0 deletions
solution/2900-2999/2931.Maximum Spending After Buying Items/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public long maxSpending(int[][] values) { | ||
int m = values.length, n = values[0].length; | ||
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); | ||
for (int i = 0; i < m; ++i) { | ||
pq.offer(new int[] {values[i][n - 1], i, n - 1}); | ||
} | ||
long ans = 0; | ||
for (int d = 1; !pq.isEmpty(); ++d) { | ||
var p = pq.poll(); | ||
int v = p[0], i = p[1], j = p[2]; | ||
ans += (long) v * d; | ||
if (j > 0) { | ||
pq.offer(new int[] {values[i][j - 1], i, j - 1}); | ||
} | ||
} | ||
return ans; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
solution/2900-2999/2931.Maximum Spending After Buying Items/Solution.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def maxSpending(self, values: List[List[int]]) -> int: | ||
n = len(values[0]) | ||
pq = [(row[-1], i, n - 1) for i, row in enumerate(values)] | ||
heapify(pq) | ||
ans = d = 0 | ||
while pq: | ||
d += 1 | ||
v, i, j = heappop(pq) | ||
ans += v * d | ||
if j: | ||
heappush(pq, (values[i][j - 1], i, j - 1)) | ||
return ans |
18 changes: 18 additions & 0 deletions
solution/2900-2999/2931.Maximum Spending After Buying Items/Solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function maxSpending(values: number[][]): number { | ||
const m = values.length; | ||
const n = values[0].length; | ||
const pq = new PriorityQueue({ compare: (a, b) => a[0] - b[0] }); | ||
for (let i = 0; i < m; ++i) { | ||
pq.enqueue([values[i][n - 1], i, n - 1]); | ||
} | ||
|
||
let ans = 0; | ||
for (let d = 1; !pq.isEmpty(); ++d) { | ||
const [v, i, j] = pq.dequeue()!; | ||
ans += v * d; | ||
if (j > 0) { | ||
pq.enqueue([values[i][j - 1], i, j - 1]); | ||
} | ||
} | ||
return ans; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.