Skip to content

Commit 97aec58

Browse files
committed
feat: add solutions to lc problem: No.1637
No.1637.Widest Vertical Area Between Two Points Containing No Points
1 parent 46239ce commit 97aec58

File tree

5 files changed

+72
-23
lines changed

5 files changed

+72
-23
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646

4747
**方法一:排序**
4848

49-
$points$ 按照 $x$ 升序排列,获取相邻点之间 $x$ 的差值的最大值。
49+
`points` 按照 $x$ 升序排列,获取相邻点之间 $x$ 的差值的最大值。
5050

51-
时间复杂度 $O(nlogn)$,其中 $n$ 表示 $points$ 的长度。
51+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ `points` 的长度。
5252

5353
<!-- tabs:start -->
5454

@@ -88,7 +88,9 @@ public:
8888
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
8989
sort(points.begin(), points.end());
9090
int ans = 0;
91-
for (int i = 0; i < points.size() - 1; ++i) ans = max(ans, points[i + 1][0] - points[i][0]);
91+
for (int i = 0; i < points.size() - 1; ++i) {
92+
ans = max(ans, points[i + 1][0] - points[i][0]);
93+
}
9294
return ans;
9395
}
9496
};
@@ -97,15 +99,12 @@ public:
9799
### **Go**
98100
99101
```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
102+
func maxWidthOfVerticalArea(points [][]int) (ans int) {
103+
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
105104
for i, p := range points[1:] {
106105
ans = max(ans, p[0]-points[i][0])
107106
}
108-
return ans
107+
return
109108
}
110109
111110
func max(a, b int) int {
@@ -116,6 +115,25 @@ func max(a, b int) int {
116115
}
117116
```
118117

118+
### **JavaScript**
119+
120+
```js
121+
/**
122+
* @param {number[][]} points
123+
* @return {number}
124+
*/
125+
var maxWidthOfVerticalArea = function (points) {
126+
points.sort((a, b) => a[0] - b[0]);
127+
let ans = 0;
128+
let px = points[0][0];
129+
for (const [x, _] of points) {
130+
ans = Math.max(ans, x - px);
131+
px = x;
132+
}
133+
return ans;
134+
};
135+
```
136+
119137
### **...**
120138

121139
```

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public:
7272
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
7373
sort(points.begin(), points.end());
7474
int ans = 0;
75-
for (int i = 0; i < points.size() - 1; ++i) ans = max(ans, points[i + 1][0] - points[i][0]);
75+
for (int i = 0; i < points.size() - 1; ++i) {
76+
ans = max(ans, points[i + 1][0] - points[i][0]);
77+
}
7678
return ans;
7779
}
7880
};
@@ -81,15 +83,12 @@ public:
8183
### **Go**
8284
8385
```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
86+
func maxWidthOfVerticalArea(points [][]int) (ans int) {
87+
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
8988
for i, p := range points[1:] {
9089
ans = max(ans, p[0]-points[i][0])
9190
}
92-
return ans
91+
return
9392
}
9493
9594
func max(a, b int) int {
@@ -100,6 +99,25 @@ func max(a, b int) int {
10099
}
101100
```
102101

102+
### **JavaScript**
103+
104+
```js
105+
/**
106+
* @param {number[][]} points
107+
* @return {number}
108+
*/
109+
var maxWidthOfVerticalArea = function (points) {
110+
points.sort((a, b) => a[0] - b[0]);
111+
let ans = 0;
112+
let px = points[0][0];
113+
for (const [x, _] of points) {
114+
ans = Math.max(ans, x - px);
115+
px = x;
116+
}
117+
return ans;
118+
};
119+
```
120+
103121
### **...**
104122

105123
```

solution/1600-1699/1637.Widest Vertical Area Between Two Points Containing No Points/Solution.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ class Solution {
33
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
44
sort(points.begin(), points.end());
55
int ans = 0;
6-
for (int i = 0; i < points.size() - 1; ++i) ans = max(ans, points[i + 1][0] - points[i][0]);
6+
for (int i = 0; i < points.size() - 1; ++i) {
7+
ans = max(ans, points[i + 1][0] - points[i][0]);
8+
}
79
return ans;
810
}
911
};

solution/1600-1699/1637.Widest Vertical Area Between Two Points Containing No Points/Solution.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
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
1+
func maxWidthOfVerticalArea(points [][]int) (ans int) {
2+
sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
63
for i, p := range points[1:] {
74
ans = max(ans, p[0]-points[i][0])
85
}
9-
return ans
6+
return
107
}
118

129
func max(a, b int) int {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[][]} points
3+
* @return {number}
4+
*/
5+
var maxWidthOfVerticalArea = function (points) {
6+
points.sort((a, b) => a[0] - b[0]);
7+
let ans = 0;
8+
let px = points[0][0];
9+
for (const [x, _] of points) {
10+
ans = Math.max(ans, x - px);
11+
px = x;
12+
}
13+
return ans;
14+
};

0 commit comments

Comments
 (0)