classSolution { public: intmatrixSum(vector<vector<int>>& nums){ int res = 0; int m = nums.size(); int n = nums[0].size(); vector<priority_queue<int>> pq(m); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { pq[i].emplace(nums[i][j]); } } for (int j = 0; j < n; j++) { int maxVal = 0; for (int i = 0; i < m; i++) { maxVal = max(maxVal, pq[i].top()); pq[i].pop(); } res += maxVal; } return res; } };
publicclassSolution { publicintMatrixSum(int[][] nums) { int res = 0; int m = nums.Length; int n = nums[0].Length; PriorityQueue<int, int>[] pq = new PriorityQueue<int, int>[m]; for (int i = 0; i < m; i++) { pq[i] = new PriorityQueue<int, int>(); for (int j = 0; j < n; j++) { pq[i].Enqueue(nums[i][j], -nums[i][j]); } } for (int j = 0; j < n; j++) { int maxVal = 0; for (int i = 0; i < m; i++) { maxVal = Math.Max(maxVal, pq[i].Dequeue()); } res += maxVal; } return res; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defmatrixSum(self, nums: List[List[int]]) -> int: res = 0 m = len(nums) n = len(nums[0]) pq = [] for i inrange(m): pq.append([]) for j inrange(n): heapq.heappush(pq[i], -nums[i][j]) for j inrange(n): maxVal = 0 for i inrange(m): maxVal = max(maxVal, -heapq.heappop(pq[i])) res += maxVal return res
classSolution { public: intmatrixSum(vector<vector<int>>& nums){ int res = 0; int m = nums.size(); int n = nums[0].size(); for (int i = 0; i < m; i++) { sort(nums[i].begin(), nums[i].end()); } for (int j = 0; j < n; j++) { int maxVal = 0; for (int i = 0; i < m; i++) { maxVal = max(maxVal, nums[i][j]); } res += maxVal; } return res; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicintmatrixSum(int[][] nums) { intres=0; intm= nums.length; intn= nums[0].length; for (inti=0; i < m; i++) { Arrays.sort(nums[i]); } for (intj=0; j < n; j++) { intmaxVal=0; for (inti=0; i < m; i++) { maxVal = Math.max(maxVal, nums[i][j]); } res += maxVal; } return res; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { publicintMatrixSum(int[][] nums) { int res = 0; int m = nums.Length; int n = nums[0].Length; for (int i = 0; i < m; i++) { Array.Sort(nums[i]); } for (int j = 0; j < n; j++) { int maxVal = 0; for (int i = 0; i < m; i++) { maxVal = Math.Max(maxVal, nums[i][j]); } res += maxVal; } return res; } }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defmatrixSum(self, nums: List[List[int]]) -> int: res = 0 m = len(nums) n = len(nums[0]) for i inrange(m): nums[i].sort() for j inrange(n): max_val = 0 for i inrange(m): max_val = max(max_val, nums[i][j]) res += max_val return res
[sol2-Go]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
funcmatrixSum(nums [][]int)int { res := 0 m := len(nums) n := len(nums[0]) for i := 0; i < m; i++ { sort.Ints(nums[i]) } for j := 0; j < n; j++ { maxVal := 0 for i := 0; i < m; i++ { if nums[i][j] > maxVal { maxVal = nums[i][j] } } res += maxVal } return res }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var matrixSum = function(nums) { let res = 0; let m = nums.length; let n = nums[0].length; for (let i = 0; i < m; i++) { nums[i].sort((a, b) => a - b); } for (let j = 0; j < n; j++) { let maxVal = 0; for (let i = 0; i < m; i++) { maxVal = Math.max(maxVal, nums[i][j]); } res += maxVal; } return res; }
intmatrixSum(int** nums, int numsSize, int* numsColSize) { int res = 0; int m = numsSize; int n = numsColSize[0]; for (int i = 0; i < m; i++) { qsort(nums[i], n, sizeof(int), cmp); } for (int j = 0; j < n; j++) { int maxVal = 0; for (int i = 0; i < m; i++) { maxVal = fmax(maxVal, nums[i][j]); } res += maxVal; } return res; }
复杂度分析
时间复杂度:O(mn \log n),其中 m,n 分别为矩阵的行数与列数。对矩阵中一行元素进行排序需要的时间复杂度为 n \log n,一共有 m 行,因此矩阵所有行排序的时间复杂度为 O(mn \log n),遍历矩阵中的所有元素需要的时间复杂度为 O(mn),因此总的时间复杂度为 O(mn \log n + mn) = O(mn \log n)。