LeetCode in Kotlin

1751. Maximum Number of Events That Can Be Attended II

Hard

You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend.

You can only attend one event at a time. If you choose to attend an event, you must attend the entire event. Note that the end day is inclusive: that is, you cannot attend two events where one of them starts and the other ends on the same day.

Return the maximum sum of values that you can receive by attending events.

Example 1:

Input: events = [[1,2,4],[3,4,3],[2,3,1]], k = 2

Output: 7

Explanation: Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.

Example 2:

Input: events = [[1,2,4],[3,4,3],[2,3,10]], k = 2

Output: 10

Explanation: Choose event 2 for a total value of 10. Notice that you cannot attend any other event as they overlap, and that you do not have to attend k events.

Example 3:

Input: events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3

Output: 9

Explanation: Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.

Constraints:

Solution

import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
    fun maxValue(events: Array<IntArray>, k: Int): Int {
        if (k == 1) {
            val value = Arrays.stream(events).max({ a: IntArray, b: IntArray -> a[2].compareTo(b[2]) })
            return if (value.isPresent) {
                value.get()[2]
            } else {
                throw NullPointerException()
            }
        }
        val n = events.size
        events.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }
        val memo = Array(n) { IntArray(k + 1) }
        return dfs(events, 0, k, memo)
    }

    private fun dfs(events: Array<IntArray>, i: Int, k: Int, memo: Array<IntArray>): Int {
        if (k == 0 || i >= events.size) {
            return 0
        }
        if (memo[i][k] > 0) {
            return memo[i][k]
        }
        val idx = binarySearch(events, events[i][1] + 1, i + 1)
        val use = events[i][2] + dfs(events, idx, k - 1, memo)
        val notUse = dfs(events, i + 1, k, memo)
        val res = Math.max(use, notUse)
        memo[i][k] = res
        return res
    }

    private fun binarySearch(events: Array<IntArray>, i: Int, st: Int): Int {
        var st = st
        if (st >= events.size) {
            return st
        }
        var end = events.size - 1
        while (st < end) {
            val mid = st + (end - st) / 2
            if (events[mid][0] < i) {
                st = mid + 1
            } else {
                end = mid
            }
        }
        return if (events[st][0] >= i) st else st + 1
    }
}