LeetCode in Kotlin

1964. Find the Longest Valid Obstacle Course at Each Position

Hard

You want to build some obstacle courses. You are given a 0-indexed integer array obstacles of length n, where obstacles[i] describes the height of the ith obstacle.

For every index i between 0 and n - 1 (inclusive), find the length of the longest obstacle course in obstacles such that:

Return an array ans of length n, where ans[i] is the length of the longest obstacle course for index i as described above.

Example 1:

Input: obstacles = [1,2,3,2]

Output: [1,2,3,3]

Explanation: The longest valid obstacle course at each position is:

Example 2:

Input: obstacles = [2,2,1]

Output: [1,2,1]

Explanation: The longest valid obstacle course at each position is:

Example 3:

Input: obstacles = [3,1,5,6,4,2]

Output: [1,1,2,3,2,2]

Explanation: The longest valid obstacle course at each position is:

Constraints:

Solution

@Suppress("NAME_SHADOWING")
class Solution {
    fun longestObstacleCourseAtEachPosition(obstacles: IntArray): IntArray {
        return longestIncreasingSubsequence(obstacles)
    }

    private fun longestIncreasingSubsequence(obstacles: IntArray): IntArray {
        var len = 1
        val length = obstacles.size
        val ans = IntArray(length)
        val arr = IntArray(length)
        arr[0] = obstacles[0]
        ans[0] = 1
        for (i in 1 until length) {
            val `val` = obstacles[i]
            if (`val` >= arr[len - 1]) {
                arr[len++] = `val`
                ans[i] = len
            } else {
                val idx = binarySearch(arr, 0, len - 1, `val`)
                arr[idx] = `val`
                ans[i] = idx + 1
            }
        }
        return ans
    }

    private fun binarySearch(arr: IntArray, lo: Int, hi: Int, `val`: Int): Int {
        var lo = lo
        var hi = hi
        var ans = -1
        while (lo <= hi) {
            val mid = (lo + hi) / 2
            if (`val` >= arr[mid]) {
                lo = mid + 1
            } else {
                ans = mid
                hi = mid - 1
            }
        }
        return ans
    }
}