File tree 2 files changed +22
-0
lines changed
2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change 266
266
371|[ Sum of Two Integers] ( ./0371-sum-of-two-integers.js ) |Medium|
267
267
372|[ Super Pow] ( ./0372-super-pow.js ) |Medium|
268
268
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|
269
270
380|[ Insert Delete GetRandom O(1)] ( ./0380-insert-delete-getrandom-o1.js ) |Medium|
270
271
383|[ Ransom Note] ( ./0383-ransom-note.js ) |Easy|
271
272
387|[ First Unique Character in a String] ( ./0387-first-unique-character-in-a-string.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments