美文网首页
LeetCode #378 Kth Smallest Eleme

LeetCode #378 Kth Smallest Eleme

作者: 刘煌旭 | 来源:发表于2021-03-19 01:27 被阅读0次
    /**
    Problem Spec:
    Given an n x n matrix where each of the rows and columns are sorted in ascending order, return 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 1:
    
    Input: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
    Output: 13
    Explanation: The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8th smallest number is 13
    Example 2:
    
    Input: matrix = [[-5]], k = 1
    Output: -5
     
    
    Constraints:
    
    n == matrix.length
    n == matrix[i].length
    1 <= n <= 300
    -109 <= matrix[i][j] <= -109
    All the rows and columns of matrix are guaranteed to be sorted in non-degreasing order.
    1 <= k <= n2
    */
    
    /**
    * 1. Use a priority queue to keep the smallest elements;
    * 2. Select the smallest element and update the queue;
    */
    
    typedef struct PriorityQueueStruct {
        int capacity;
        int count;
        void **keys;
        int(*cmp)(void*, void*);
    }*PriorityQueue;
    
    void pq_exch(void **a, int i, int j) {
        void *t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
    
    int pq_max(void **a, int i, int j, int(*cmp)(void*, void*)) { return cmp(a[i], a[j]) > 0 ? i : j; }
    
    void pq_swim(void **a, int k, int (*cmp)(void*, void*)) {
        int p = k / 2;
        while (k > 1 && cmp(a[k], a[p]) > 0) {
            pq_exch(a, k, p);
            k = p;
            p = k / 2;
        }
    }
    
    void pq_sink(void **a, int n, int k, int(*cmp)(void*, void*)) {
        while (2 * k <= n) {
            int j = 2 * k;
            if (j < n && cmp(a[j], a[j + 1]) < 0) j += 1;
            if (cmp(a[k], a[j]) >= 0) break;
            pq_exch(a, k, j);
            k = j;
        }
    }
    
    PriorityQueue PriorityQueueCreate(int(*cmp)(void*, void*), int n) {
        PriorityQueue pq = (PriorityQueue)malloc(sizeof(*pq));
        pq->cmp = cmp;
        pq->capacity = n + 1;//16;//initial capacity
        pq->count = 0;
        pq->keys = malloc((pq->capacity + 1) * sizeof(void*));
        return pq;
    }
    
    bool PriorityQueueIsEmpty(PriorityQueue pq) { return pq->count == 0; }
    
    void PriorityQueueInsert(PriorityQueue pq, void *k) {
        pq->keys[++pq->count] = k;
        pq_swim(pq->keys, pq->count, pq->cmp);
    }
    
    void* PriorityQueueDelete(PriorityQueue pq) {
        void *ret = pq->keys[1];
        pq->keys[1] = pq->keys[pq->count];
        pq->keys[pq->count] = NULL;
        pq->count--;
        pq_sink(pq->keys, pq->count, 1, pq->cmp);
        return ret;
    }
    
    typedef struct {
        int i, j, val;
    }* Element;
    Element MakeElement(int i, int j, int val) {
        Element e = (Element)malloc(sizeof(*e));
        e->i = i;
        e->j = j;
        e->val = val;
        return e;
    }
    
    int ElementCmp(Element e1, Element e2) { return e2->val - e1->val; }
    
    Element ElementRight(Element e, int **a, int n, bool **flags) {
        if (e->j < n - 1 && !flags[e->i][e->j + 1]) { return MakeElement(e->i, e->j + 1, a[e->i][e->j + 1]); }
        return NULL;
    }
    
    Element ElementDown(Element e, int **a, int n, bool **flags) {
        if (e->i < n - 1 && !flags[e->i + 1][e->j]) { return MakeElement(e->i + 1, e->j, a[e->i + 1][e->j]); }
        return NULL;
    }
    
    int kthSmallest(int** matrix, int matrixSize, int* matrixColSize, int k){
        int n = matrixSize;
        bool **flags = (bool**)malloc(n * sizeof(*flags));
        for (int i = 0; i < n; i++) {
            flags[i] = (bool*)malloc(n * sizeof(bool));
            for (int j = 0; j < n; j++) { 
                flags[i][j] = false;
            }
        }
        PriorityQueue pq = PriorityQueueCreate((int(*)(void*, void*))ElementCmp, n * n);
        Element e = MakeElement(0, 0, matrix[0][0]);
        PriorityQueueInsert(pq, e);
        flags[0][0] = true;
        Element kth = NULL;
        while (k > 0) {
            kth = PriorityQueueDelete(pq);
            Element down = ElementDown(kth, matrix, n, flags), right = ElementRight(kth, matrix, n, flags);
            if (down != NULL && !flags[down->i][down->j]) { 
                PriorityQueueInsert(pq, down); 
                flags[down->i][down->j] = true;
            }
            if (right != NULL && !flags[right->i][right->j]) { 
                PriorityQueueInsert(pq, right); 
                flags[right->i][right->j] = true;
            }
            k--;
        }
        return kth->val;
    }
    

    相关文章

      网友评论

          本文标题:LeetCode #378 Kth Smallest Eleme

          本文链接:https://www.haomeiwen.com/subject/evvmcltx.html