Problem : https://leetcode.com/problems/best-sightseeing-pair/
Keep tracking the maximum score we encountered. Decrease the maximum score while iterating to right side.
class Solution {
fun maxScoreSightseeingPair(values: IntArray): Int {
var mxScore = values[0]
var result = 0
for (i in 1 until values.size) {
mxScore -= 1
result = maxOf(result, values[i] + mxScore)
mxScore = maxOf(mxScore, values[i])
}
return result
}
}
No comments:
Post a Comment