가로랑 세로 0갯수 1갯수 다 만들어두고
해당위치 계산해서 리턴
정답
class Solution {
fun onesMinusZeros(grid: Array<IntArray>): Array<IntArray> {
val oneRowList= MutableList(grid.size){0}
val oneColumnList=MutableList(grid[0].size){0}
val zeroRowList= MutableList(grid.size){0}
val zeroColumnList=MutableList(grid[0].size){0}
val resList= Array(grid.size,{IntArray(grid[0].size,{0})})
for (row in 0 until grid.size){
for (column in 0 until grid[0].size){
if (grid[row][column]==1) {
oneRowList[row]++
oneColumnList[column]++
}
if (grid[row][column]==0) {
zeroRowList[row]++
zeroColumnList[column]++
}
}
}
for (row in 0 until grid.size){
for (column in 0 until grid[0].size){
resList[row][column]=oneRowList[row]+oneColumnList[column]-zeroRowList[row]-zeroColumnList[column]
}
}
//println(resList.map{it.toList()}.toList())
return resList
}
}
'알고리즘' 카테고리의 다른 글
릿코드 242. Valid Anagram 코틀린 (0) | 2023.12.16 |
---|---|
릿코드 1436. Destination City 코틀린 (0) | 2023.12.15 |
릿코드 1582. Special Positions in a Binary Matrix 코틀린 (0) | 2023.12.13 |
릿코드 1464. Maximum Product of Two Elements in an Array 코틀린 (0) | 2023.12.12 |
릿코드 1287. Element Appearing More Than 25% In Sorted Array 코틀린 (0) | 2023.12.11 |