Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3536c5f

Browse files
committed
feat: add solutions to lc problems: No.0296,2448
* No.0296.Best Meeting Point * No.2448.Minimum Cost to Make Array Equal
1 parent 2b52653 commit 3536c5f

File tree

9 files changed

+574
-3
lines changed

9 files changed

+574
-3
lines changed

‎solution/0200-0299/0296.Best Meeting Point/README.md‎

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,143 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
**方法一:排序 + 中位数**
50+
51+
对于每一行,我们可以将所有的 1ドル$ 的下标排序,然后取中位数 $i$ 作为碰头地点的横坐标。
52+
53+
对于每一列,我们可以将所有的 1ドル$ 的下标排序,然后取中位数 $i$ 作为碰头地点的纵坐标。
54+
55+
最后,我们计算所有 1ドル$ 到碰头地点 $(i, j)$ 的曼哈顿距离之和即可。
56+
57+
时间复杂度 $O(m\times n\times \log(m\times n))$。最多有 $m\times n$ 个 1ドル,ドル排序的时间复杂度为 $\log(m\times n)$。
58+
59+
相似题目:
60+
61+
- [462. 最少移动次数使数组元素相等 II](/solution/0400-0499/0462.Minimum%20Moves%20to%20Equal%20Array%20Elements%20II/README.md)
62+
- [2448. 使数组相等的最小开销](/solution/2400-2499/2448.Minimum%20Cost%20to%20Make%20Array%20Equal/README.md)
63+
4964
<!-- tabs:start -->
5065

5166
### **Python3**
5267

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

5570
```python
56-
71+
class Solution:
72+
def minTotalDistance(self, grid: List[List[int]]) -> int:
73+
def f(arr, x):
74+
return sum(abs(v - x) for v in arr)
75+
76+
rows, cols = [], []
77+
for i, row in enumerate(grid):
78+
for j, v in enumerate(row):
79+
if v:
80+
rows.append(i)
81+
cols.append(j)
82+
cols.sort()
83+
i = rows[len(rows) >> 1]
84+
j = cols[len(cols) >> 1]
85+
return f(rows, i) + f(cols, j)
5786
```
5887

5988
### **Java**
6089

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

6392
```java
93+
class Solution {
94+
public int minTotalDistance(int[][] grid) {
95+
int m = grid.length, n = grid[0].length;
96+
List<Integer> rows = new ArrayList<>();
97+
List<Integer> cols = new ArrayList<>();
98+
for (int i = 0; i < m; ++i) {
99+
for (int j = 0; j < n; ++j) {
100+
if (grid[i][j] == 1) {
101+
rows.add(i);
102+
cols.add(j);
103+
}
104+
}
105+
}
106+
Collections.sort(cols);
107+
int i = rows.get(rows.size() >> 1);
108+
int j = cols.get(cols.size() >> 1);
109+
return f(rows, i) + f(cols, j);
110+
}
111+
112+
private int f(List<Integer> arr, int x) {
113+
int s = 0;
114+
for (int v : arr) {
115+
s += Math.abs(v - x);
116+
}
117+
return s;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int minTotalDistance(vector<vector<int>>& grid) {
128+
int m = grid.size(), n = grid[0].size();
129+
vector<int> rows;
130+
vector<int> cols;
131+
for (int i = 0; i < m; ++i) {
132+
for (int j = 0; j < n; ++j) {
133+
if (grid[i][j]) {
134+
rows.emplace_back(i);
135+
cols.emplace_back(j);
136+
}
137+
}
138+
}
139+
sort(cols.begin(), cols.end());
140+
int i = rows[rows.size() / 2];
141+
int j = cols[cols.size() / 2];
142+
auto f = [](vector<int>& arr, int x) {
143+
int s = 0;
144+
for (int v : arr) {
145+
s += abs(v - x);
146+
}
147+
return s;
148+
};
149+
return f(rows, i) + f(cols, j);
150+
}
151+
};
152+
```
64153
154+
### **Go**
155+
156+
```go
157+
func minTotalDistance(grid [][]int) int {
158+
rows, cols := []int{}, []int{}
159+
for i, row := range grid {
160+
for j, v := range row {
161+
if v == 1 {
162+
rows = append(rows, i)
163+
cols = append(cols, j)
164+
}
165+
}
166+
}
167+
sort.Ints(cols)
168+
i := rows[len(rows)>>1]
169+
j := cols[len(cols)>>1]
170+
f := func(arr []int, x int) int {
171+
s := 0
172+
for _, v := range arr {
173+
s += abs(v - x)
174+
}
175+
return s
176+
}
177+
return f(rows, i) + f(cols, j)
178+
}
179+
180+
func abs(x int) int {
181+
if x < 0 {
182+
return -x
183+
}
184+
return x
185+
}
65186
```
66187

67188
### **...**

‎solution/0200-0299/0296.Best Meeting Point/README_EN.md‎

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,119 @@ So return 6.
4646
### **Python3**
4747

4848
```python
49-
49+
class Solution:
50+
def minTotalDistance(self, grid: List[List[int]]) -> int:
51+
def f(arr, x):
52+
return sum(abs(v - x) for v in arr)
53+
54+
rows, cols = [], []
55+
for i, row in enumerate(grid):
56+
for j, v in enumerate(row):
57+
if v:
58+
rows.append(i)
59+
cols.append(j)
60+
cols.sort()
61+
i = rows[len(rows) >> 1]
62+
j = cols[len(cols) >> 1]
63+
return f(rows, i) + f(cols, j)
5064
```
5165

