# Code language: Python classSolution: defmatrixSum(self, nums: List[List[int]]) -> int: nums = [[-a for a in row] for row in nums] for row in nums: heapq.heapify(row) returnsum(max(-heapq.heappop(row) for row in nums) for _ inrange(len(nums[0])))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Code language: Java classSolution { publicintmatrixSum(int[][] nums) { intn= nums.length, m = nums[0].length, ans = 0; PriorityQueue<Integer>[] st = newPriorityQueue[n]; for (inti=0; i < n; ++i) { st[i] = newPriorityQueue<Integer>((a, b) -> b - a); for (intj=0; j < m; ++j) st[i].add(nums[i][j]); } for (inti=0; i < m; ++i) { intcur=0; for (intj=0; j < n; ++j) { cur = Math.max(cur, st[j].poll()); } ans += cur; } return ans; } }
// Code language: C++ classSolution { public: intmatrixSum(vector<vector<int>>& nums){ int n = nums.size(), m = nums[0].size(), ans = 0; vector<priority_queue<int>> st(n, priority_queue<int>()); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) st[i].emplace(nums[i][j]); } for (int i = 0; i < m; ++i) { int cur = 0; for (int j = 0; j < n; ++j) { cur = max(cur, st[j].top()); st[j].pop(); } ans += cur; } return ans; } };