g1701_1800.s1706_where_will_the_ball_fall.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1701_1800.s1706_where_will_the_ball_fall
// #Medium #Array #Dynamic_Programming #Depth_First_Search #Matrix #Simulation
// #Level_2_Day_1_Implementation/Simulation #2023_06_15_Time_270_ms_(67.86%)_Space_45.6_MB_(82.14%)
class Solution {
fun findBall(grid: Array): IntArray {
val m = grid.size
val n = grid[0].size
val res = IntArray(n)
for (j in 0 until n) {
var currentJ = j
var currentI = 0
while (currentJ < n && currentI < m) {
if (grid[currentI][currentJ] == 1) {
currentJ++
if (currentJ < n && grid[currentI][currentJ] == 1) {
currentI++
} else {
break
}
} else {
currentJ--
if (currentJ >= 0 && grid[currentI][currentJ] == -1) {
currentI++
} else {
break
}
}
}
if (currentI == m) {
res[j] = currentJ
} else {
res[j] = -1
}
}
return res
}
}