LeetCode 1439: Find the Kth Smallest Sum of a Matrix With Sorted Rows Solution

Master LeetCode problem 1439 (Find the Kth Smallest Sum of a Matrix With Sorted Rows), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows

Problem Explanation

Explanation

To find the kth smallest sum of arrays formed by choosing one element from each row of the given matrix with sorted rows, we can use a priority queue (min-heap) to keep track of the sums while exploring all possible combinations. We start by adding the first element of each row to the priority queue. Then in each iteration, we pop the smallest sum from the priority queue, add the next element from the corresponding row, and push the updated sum back into the priority queue. We repeat this process k times to find the kth smallest sum.

Algorithm:

  1. Initialize a priority queue with the first element from each row.
  2. Repeat the following steps k times:
    • Pop the smallest sum from the priority queue.
    • For each popped sum, add the next element from the corresponding row and push the updated sum back into the priority queue.
  3. Return the kth smallest sum.

Time Complexity: O(klogk) - We perform k iterations, each requiring logk time to adjust the priority queue.

Space Complexity: O(k) - We maintain a priority queue with at most k elements.

Solution Code

import java.util.PriorityQueue;

class Solution {
    public int kthSmallest(int[][] mat, int k) {
        int m = mat.length, n = mat[0].length;
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        int sum = 0;
        for (int i = 0; i < m; i++) {
            sum += mat[i][0];
        }
        pq.offer(sum);
        for (int i = 0; i < k - 1; i++) {
            int currentSum = pq.poll();
            for (int j = 0; j < m; j++) {
                for (int p = 1; p < n; p++) {
                    int newSum = currentSum - mat[j][p - 1] + mat[j][p];
                    pq.offer(newSum);
                }
            }
        }
        return pq.poll();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1439 (Find the Kth Smallest Sum of a Matrix With Sorted Rows)?

This page provides optimized solutions for LeetCode problem 1439 (Find the Kth Smallest Sum of a Matrix With Sorted Rows) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1439 (Find the Kth Smallest Sum of a Matrix With Sorted Rows)?

The time complexity for LeetCode 1439 (Find the Kth Smallest Sum of a Matrix With Sorted Rows) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1439 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1439 in Java, C++, or Python.

Back to LeetCode Solutions