Skip to content

Commit 67ce94a

Browse files
committed
day 7
1 parent 01049b2 commit 67ce94a

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Kth Smallest Element in a Sorted Matrix
3+
========================================
4+
5+
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.
6+
7+
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
8+
9+
Example 1:
10+
Input: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
11+
Output: 13
12+
Explanation: The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8th smallest number is 13
13+
14+
Example 2:
15+
Input: matrix = [[-5]], k = 1
16+
Output: -5
17+
18+
Constraints:
19+
n == matrix.length
20+
n == matrix[i].length
21+
1 <= n <= 300
22+
-109 <= matrix[i][j] <= 109
23+
All the rows and columns of matrix are guaranteed to be sorted in non-decreasing order.
24+
1 <= k <= n2
25+
*/
26+
27+
class Solution
28+
{
29+
public:
30+
int kthSmallest(vector<vector<int>> &A, int k)
31+
{
32+
int n = A.size(), m = A[0].size();
33+
int l = A[0][0], h = A[n - 1][m - 1], ans;
34+
35+
while (l < h)
36+
{
37+
int mid = (l + h) / 2;
38+
int count = 0, j = m - 1;
39+
40+
for (int i = 0; i < n; ++i)
41+
{
42+
while (j >= 0 && A[i][j] > mid)
43+
j--;
44+
count += j + 1;
45+
}
46+
47+
if (count < k)
48+
l = mid + 1;
49+
else
50+
h = mid;
51+
}
52+
return l;
53+
}
54+
};

Diff for: Leetcode Daily Challenge/July-2021/README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# June 2021 LeetCoding Challenge
22

3-
| Day | Question Links | Solutions |
4-
| :-: | :--------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------: |
5-
| 1. | [Gray Code](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3799/) | [cpp](./01.%20Gray%20Code.cpp) |
6-
| 2. | [Find K Closest Elements](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3800/) | [cpp](./02.%20Find%20K%20Closest%20Elements.cpp) |
7-
| 4. | [Count Vowels Permutation](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3802/) | [cpp](./04.%20Count%20Vowels%20Permutation.cpp) |
8-
| 5. | [Reshape the Matrix](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3803/) | [cpp](./05.%20Reshape%20the%20Matrix.cpp) |
9-
| 6. | [Reduce Array Size to The Half](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3804/) | [cpp](./06.%20Reduce%20Array%20Size%20to%20The%20Half.cpp) |
3+
| Day | Question Links | Solutions |
4+
| :-: | :------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------: |
5+
| 1. | [Gray Code](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3799/) | [cpp](./01.%20Gray%20Code.cpp) |
6+
| 2. | [Find K Closest Elements](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3800/) | [cpp](./02.%20Find%20K%20Closest%20Elements.cpp) |
7+
| 4. | [Count Vowels Permutation](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3802/) | [cpp](./04.%20Count%20Vowels%20Permutation.cpp) |
8+
| 5. | [Reshape the Matrix](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3803/) | [cpp](./05.%20Reshape%20the%20Matrix.cpp) |
9+
| 6. | [Reduce Array Size to The Half](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3804/) | [cpp](./06.%20Reduce%20Array%20Size%20to%20The%20Half.cpp) |
10+
| 7. | [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3805/) | [cpp](./07.%20Kth%20Smallest%20Element%20in%20a%20Sorted%20Matrix.cpp) |

0 commit comments

Comments
 (0)