1 Star 0 Fork 0

catyMap/AlgorithmNote

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
kWeakestRows.go 789 Bytes
Copy Edit Raw Blame History
catyMap authored 2021-08-16 08:30 +08:00 . 新增题库
package main
import (
"fmt"
"sort"
)
func kWeakestRows(mat [][]int, k int) (res []int) {
n, m := len(mat), len(mat[0])
matInfo := make([][2]int, n, n)
for i := 0; i < n; i++ {
soNumber := 0
for j := 0; j < m; j++ {
if mat[i][j] == 1 {
soNumber++
}
}
matInfo[i][0], matInfo[i][1] = soNumber, i
}
sort.Slice(matInfo, func(i, j int) bool {
if matInfo[i][0] == matInfo[j][0] {
return matInfo[i][1] < matInfo[j][1]
}
return matInfo[i][0] < matInfo[j][0]
})
subMat := matInfo[:k]
for _, v := range subMat {
res = append(res, v[1])
}
return res
}
func main() {
fmt.Println(kWeakestRows([][]int{{1, 1, 0}, {1, 1, 0}, {1, 1, 1}, {1, 1, 1}, {0, 0, 0}, {1, 1, 1}, {1, 0, 0}}, 6))
}
/*
{1,1,0},
{1,1,0},
{1,1,1},
{1,1,1},
{0,0,0},
{1,1,1},
{1,0,0},
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dogemap/algorithm-note.git
git@gitee.com:dogemap/algorithm-note.git
dogemap
algorithm-note
AlgorithmNote
dc486f96f6c1

Search