classSolution { public: intkthSmallest(vector<vector<int>>& matrix, int k){ structpoint { int val, x, y; point(int val, int x, int y) : val(val), x(x), y(y) {} booloperator> (const point& a) const { returnthis->val > a.val; } }; priority_queue<point, vector<point>, greater<point>> que; int n = matrix.size(); for (int i = 0; i < n; i++) { que.emplace(matrix[i][0], i, 0); } for (int i = 0; i < k - 1; i++) { point now = que.top(); que.pop(); if (now.y != n - 1) { que.emplace(matrix[now.x][now.y + 1], now.x, now.y + 1); } } return que.top().val; } };
classSolution { publicintkthSmallest(int[][] matrix, int k) { PriorityQueue<int[]> pq = newPriorityQueue<int[]>(newComparator<int[]>() { publicintcompare(int[] a, int[] b) { return a[0] - b[0]; } }); intn= matrix.length; for (inti=0; i < n; i++) { pq.offer(newint[]{matrix[i][0], i, 0}); } for (inti=0; i < k - 1; i++) { int[] now = pq.poll(); if (now[2] != n - 1) { pq.offer(newint[]{matrix[now[1]][now[2] + 1], now[1], now[2] + 1}); } } return pq.poll()[0]; } }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defkthSmallest(self, matrix: List[List[int]], k: int) -> int: n = len(matrix) pq = [(matrix[i][0], i, 0) for i inrange(n)] heapq.heapify(pq)
ret = 0 for i inrange(k - 1): num, x, y = heapq.heappop(pq) if y != n - 1: heapq.heappush(pq, (matrix[x][y + 1], x, y + 1)) return heapq.heappop(pq)[0]
funckthSmallest(matrix [][]int, k int)int { h := &IHeap{} for i := 0; i < len(matrix); i++ { heap.Push(h, [3]int{matrix[i][0], i, 0}) }
for i := 0; i < k - 1; i++ { now := heap.Pop(h).([3]int) if now[2] != len(matrix) - 1 { heap.Push(h, [3]int{matrix[now[1]][now[2]+1], now[1], now[2]+1}) } } return heap.Pop(h).([3]int)[0] }
boolcmp(point a, point b) { return a.val >= b.val; }
voidswap(point* a, point* b) { point t = *a; *a = *b, *b = t; }
voidpush(point heap[], int* size, point* p) { heap[++(*size)] = *p; int s = (*size); while (s > 1) { if (cmp(heap[s], heap[s >> 1])) { break; } swap(&heap[s], &heap[s >> 1]); s >>= 1; } }
voidpop(point heap[], int* size) { heap[1] = heap[(*size)--]; int p = 1, s = 2; while (s <= (*size)) { if (s < (*size) && !cmp(heap[s + 1], heap[s])) { s++; } if (cmp(heap[s], heap[p])) { break; } swap(&heap[s], &heap[p]); p = s, s = p << 1; } }
intkthSmallest(int** matrix, int matrixSize, int* matrixColSize, int k) { point heap[matrixSize + 1]; int size = 0; for (int i = 0; i < matrixSize; i++) { point p = {matrix[i][0], i, 0}; push(heap, &size, &p); } for (int i = 0; i < k - 1; i++) { point now = heap[1]; pop(heap, &size); if (now.y != matrixSize - 1) { point p = {matrix[now.x][now.y + 1], now.x, now.y + 1}; push(heap, &size, &p); } } return heap[1].val; }
classSolution { public: boolcheck(vector<vector<int>>& matrix, int mid, int k, int n){ int i = n - 1; int j = 0; int num = 0; while (i >= 0 && j < n) { if (matrix[i][j] <= mid) { num += i + 1; j++; } else { i--; } } return num >= k; }
intkthSmallest(vector<vector<int>>& matrix, int k){ int n = matrix.size(); int left = matrix[0][0]; int right = matrix[n - 1][n - 1]; while (left < right) { int mid = left + ((right - left) >> 1); if (check(matrix, mid, k, n)) { right = mid; } else { left = mid + 1; } } return left; } };
classSolution { publicintkthSmallest(int[][] matrix, int k) { intn= matrix.length; intleft= matrix[0][0]; intright= matrix[n - 1][n - 1]; while (left < right) { intmid= left + ((right - left) >> 1); if (check(matrix, mid, k, n)) { right = mid; } else { left = mid + 1; } } return left; }
publicbooleancheck(int[][] matrix, int mid, int k, int n) { inti= n - 1; intj=0; intnum=0; while (i >= 0 && j < n) { if (matrix[i][j] <= mid) { num += i + 1; j++; } else { i--; } } return num >= k; } }
funckthSmallest(matrix [][]int, k int)int { n := len(matrix) left, right := matrix[0][0], matrix[n-1][n-1] for left < right { mid := left + (right - left) / 2 if check(matrix, mid, k, n) { right = mid } else { left = mid + 1 } } return left }
funccheck(matrix [][]int, mid, k, n int)bool { i, j := n - 1, 0 num := 0 for i >= 0 && j < n { if matrix[i][j] <= mid { num += i + 1 j++ } else { i-- } } return num >= k }
boolcheck(int **matrix, int mid, int k, int n) { int i = n - 1; int j = 0; int num = 0; while (i >= 0 && j < n) { if (matrix[i][j] <= mid) { num += i + 1; j++; } else { i--; } } return num >= k; }
intkthSmallest(int **matrix, int matrixSize, int *matrixColSize, int k) { int left = matrix[0][0]; int right = matrix[matrixSize - 1][matrixSize - 1]; while (left < right) { int mid = left + ((right - left) >> 1); if (check(matrix, mid, k, matrixSize)) { right = mid; } else { left = mid + 1; } } return left; }