亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LeetCode] 378. Kth Smallest Element in a Sorted M

Shihira / 2655人閱讀

摘要:先放一行,或一列把堆頂?shù)淖钚≡厝〕鰜?,取次,如果該有下一行下一列的,放入堆中最小的個元素已經(jīng)在上面的循環(huán)被完了,下一個堆頂元素就是

Problem

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.

Note that it is the kth smallest element in the sorted order, not the kth distinct element.

Example:

matrix = [
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
],
k = 8,

return 13.

Note:
You may assume k is always valid, 1 ≤ k ≤ n^2.

Solution
class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        int n = matrix.length;
        Queue queue = new PriorityQueue((a, b)->a.val-b.val);
        //先放一行,或一列
        for (int i = 0; i < n; i++) queue.offer(new Node(i, 0, matrix[i][0]));
        //把堆頂?shù)淖钚≡厝〕鰜?,取k-1次,如果該node有下一行/下一列的node,放入堆中
        for (int i = 0; i < k-1; i++) {
            Node node = queue.poll();
            if (node.y == n-1) continue;
            else queue.offer(new Node(node.x, node.y+1, matrix[node.x][node.y+1]));
        }
        //最小的k-1個元素已經(jīng)在上面的for循環(huán)被poll完了,下一個堆頂元素就是kth smallest
        return queue.poll().val;
    }
}

class Node {
    int x;
    int y;
    int val;
    public Node(int x, int y, int val) {
        this.x = x;
        this.y = y;
        this.val = val;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/72219.html

相關(guān)文章

  • 378. m>Kthm> m>Smm>am>llestm> m>Elementm> m>inm> m>am> m>Sortedm> Mm>am>trix

    摘要:復(fù)雜度是,其中。這做法和異曲同工??戳司W(wǎng)上給的解法,沒有二分,二分的是結(jié)果。每次找到一個,然后求比它小的元素的個數(shù),根據(jù)個數(shù)大于還是小于來二分。參考算的時候可以優(yōu)化 378. Kth Smallest Element in a Sorted Matrix 題目鏈接:https://leetcode.com/problems... 求矩陣里面第k小的數(shù),首先比較容易想到的是用heap來做...

    Y3G 評論0 收藏0
  • m>leetcodem>378. m>Kthm> m>Smm>am>llestm> m>Elementm> m>inm> m>am> m>Sortedm> Mm>am>tr

    摘要:因此我們可以采用部分元素堆排序即可。即我們每次只需要可能構(gòu)成第個元素的值進行堆排序就可以了。 題目要求 Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that...

    dailybird 評論0 收藏0
  • 378. m>Kthm> m>Smm>am>llestm> m>Elementm> m>inm> m>am> m>Sortedm> Mm>am>trix

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not the...

    makeFoxPlay 評論0 收藏0
  • [m>Leetcodem>] m>Kthm> m>Smm>am>llestm> m>Elementm> m>inm> m>am> BST 二叉搜索樹第k小節(jié)

    摘要:中序遍歷復(fù)雜度時間空間思路因為左節(jié)點小于根節(jié)點小于右節(jié)點,二叉搜索樹的一個特性就是中序遍歷的結(jié)果就是樹內(nèi)節(jié)點從小到大順序輸出的結(jié)果。這里采用迭代形式,我們就可以在找到第小節(jié)點時馬上退出。這樣我們就可以用二叉樹搜索的方法來解決這個問題了。 Kth Smallest Element in a BST Given a binary search tree, write a function...

    Dean 評論0 收藏0
  • [m>Leetcodem>-Tree] m>Kthm> m>Smm>am>llestm> m>Elementm> m>inm> m>am> BST

    摘要:解題思路本題需要找的是第小的節(jié)點值,而二叉搜索樹的中序遍歷正好是數(shù)值從小到大排序的,那么這題就和中序遍歷一個情況。 Kth Smallest Element in a BSTGiven a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You ...

    Carl 評論0 收藏0

發(fā)表評論

0條評論

Shihira

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<