-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2718-SumOfMatrixAfterQueries.go
More file actions
73 lines (64 loc) · 2.78 KB
/
2718-SumOfMatrixAfterQueries.go
File metadata and controls
73 lines (64 loc) · 2.78 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
package main
// 2718. Sum of Matrix After Queries
// You are given an integer n and a 0-indexed 2D array queries where queries[i] = [typei, indexi, vali].
// Initially, there is a 0-indexed n x n matrix filled with 0's.
// For each query, you must apply one of the following changes:
// if typei == 0, set the values in the row with indexi to vali, overwriting any previous values.
// if typei == 1, set the values in the column with indexi to vali, overwriting any previous values.
// Return the sum of integers in the matrix after all queries are applied.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2023/05/11/exm1.png" />
// Input: n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]
// Output: 23
// Explanation: The image above describes the matrix after each query.
// The sum of the matrix after all queries are applied is 23.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2023/05/11/exm2.png" />
// Input: n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]
// Output: 17
// Explanation: The image above describes the matrix after each query.
// The sum of the matrix after all queries are applied is 17.
// Constraints:
// 1 <= n <= 10^4
// 1 <= queries.length <= 5 * 10^4
// queries[i].length == 3
// 0 <= typei <= 1
// 0 <= indexi < n
// 0 <= vali <= 10^5
import "fmt"
func matrixSumQueries(n int, queries [][]int) int64 {
res, row,col, m := 0, n, n, len(queries)
rowval,colval := make([]bool,n), make([]bool,n)
for i := 0; i < n; i++ {
rowval[i], colval[i] = false, false
}
for i := m-1; i >= 0; i-- {
if queries[i][0] == 0 && rowval[queries[i][1]] == false {
res += queries[i][2] * col
rowval[queries[i][1]] = true
row--
}
if queries[i][0] ==1 && colval[queries[i][1]] == false {
res += queries[i][2] * row
colval[queries[i][1]] = true
col--
}
}
return int64(res)
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2023/05/11/exm1.png" />
// Input: n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]
// Output: 23
// Explanation: The image above describes the matrix after each query.
// The sum of the matrix after all queries are applied is 23.
fmt.Println(matrixSumQueries(3, [][]int{{0,0,1},{1,2,2},{0,2,3},{1,0,4}})) // 23
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2023/05/11/exm2.png" />
// Input: n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]
// Output: 17
// Explanation: The image above describes the matrix after each query.
// The sum of the matrix after all queries are applied is 17.
fmt.Println(matrixSumQueries(3, [][]int{{0,0,4},{0,1,2},{1,0,1},{0,2,3},{1,2,1}})) // 17
}