Skip to content

Commit 73811d4

Browse files
committed
feat: add solutions to lc problem: No.2387
No.2387.Median of a Row Wise Sorted Matrix
1 parent 9587da8 commit 73811d4

File tree

7 files changed

+326
-5
lines changed

7 files changed

+326
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def removeKdigits(self, num: str, k: int) -> str:
3-
stack, remain = [], len(num)-k
3+
stack, remain = [], len(num) - k
44
for value in num:
55
while k and stack and stack[-1] > value:
6-
k = k-1
6+
k = k - 1
77
stack.pop()
88
stack.append(value)
9-
return "".join(stack[:remain]).lstrip('0') or '0'
9+
return "".join(stack[:remain]).lstrip('0') or '0'

solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README.md

+116-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,137 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:两次二分查找**
47+
48+
中位数实际上是排序后第 $target = \left \lceil \frac{m\times n}{2} \right \rceil $ 个数。
49+
50+
我们二分枚举矩阵的元素 $x$,统计网格中大于该元素的个数 $cnt$,如果 $cnt \ge target$,说明中位数在 $x$ 的左侧(包含 $x$),否则在右侧。
51+
52+
时间复杂度 $O(m\times \log n \times log M)$,其中 $M$ 为矩阵中的最大值。
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**
4957

5058
<!-- 这里可写当前语言的特殊实现逻辑 -->
5159

5260
```python
53-
61+
class Solution:
62+
def matrixMedian(self, grid: List[List[int]]) -> int:
63+
def count(x):
64+
return sum(bisect_right(row, x) for row in grid)
65+
66+
m, n = len(grid), len(grid[0])
67+
target = (m * n + 1) >> 1
68+
return bisect_left(range(10**6 + 1), target, key=count)
5469
```
5570

5671
### **Java**
5772

5873
<!-- 这里可写当前语言的特殊实现逻辑 -->
5974

6075
```java
76+
class Solution {
77+
private int[][] grid;
78+
79+
public int matrixMedian(int[][] grid) {
80+
this.grid = grid;
81+
int m = grid.length, n = grid[0].length;
82+
int target = (m * n + 1) >> 1;
83+
int left = 0, right = 1000010;
84+
while (left < right) {
85+
int mid = (left + right) >> 1;
86+
if (count(mid) >= target) {
87+
right = mid;
88+
} else {
89+
left = mid + 1;
90+
}
91+
}
92+
return left;
93+
}
94+
95+
private int count(int x) {
96+
int cnt = 0;
97+
for (var row : grid) {
98+
int left = 0, right = row.length;
99+
while (left < right) {
100+
int mid = (left + right) >> 1;
101+
if (row[mid] > x) {
102+
right = mid;
103+
} else {
104+
left = mid + 1;
105+
}
106+
}
107+
cnt += left;
108+
}
109+
return cnt;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int matrixMedian(vector<vector<int>>& grid) {
120+
int m = grid.size(), n = grid[0].size();
121+
int left = 0, right = 1e6 + 1;
122+
int target = (m * n + 1) >> 1;
123+
auto count = [&](int x) {
124+
int cnt = 0;
125+
for (auto& row : grid) {
126+
cnt += (upper_bound(row.begin(), row.end(), x) - row.begin());
127+
}
128+
return cnt;
129+
};
130+
while (left < right) {
131+
int mid = (left + right) >> 1;
132+
if (count(mid) >= target) {
133+
right = mid;
134+
} else {
135+
left = mid + 1;
136+
}
137+
}
138+
return left;
139+
}
140+
};
141+
```
61142
143+
### **Go**
144+
145+
```go
146+
func matrixMedian(grid [][]int) int {
147+
m, n := len(grid), len(grid[0])
148+
149+
count := func(x int) int {
150+
cnt := 0
151+
for _, row := range grid {
152+
left, right := 0, n
153+
for left < right {
154+
mid := (left + right) >> 1
155+
if row[mid] > x {
156+
right = mid
157+
} else {
158+
left = mid + 1
159+
}
160+
}
161+
cnt += left
162+
}
163+
return cnt
164+
}
165+
left, right := 0, 1000010
166+
target := (m*n + 1) >> 1
167+
for left < right {
168+
mid := (left + right) >> 1
169+
if count(mid) >= target {
170+
right = mid
171+
} else {
172+
left = mid + 1
173+
}
174+
}
175+
return left
176+
}
62177
```
63178

64179
### **TypeScript**

solution/2300-2399/2387.Median of a Row Wise Sorted Matrix/README_EN.md

