Leetcode Note: Go - The K Weakest Rows in a Matrix
The K Weakest Rows in a Matrix - LeetCode
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/
- Go 言語で取り組んだメモ
回答
[Python/Go] 🌟 5️⃣ Different Solutions and Explanations 💕 - The K Weakest Rows in a Matrix - LeetCode
https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/solutions/1888004/python-go-5-different-solutions-and-explanations/
// Linear Search, Sort O(M*N + MlogM) ) | O(M)
func kWeakestRows(mat [][]int, k int) []int {
countRow := make([][]int, 0)
for rowIdx, row := range(mat){
count := 0
for _, num := range(row){
count += num
}
countRow = append(countRow, []int{rowIdx, count})
}
sort.Slice(countRow, func(i, j int)bool{
if countRow[i][1] == countRow[j][1]{
return countRow[i][0] < countRow[j][0]
}
return countRow[i][1] < countRow[j][1]
})
res := make([]int, k)
for i:=0; i < k; i++{
res[i] = countRow[i][0]
}
return res
}