Skip to content

Commit bed3836

Browse files
committed
Add solution #378
1 parent 275f002 commit bed3836

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
371|[Sum of Two Integers](./0371-sum-of-two-integers.js)|Medium|
267267
372|[Super Pow](./0372-super-pow.js)|Medium|
268268
374|[Guess Number Higher or Lower](./0374-guess-number-higher-or-lower.js)|Medium|
269+
378|[Kth Smallest Element in a Sorted Matrix](./0378-kth-smallest-element-in-a-sorted-matrix.js)|Medium|
269270
380|[Insert Delete GetRandom O(1)](./0380-insert-delete-getrandom-o1.js)|Medium|
270271
383|[Ransom Note](./0383-ransom-note.js)|Easy|
271272
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 378. Kth Smallest Element in a Sorted Matrix
3+
* https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
4+
* Difficulty: Medium
5+
*
6+
* Given an n x n matrix where each of the rows and columns is sorted in ascending order,
7+
* return the kth smallest element in the matrix.
8+
*
9+
* Note that it is the kth smallest element in the sorted order, not the kth distinct element.
10+
*
11+
* You must find a solution with a memory complexity better than O(n2).
12+
*/
13+
14+
/**
15+
* @param {number[][]} matrix
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var kthSmallest = function(matrix, k) {
20+
return matrix.flat().sort((a, b) => a - b)[k - 1];
21+
};

0 commit comments

Comments
 (0)