+108-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,120 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def matrixMedian(self, grid: List[List[int]]) -> int:
49+
def count(x):
50+
return sum(bisect_right(row, x) for row in grid)
51+
52+
m, n = len(grid), len(grid[0])
53+
target = (m * n + 1) >> 1
54+
return bisect_left(range(10**6 + 1), target, key=count)
4855
```
4956

5057
### **Java**
5158

5259
```java
60+
class Solution {
61+
private int[][] grid;
62+
63+
public int matrixMedian(int[][] grid) {
64+
this.grid = grid;
65+
int m = grid.length, n = grid[0].length;
66+
int target = (m * n + 1) >> 1;
67+
int left = 0, right = 1000010;
68+
while (left < right) {
69+
int mid = (left + right) >> 1;
70+
if (count(mid) >= target) {
71+
right = mid;
72+
} else {
73+
left = mid + 1;
74+
}
75+
}
76+
return left;
77+
}
78+
79+
private int count(int x) {
80+
int cnt = 0;
81+
for (var row : grid) {
82+
int left = 0, right = row.length;
83+
while (left < right) {
84+
int mid = (left + right) >> 1;
85+
if (row[mid] > x) {
86+
right = mid;
87+
} else {
88+
left = mid + 1;
89+
}
90+
}
91+
cnt += left;
92+
}
93+
return cnt;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int matrixMedian(vector<vector<int>>& grid) {
104+
int m = grid.size(), n = grid[0].size();
105+
int left = 0, right = 1e6 + 1;
106+
int target = (m * n + 1) >> 1;
107+
auto count = [&](int x) {
108+
int cnt = 0;
109+
for (auto& row : grid) {
110+
cnt += (upper_bound(row.begin(), row.end(), x) - row.begin());
111+
}
112+
return cnt;
113+
};
114+
while (left < right) {
115+
int mid = (left + right) >> 1;
116+
if (count(mid) >= target) {
117+
right = mid;
118+
} else {
119+
left = mid + 1;
120+
}
121+
}
122+
return left;
123+
}
124+
};
125+
```
53126
127+
### **Go**
128+
129+
```go
130+
func matrixMedian(grid [][]int) int {
131+
m, n := len(grid), len(grid[0])
132+
133+
count := func(x int) int {
134+
cnt := 0
135+
for _, row := range grid {
136+
left, right := 0, n
137+
for left < right {
138+
mid := (left + right) >> 1
139+
if row[mid] > x {
140+
right = mid
141+
} else {
142+
left = mid + 1
143+
}
144+
}
145+
cnt += left
146+
}
147+
return cnt
148+
}
149+
left, right := 0, 1000010
150+
target := (m*n + 1) >> 1
151+
for left < right {
152+
mid := (left + right) >> 1
153+
if count(mid) >= target {
154+
right = mid
155+
} else {
156+
left = mid + 1
157+
}
158+
}
159+
return left
160+
}
54161
```
55162

56163
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int matrixMedian(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
int left = 0, right = 1e6 + 1;
6+
int target = (m * n + 1) >> 1;
7+
auto count = [&](int x) {
8+
int cnt = 0;
9+
for (auto& row : grid) {
10+
cnt += (upper_bound(row.begin(), row.end(), x) - row.begin());
11+
}
12+
return cnt;
13+
};
14+
while (left < right) {
15+
int mid = (left + right) >> 1;
16+
if (count(mid) >= target) {
17+
right = mid;
18+
} else {
19+
left = mid + 1;
20+
}
21+
}
22+
return left;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func matrixMedian(grid [][]int) int {
2+
m, n := len(grid), len(grid[0])
3+
4+
count := func(x int) int {
5+
cnt := 0
6+
for _, row := range grid {
7+
left, right := 0, n
8+
for left < right {
9+
mid := (left + right) >> 1
10+
if row[mid] > x {
11+
right = mid
12+
} else {
13+
left = mid + 1
14+
}
15+
}
16+
cnt += left
17+
}
18+
return cnt
19+
}
20+
left, right := 0, 1000010
21+
target := (m*n + 1) >> 1
22+
for left < right {
23+
mid := (left + right) >> 1
24+
if count(mid) >= target {
25+
right = mid
26+
} else {
27+
left = mid + 1
28+
}
29+
}
30+
return left
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
private int[][] grid;
3+
4+
public int matrixMedian(int[][] grid) {
5+
this.grid = grid;
6+
int m = grid.length, n = grid[0].length;
7+
int target = (m * n + 1) >> 1;
8+
int left = 0, right = 1000010;
9+
while (left < right) {
10+
int mid = (left + right) >> 1;
11+
if (count(mid) >= target) {
12+
right = mid;
13+
} else {
14+
left = mid + 1;
15+
}
16+
}
17+
return left;
18+
}
19+
20+
private int count(int x) {
21+
int cnt = 0;
22+
for (var row : grid) {
23+
int left = 0, right = row.length;
24+
while (left < right) {
25+
int mid = (left + right) >> 1;
26+
if (row[mid] > x) {
27+
right = mid;
28+
} else {
29+
left = mid + 1;
30+
}
31+
}
32+
cnt += left;
33+
}
34+
return cnt;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def matrixMedian(self, grid: List[List[int]]) -> int:
3+
def count(x):
4+
return sum(bisect_right(row, x) for row in grid)
5+
6+
m, n = len(grid), len(grid[0])
7+
target = (m * n + 1) >> 1
8+
return bisect_left(range(10**6 + 1), target, key=count)

0 commit comments

Comments
 (0)