5266
### **Java**
5367

5468
```java
69+
class Solution {
70+
public int minTotalDistance(int[][] grid) {
71+
int m = grid.length, n = grid[0].length;
72+
List<Integer> rows = new ArrayList<>();
73+
List<Integer> cols = new ArrayList<>();
74+
for (int i = 0; i < m; ++i) {
75+
for (int j = 0; j < n; ++j) {
76+
if (grid[i][j] == 1) {
77+
rows.add(i);
78+
cols.add(j);
79+
}
80+
}
81+
}
82+
Collections.sort(cols);
83+
int i = rows.get(rows.size() >> 1);
84+
int j = cols.get(cols.size() >> 1);
85+
return f(rows, i) + f(cols, j);
86+
}
87+
88+
private int f(List<Integer> arr, int x) {
89+
int s = 0;
90+
for (int v : arr) {
91+
s += Math.abs(v - x);
92+
}
93+
return s;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int minTotalDistance(vector<vector<int>>& grid) {
104+
int m = grid.size(), n = grid[0].size();
105+
vector<int> rows;
106+
vector<int> cols;
107+
for (int i = 0; i < m; ++i) {
108+
for (int j = 0; j < n; ++j) {
109+
if (grid[i][j]) {
110+
rows.emplace_back(i);
111+
cols.emplace_back(j);
112+
}
113+
}
114+
}
115+
sort(cols.begin(), cols.end());
116+
int i = rows[rows.size() / 2];
117+
int j = cols[cols.size() / 2];
118+
auto f = [](vector<int>& arr, int x) {
119+
int s = 0;
120+
for (int v : arr) {
121+
s += abs(v - x);
122+
}
123+
return s;
124+
};
125+
return f(rows, i) + f(cols, j);
126+
}
127+
};
128+
```
55129
130+
### **Go**
131+
132+
```go
133+
func minTotalDistance(grid [][]int) int {
134+
rows, cols := []int{}, []int{}
135+
for i, row := range grid {
136+
for j, v := range row {
137+
if v == 1 {
138+
rows = append(rows, i)
139+
cols = append(cols, j)
140+
}
141+
}
142+
}
143+
sort.Ints(cols)
144+
i := rows[len(rows)>>1]
145+
j := cols[len(cols)>>1]
146+
f := func(arr []int, x int) int {
147+
s := 0
148+
for _, v := range arr {
149+
s += abs(v - x)
150+
}
151+
return s
152+
}
153+
return f(rows, i) + f(cols, j)
154+
}
155+
156+
func abs(x int) int {
157+
if x < 0 {
158+
return -x
159+
}
160+
return x
161+
}
56162
```
57163

58164
### **...**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int minTotalDistance(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
vector<int> rows;
6+
vector<int> cols;
7+
for (int i = 0; i < m; ++i) {
8+
for (int j = 0; j < n; ++j) {
9+
if (grid[i][j]) {
10+
rows.emplace_back(i);
11+
cols.emplace_back(j);
12+
}
13+
}
14+
}
15+
sort(cols.begin(), cols.end());
16+
int i = rows[rows.size() / 2];
17+
int j = cols[cols.size() / 2];
18+
auto f = [](vector<int>& arr, int x) {
19+
int s = 0;
20+
for (int v : arr) {
21+
s += abs(v - x);
22+
}
23+
return s;
24+
};
25+
return f(rows, i) + f(cols, j);
26+
}
27+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func minTotalDistance(grid [][]int) int {
2+
rows, cols := []int{}, []int{}
3+
for i, row := range grid {
4+
for j, v := range row {
5+
if v == 1 {
6+
rows = append(rows, i)
7+
cols = append(cols, j)
8+
}
9+
}
10+
}
11+
sort.Ints(cols)
12+
i := rows[len(rows)>>1]
13+
j := cols[len(cols)>>1]
14+
f := func(arr []int, x int) int {
15+
s := 0
16+
for _, v := range arr {
17+
s += abs(v - x)
18+
}
19+
return s
20+
}
21+
return f(rows, i) + f(cols, j)
22+
}
23+
24+
func abs(x int) int {
25+
if x < 0 {
26+
return -x
27+
}
28+
return x
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int minTotalDistance(int[][] grid) {
3+
int m = grid.length, n = grid[0].length;
4+
List<Integer> rows = new ArrayList<>();
5+
List<Integer> cols = new ArrayList<>();
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
if (grid[i][j] == 1) {
9+
rows.add(i);
10+
cols.add(j);
11+
}
12+
}
13+
}
14+
Collections.sort(cols);
15+
int i = rows.get(rows.size() >> 1);
16+
int j = cols.get(cols.size() >> 1);
17+
return f(rows, i) + f(cols, j);
18+
}
19+
20+
private int f(List<Integer> arr, int x) {
21+
int s = 0;
22+
for (int v : arr) {
23+
s += Math.abs(v - x);
24+
}
25+
return s;
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def minTotalDistance(self, grid: List[List[int]]) -> int:
3+
def f(arr, x):
4+
return sum(abs(v - x) for v in arr)
5+
6+
rows, cols = [], []
7+
for i, row in enumerate(grid):
8+
for j, v in enumerate(row):
9+
if v:
10+
rows.append(i)
11+
cols.append(j)
12+
cols.sort()
13+
i = rows[len(rows) >> 1]
14+
j = cols[len(cols) >> 1]
15+
return f(rows, i) + f(cols, j)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /