Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1738.html

1738. Find Kth Largest XOR Coordinate Value

Level

Medium

Description

You are given a 2D matrix of size m x n, consisting of non-negative integers. You are also given an integer k.

The value of coordinate (a, b) of the matrix is the XOR of all matrix[i][j] where 0 <= i <= a < m and 0 <= j <= b < n (0-indexed).

Find the k-th largest value (1-indexed) of all the coordinates of matrix.

Example 1:

Input: matrix = [[5,2],[1,6]], k = 1

Output: 7

Explanation: The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.

Example 2:

Input: matrix = [[5,2],[1,6]], k = 2

Output: 5

Explanation: The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value.

Example 3:

Input: matrix = [[5,2],[1,6]], k = 3

Output: 4

Explanation: The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value.

Example 4:

Input: matrix = [[5,2],[1,6]], k = 4

Output: 0

Explanation: The value of coordinate (1,1) is 5 XOR 2 XOR 1 XOR 6 = 0, which is the 4th largest value.

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 1000
  • 0 <= matrix[i][j] <= 10^6
  • 1 <= k <= m * n

Solution

First, calculate the values of all coordinates of the matrix. To do this, first calculate each row’s prefix XOR results. To calculate the value of coordinate (a, b), if a == 0, then the value is simply the prefix XOR of coordinate (a, b). Otherwise, the value is the XOR result of the value of coordinate (a - 1, b) and the prefix XOR of coordinate (a, b).

Next, use a priority queue to store the top k XOR values. Loop over all values of the matrix and update the priority queue.

Finally, return the k-th largest XOR coordinate value from the priority queue.

  • class Solution {
        public int kthLargestValue(int[][] matrix, int k) {
            int rows = matrix.length, columns = matrix[0].length;
            int[][] rowXORs = new int[rows][columns];
            for (int i = 0; i < rows; i++) {
                rowXORs[i][0] = matrix[i][0];
                for (int j = 1; j < columns; j++)
                    rowXORs[i][j] = rowXORs[i][j - 1] ^ matrix[i][j];
            }
            int[][] totalXORs = new int[rows][columns];
            for (int j = 0; j < columns; j++)
                totalXORs[0][j] = rowXORs[0][j];
            for (int i = 1; i < rows; i++) {
                for (int j = 0; j < columns; j++)
                    totalXORs[i][j] = totalXORs[i - 1][j] ^ rowXORs[i][j];
            }
            PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    priorityQueue.offer(totalXORs[i][j]);
                    if (priorityQueue.size() > k)
                        priorityQueue.poll();
                }
            }
            return priorityQueue.poll();
        }
    }
    
    ############
    
    class Solution {
        public int kthLargestValue(int[][] matrix, int k) {
            int m = matrix.length, n = matrix[0].length;
            int[][] s = new int[m + 1][n + 1];
            List<Integer> ans = new ArrayList<>();
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j];
                    ans.add(s[i + 1][j + 1]);
                }
            }
            Collections.sort(ans);
            return ans.get(ans.size() - k);
        }
    }
    
  • // OJ: https://leetcode.com/problems/find-kth-largest-xor-coordinate-value/
    // Time: O(MNlog(MN))
    // Space: O(MN)
    class Solution {
    public:
        int kthLargestValue(vector<vector<int>>& A, int k) {
            vector<int> v;
            int M = A.size(), N = A[0].size();
            for (int i = 0; i < M; ++i) {
                int val = 0;
                for (int j = 0; j < N; ++j) {
                    val ^= A[i][j];
                    A[i][j] = val;
                    if (i - 1 >= 0) A[i][j] ^= A[i - 1][j];
                    v.push_back(A[i][j]);
                }
            }
            sort(begin(v), end(v), greater<>());
            return v[k - 1];
        }
    };
    
  • class Solution:
        def kthLargestValue(self, matrix: List[List[int]], k: int) -> int:
            m, n = len(matrix), len(matrix[0])
            s = [[0] * (n + 1) for _ in range(m + 1)]
            ans = []
            for i in range(m):
                for j in range(n):
                    s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j]
                    ans.append(s[i + 1][j + 1])
            return nlargest(k, ans)[-1]
    
    ############
    
    # 1738. Find Kth Largest XOR Coordinate Value
    # https://leetcode.com/problems/find-kth-largest-xor-coordinate-value/
    
    class Solution:
        def kthLargestValue(self, matrix: List[List[int]], k: int):
            rows, cols = len(matrix), len(matrix[0])
            res = []
            
            for i in range(rows):
                for j in range(cols):
                    if i:
                        matrix[i][j] ^= matrix[i-1][j]
                    if j:
                        matrix[i][j] ^= matrix[i][j-1]
                    if i and j:
                        matrix[i][j] ^= matrix[i-1][j-1]
                    
                    res.append(matrix[i][j])
                    
            return sorted(res)[-k]
    
  • func kthLargestValue(matrix [][]int, k int) int {
    	m, n := len(matrix), len(matrix[0])
    	s := make([][]int, m+1)
    	for i := range s {
    		s[i] = make([]int, n+1)
    	}
    	var ans []int
    	for i := 0; i < m; i++ {
    		for j := 0; j < n; j++ {
    			s[i+1][j+1] = s[i+1][j] ^ s[i][j+1] ^ s[i][j] ^ matrix[i][j]
    			ans = append(ans, s[i+1][j+1])
    		}
    	}
    	sort.Ints(ans)
    	return ans[len(ans)-k]
    }
    
  • function kthLargestValue(matrix: number[][], k: number): number {
        const m: number = matrix.length;
        const n: number = matrix[0].length;
        const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => 0));
        const ans: number[] = [];
        for (let i = 0; i < m; ++i) {
            for (let j = 0; j < n; ++j) {
                s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j];
                ans.push(s[i + 1][j + 1]);
            }
        }
        ans.sort((a, b) => b - a);
        return ans[k - 1];
    }
    
    

All Problems

All Solutions