-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2679-SumInAMatrix.go
More file actions
79 lines (69 loc) · 2.51 KB
/
2679-SumInAMatrix.go
File metadata and controls
79 lines (69 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
// 2679. Sum in a Matrix
// You are given a 0-indexed 2D integer array nums.
// Initially, your score is 0. Perform the following operations until the matrix becomes empty:
// 1. From each row in the matrix, select the largest number and remove it.
// In the case of a tie, it does not matter which number is chosen.
// 2. Identify the highest number amongst all those removed in step 1.
// Add that number to your score.
// Return the final score.
// Example 1:
// Input: nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
// Output: 15
// Explanation: In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15.
// Example 2:
// Input: nums = [[1]]
// Output: 1
// Explanation: We remove 1 and add it to the answer. We return 1.
// Constraints:
// 1 <= nums.length <= 300
// 1 <= nums[i].length <= 500
// 0 <= nums[i][j] <= 10^3
import "fmt"
import "sort"
func matrixSum(nums [][]int) int {
res, n, m := 0, len(nums), len(nums[0])
for i := 0; i < n; i++ {
sort.Ints(nums[i])
}
max := func (x, y int) int { if x > y { return x; }; return y; }
for j := 0; j < m; j++ {
sum := 0
for i := 0; i < n; i++ {
sum = max(sum, nums[i][j])
}
res += sum
}
return res
}
func matrixSum1(nums [][]int) int {
for _, row := range nums {
sort.Ints(row)
}
res, col, row := 0, len(nums[0]) - 1, len(nums)
for col >= 0 {
mx := nums[0][col]
for i := 0; i < row; i++ {
if nums[i][col] > mx {
mx = nums[i][col]
}
}
res += mx
col--
}
return res
}
func main() {
// Example 1:
// Input: nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
// Output: 15
// Explanation: In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15.
fmt.Println(matrixSum([][]int{{7,2,1},{6,4,2},{6,5,3},{3,2,1}})) // 15
// Example 2:
// Input: nums = [[1]]
// Output: 1
// Explanation: We remove 1 and add it to the answer. We return 1.
fmt.Println(matrixSum([][]int{{1}})) // 1
fmt.Println(matrixSum1([][]int{{7,2,1},{6,4,2},{6,5,3},{3,2,1}})) // 15
fmt.Println(matrixSum1([][]int{{1}})) // 1
}