Re: [闲聊] 每日leetcode

楼主: JIWP (JIWP)   2025-01-20 21:38:11
2661. First Completely Painted Row or Column
思路:
就记录每个数字所在的行、列
接着去遍历arr
看哪一行、哪一列先凑齐
没什么难度
golang code :
func firstCompleteIndex(arr []int, mat [][]int) int {
n, m := len(mat), len(mat[0])
rec := make([][2]int, len(arr)+1)
cnt := make([]int, n+m)
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
rec[mat[i][j]] = [2]int{i, j + n}
}
}
for key, val := range arr {
cnt[rec[val][0]]++
cnt[rec[val][1]]++
if cnt[rec[val][0]] == m || cnt[rec[val][1]] == n {
return key
}
}
return -1
}
作者: sustainer123 (caster)   2024-01-20 21:38:00
大师
作者: DJYOMIYAHINA (通通打死)   2025-01-20 21:43:00
卷爽没

Links booklink

Contact Us: admin [ a t ] ucptt.com