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

<p>The <strong>value</strong> of coordinate <code>(a, b)</code> of the matrix is the XOR of all <code>matrix[i][j]</code> where <code>0 &lt;= i &lt;= a &lt; m</code> and <code>0 &lt;= j &lt;= b &lt; n</code> <strong>(0-indexed)</strong>.</p>

<p>Find the <code>k<sup>th</sup></code> largest value <strong>(1-indexed)</strong> of all the coordinates of <code>matrix</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[5,2],[1,6]], k = 1
<strong>Output:</strong> 7
<strong>Explanation:</strong> The value of coordinate (0,1) is 5 XOR 2 = 7, which is the largest value.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[5,2],[1,6]], k = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The value of coordinate (0,0) is 5 = 5, which is the 2nd largest value.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[5,2],[1,6]], k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The value of coordinate (1,0) is 5 XOR 1 = 4, which is the 3rd largest value.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 1000</code></li>
	<li><code>0 &lt;= matrix[i][j] &lt;= 10<sup>6</sup></code></li>
	<li><code>1 &lt;= k &lt;= m * n</code></li>
</ul>

class Solution:
    def kthLargestValue(self, matrix: List[List[int]], k: int) -> int:
        # 1. prefix_XOR
        m, n = len(matrix), len(matrix[0])
        pref_xor = [[matrix[i][j] for j in range(n)] for i in range(m)]

        for i in range(m):
            for j in range(1, n):
                pref_xor[i][j] = pref_xor[i][j]^pref_xor[i][j-1]
        
        for j in range(n):
            for i in range(1, m):
                pref_xor[i][j] = pref_xor[i][j]^pref_xor[i-1][j]
        
        # expand to a list
        xor_list = [pref_xor[i][j] for i in range(m) for j in range(n)]
        
        # 2. quick_select of (m-k)th smallest
        # l_x = len(xor_list)
        # ans = self.quick_select(xor_list, 0, l_x - 1, l_x - k)

        # 3. sorting
        xor_list.sort()

        return xor_list[len(xor_list) - k]
    

    def quick_select(self, nums, l, r, k):
        if l == r:
            return nums[l]
        
        pivot = self.partition(nums, l, r)

        if pivot == k:
            return nums[pivot]
        elif pivot < k: # on right
            return self.quick_select(nums, pivot+1, r, k)
        else:
            return self.quick_select(nums, l, pivot-1, k)


    def partition(self, nums, l, r):
        # pick a random pivot and move to end
        pivot = random.randint(l, r)
        nums[pivot], nums[r] = nums[r], nums[pivot]

        pointer = l
        for i in range(l, r):
            if nums[i] < nums[r]:
                nums[pointer], nums[i] = nums[i], nums[pointer]
                pointer += 1
        nums[pointer], nums[r] = nums[r], nums[pointer]
        return pointer
50100