LeetCode in Kotlin

3276. Select Cells in Grid With Maximum Score

Hard

You are given a 2D matrix grid consisting of positive integers.

You have to select one or more cells from the matrix such that the following conditions are satisfied:

Your score will be the sum of the values of the selected cells.

Return the maximum score you can achieve.

Example 1:

Input: grid = [[1,2,3],[4,3,2],[1,1,1]]

Output: 8

Explanation:

We can select the cells with values 1, 3, and 4 that are colored above.

Example 2:

Input: grid = [[8,7,6],[8,3,2]]

Output: 15

Explanation:

We can select the cells with values 7 and 8 that are colored above.

Constraints:

Solution

import kotlin.math.max

class Solution {
    fun maxScore(grid: List<List<Int>>): Int {
        val n = grid.size
        val m = grid[0].size
        val arr = Array(n * m) { IntArray(2) }
        for (i in 0 until n) {
            val l = grid[i]
            for (j in l.indices) {
                arr[i * m + j][0] = l[j]
                arr[i * m + j][1] = i
            }
        }
        arr.sortWith { a: IntArray, b: IntArray -> b[0] - a[0] }
        var dp = IntArray(1 shl n)
        var i = 0
        while (i < arr.size) {
            val seen = BooleanArray(n)
            seen[arr[i][1]] = true
            val v = arr[i][0]
            i++
            while (i < arr.size && arr[i][0] == v) {
                seen[arr[i][1]] = true
                i++
            }
            val next = dp.copyOf(dp.size)
            for (j in 0 until n) {
                if (seen[j]) {
                    val and = ((1 shl n) - 1) xor (1 shl j)
                    var k = and
                    while (k > 0) {
                        next[k or (1 shl j)] = max(next[k or (1 shl j)], (dp[k] + v))
                        k = (k - 1) and and
                    }
                    next[1 shl j] = max(next[1 shl j], v)
                }
            }
            dp = next
        }
        return dp[dp.size - 1]
    }
}