Skip to content

Commit 3952589

Browse files
authoredJun 5, 2022
feat: add solutions to lc problem: No.1637
No.1637.Widest Vertical Area Between Two Points Containing No Points
1 parent 2c4113c commit 3952589

File tree

7 files changed

+144
-3
lines changed

7 files changed

+144
-3
lines changed
 

‎solution/1600-1699/1637.Widest Vertical Area Between Two Points Containing No Points/README.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,76 @@
4444

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

47+
**方法一:排序**
48+
49+
对 $points$ 按照 $x$ 升序排列,获取相邻点之间 $x$ 的差值的最大值。
50+
51+
时间复杂度 $O(nlogn)$,其中 $n$ 表示 $points$ 的长度。
52+
4753
<!-- tabs:start -->
4854

4955
### **Python3**
5056

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

5359
```python
54-
60+
class Solution:
61+
def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int:
62+
points.sort()
63+
return max(b[0] - a[0] for a, b in pairwise(points))
5564
```
5665

5766
### **Java**
5867

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

6170
```java
71+
class Solution {
72+
public int maxWidthOfVerticalArea(int[][] points) {
73+
Arrays.sort(points, (a, b) -> a[0] - b[0]);
74+
int ans = 0;
75+
for (int i = 0; i < points.length - 1; ++i) {
76+
ans = Math.max(ans, points[i + 1][0] - points[i][0]);
77+
}
78+
return ans;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
89+
sort(points.begin(), points.end());
90+
int ans = 0;
91+
for (int i = 0; i < points.size() - 1; ++i) ans = max(ans, points[i + 1][0] - points[i][0]);
92+
return ans;
93+
}
94+
};
95+
```
6296
97+
### **Go**
98+
99+
```go
100+
func maxWidthOfVerticalArea(points [][]int) int {
101+
sort.Slice(points, func(i, j int) bool {
102+
return points[i][0] < points[j][0]
103+
})
104+
ans := 0
105+
for i, p := range points[1:] {
106+
ans = max(ans, p[0]-points[i][0])
107+
}
108+
return ans
109+
}
110+
111+
func max(a, b int) int {
112+
if a > b {
113+
return a
114+
}
115+
return b
116+
}
63117
```
64118

65119
### **...**

‎solution/1600-1699/1637.Widest Vertical Area Between Two Points Containing No Points/README_EN.md

+49-1
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,61 @@
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int:
48+
points.sort()
49+
return max(b[0] - a[0] for a, b in pairwise(points))
4750
```
4851

4952
### **Java**
5053

5154
```java
55+
class Solution {
56+
public int maxWidthOfVerticalArea(int[][] points) {
57+
Arrays.sort(points, (a, b) -> a[0] - b[0]);
58+
int ans = 0;
59+
for (int i = 0; i < points.length - 1; ++i) {
60+
ans = Math.max(ans, points[i + 1][0] - points[i][0]);
61+
}
62+
return ans;
63+
}
64+
}
65+
```
66+
67+
### **C++**
68+
69+
```cpp
70+
class Solution {
71+
public:
72+
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
73+
sort(points.begin(), points.end());
74+
int ans = 0;
75+
for (int i = 0; i < points.size() - 1; ++i) ans = max(ans, points[i + 1][0] - points[i][0]);
76+
return ans;
77+
}
78+
};
79+
```
5280
81+
### **Go**
82+
83+
```go
84+
func maxWidthOfVerticalArea(points [][]int) int {
85+
sort.Slice(points, func(i, j int) bool {
86+
return points[i][0] < points[j][0]
87+
})
88+
ans := 0
89+
for i, p := range points[1:] {
90+
ans = max(ans, p[0]-points[i][0])
91+
}
92+
return ans
93+
}
94+
95+
func max(a, b int) int {
96+
if a > b {
97+
return a
98+
}
99+
return b
100+
}
53101
```
54102

55103
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
4+
sort(points.begin(), points.end());
5+
int ans = 0;
6+
for (int i = 0; i < points.size() - 1; ++i) ans = max(ans, points[i + 1][0] - points[i][0]);
7+
return ans;
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func maxWidthOfVerticalArea(points [][]int) int {
2+
sort.Slice(points, func(i, j int) bool {
3+
return points[i][0] < points[j][0]
4+
})
5+
ans := 0
6+
for i, p := range points[1:] {
7+
ans = max(ans, p[0]-points[i][0])
8+
}
9+
return ans
10+
}
11+
12+
func max(a, b int) int {
13+
if a > b {
14+
return a
15+
}
16+
return b
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int maxWidthOfVerticalArea(int[][] points) {
3+
Arrays.sort(points, (a, b) -> a[0] - b[0]);
4+
int ans = 0;
5+
for (int i = 0; i < points.length - 1; ++i) {
6+
ans = Math.max(ans, points[i + 1][0] - points[i][0]);
7+
}
8+
return ans;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int:
3+
points.sort()
4+
return max(b[0] - a[0] for a, b in pairwise(points))

‎solution/2200-2299/2241.Design an ATM Machine/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class ATM:
2-
32
def __init__(self):
43
self.cnt = [0] * 5
54
self.m = [500, 200, 100, 50, 20]

0 commit comments

Comments
 (0)
Please sign in to comment.