Skip to content

Commit 4af7f8a

Browse files
authored
feat: add solutions to lc problem: No.1762 (#1716)
No.1762.Buildings With an Ocean View
1 parent 95f6e13 commit 4af7f8a

File tree

10 files changed

+146
-111
lines changed

10 files changed

+146
-111
lines changed

solution/1700-1799/1762.Buildings With an Ocean View/README.md

+39-27
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858

5959
**方法一:逆序遍历求右侧最大值**
6060

61-
逆序遍历数组 $height$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $ans$。
61+
我们逆序遍历数组 $height$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $ans$。然后我们更新 $mx$ 为 $v$。
6262

63-
最后逆序返回 $ans$。
63+
遍历结束后,逆序返回 $ans$ 即可
6464

65-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
65+
时间复杂度 $O(n)$,其中 $n$ 为数组长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
6666

6767
<!-- tabs:start -->
6868

@@ -73,13 +73,12 @@
7373
```python
7474
class Solution:
7575
def findBuildings(self, heights: List[int]) -> List[int]:
76-
mx = 0
7776
ans = []
77+
mx = 0
7878
for i in range(len(heights) - 1, -1, -1):
79-
v = heights[i]
80-
if mx < v:
79+
if heights[i] > mx:
8180
ans.append(i)
82-
mx = v
81+
mx = heights[i]
8382
return ans[::-1]
8483
```
8584

@@ -90,16 +89,17 @@ class Solution:
9089
```java
9190
class Solution {
9291
public int[] findBuildings(int[] heights) {
92+
int n = heights.length;
93+
List<Integer> ans = new ArrayList<>();
9394
int mx = 0;
94-
LinkedList<Integer> ans = new LinkedList<>();
9595
for (int i = heights.length - 1; i >= 0; --i) {
96-
int v = heights[i];
97-
if (mx < v) {
98-
ans.addFirst(i);
99-
mx = v;
96+
if (heights[i] > mx) {
97+
ans.add(i);
98+
mx = heights[i];
10099
}
101100
}
102-
return ans.stream().mapToInt(i -> i).toArray();
101+
Collections.reverse(ans);
102+
return ans.stream().mapToInt(Integer::intValue).toArray();
103103
}
104104
}
105105
```
@@ -110,13 +110,12 @@ class Solution {
110110
class Solution {
111111
public:
112112
vector<int> findBuildings(vector<int>& heights) {
113-
int mx = 0;
114113
vector<int> ans;
114+
int mx = 0;
115115
for (int i = heights.size() - 1; ~i; --i) {
116-
int v = heights[i];
117-
if (mx < v) {
116+
if (heights[i] > mx) {
118117
ans.push_back(i);
119-
mx = v;
118+
mx = heights[i];
120119
}
121120
}
122121
reverse(ans.begin(), ans.end());
@@ -128,20 +127,34 @@ public:
128127
### **Go**
129128
130129
```go
131-
func findBuildings(heights []int) []int {
130+
func findBuildings(heights []int) (ans []int) {
132131
mx := 0
133-
ans := []int{}
134132
for i := len(heights) - 1; i >= 0; i-- {
135-
v := heights[i]
136-
if mx < v {
133+
if v := heights[i]; v > mx {
137134
ans = append(ans, i)
138135
mx = v
139136
}
140137
}
141138
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
142139
ans[i], ans[j] = ans[j], ans[i]
143140
}
144-
return ans
141+
return
142+
}
143+
```
144+
145+
### **TypeScript**
146+
147+
```ts
148+
function findBuildings(heights: number[]): number[] {
149+
const ans: number[] = [];
150+
let mx = 0;
151+
for (let i = heights.length - 1; ~i; --i) {
152+
if (heights[i] > mx) {
153+
ans.push(i);
154+
mx = heights[i];
155+
}
156+
}
157+
return ans.reverse();
145158
}
146159
```
147160

@@ -153,13 +166,12 @@ func findBuildings(heights []int) []int {
153166
* @return {number[]}
154167
*/
155168
var findBuildings = function (heights) {
169+
const ans = [];
156170
let mx = 0;
157-
let ans = [];
158-
for (let i = heights.length - 1; i >= 0; --i) {
159-
const v = heights[i];
160-
if (mx < v) {
171+
for (let i = heights.length - 1; ~i; --i) {
172+
if (heights[i] > mx) {
161173
ans.push(i);
162-
mx = v;
174+
mx = heights[i];
163175
}
164176
}
165177
return ans.reverse();

solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md

+36-24
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@
5252
```python
5353
class Solution:
5454
def findBuildings(self, heights: List[int]) -> List[int]:
55-
mx = 0
5655
ans = []
56+
mx = 0
5757
for i in range(len(heights) - 1, -1, -1):
58-
v = heights[i]
59-
if mx < v:
58+
if heights[i] > mx:
6059
ans.append(i)
61-
mx = v
60+
mx = heights[i]
6261
return ans[::-1]
6362
```
6463

@@ -67,16 +66,17 @@ class Solution:
6766
```java
6867
class Solution {
6968
public int[] findBuildings(int[] heights) {
69+
int n = heights.length;
70+
List<Integer> ans = new ArrayList<>();
7071
int mx = 0;
71-
LinkedList<Integer> ans = new LinkedList<>();
7272
for (int i = heights.length - 1; i >= 0; --i) {
73-
int v = heights[i];
74-
if (mx < v) {
75-
ans.addFirst(i);
76-
mx = v;
73+
if (heights[i] > mx) {
74+
ans.add(i);
75+
mx = heights[i];
7776
}
7877
}
79-
return ans.stream().mapToInt(i -> i).toArray();
78+
Collections.reverse(ans);
79+
return ans.stream().mapToInt(Integer::intValue).toArray();
8080
}
8181
}
8282
```
@@ -87,13 +87,12 @@ class Solution {
8787
class Solution {
8888
public:
8989
vector<int> findBuildings(vector<int>& heights) {
90-
int mx = 0;
9190
vector<int> ans;
91+
int mx = 0;
9292
for (int i = heights.size() - 1; ~i; --i) {
93-
int v = heights[i];
94-
if (mx < v) {
93+
if (heights[i] > mx) {
9594
ans.push_back(i);
96-
mx = v;
95+
mx = heights[i];
9796
}
9897
}
9998
reverse(ans.begin(), ans.end());
@@ -105,20 +104,34 @@ public:
105104
### **Go**
106105
107106
```go
108-
func findBuildings(heights []int) []int {
107+
func findBuildings(heights []int) (ans []int) {
109108
mx := 0
110-
ans := []int{}
111109
for i := len(heights) - 1; i >= 0; i-- {
112-
v := heights[i]
113-
if mx < v {
110+
if v := heights[i]; v > mx {
114111
ans = append(ans, i)
115112
mx = v
116113
}
117114
}
118115
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
119116
ans[i], ans[j] = ans[j], ans[i]
120117
}
121-
return ans
118+
return
119+
}
120+
```
121+
122+
### **TypeScript**
123+
124+
```ts
125+
function findBuildings(heights: number[]): number[] {
126+
const ans: number[] = [];
127+
let mx = 0;
128+
for (let i = heights.length - 1; ~i; --i) {
129+
if (heights[i] > mx) {
130+
ans.push(i);
131+
mx = heights[i];
132+
}
133+
}
134+
return ans.reverse();
122135
}
123136
```
124137

@@ -130,13 +143,12 @@ func findBuildings(heights []int) []int {
130143
* @return {number[]}
131144
*/
132145
var findBuildings = function (heights) {
146+
const ans = [];
133147
let mx = 0;
134-
let ans = [];
135-
for (let i = heights.length - 1; i >= 0; --i) {
136-
const v = heights[i];
137-
if (mx < v) {
148+
for (let i = heights.length - 1; ~i; --i) {
149+
if (heights[i] > mx) {
138150
ans.push(i);
139-
mx = v;
151+
mx = heights[i];
140152
}
141153
}
142154
return ans.reverse();
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
class Solution {
2-
public:
3-
vector<int> findBuildings(vector<int>& heights) {
4-
int mx = 0;
5-
vector<int> ans;
6-
for (int i = heights.size() - 1; ~i; --i) {
7-
int v = heights[i];
8-
if (mx < v) {
9-
ans.push_back(i);
10-
mx = v;
11-
}
12-
}
13-
reverse(ans.begin(), ans.end());
14-
return ans;
15-
}
1+
class Solution {
2+
public:
3+
vector<int> findBuildings(vector<int>& heights) {
4+
vector<int> ans;
5+
int mx = 0;
6+
for (int i = heights.size() - 1; ~i; --i) {
7+
if (heights[i] > mx) {
8+
ans.push_back(i);
9+
mx = heights[i];
10+
}
11+
}
12+
reverse(ans.begin(), ans.end());
13+
return ans;
14+
}
1615
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
func findBuildings(heights []int) []int {
1+
func findBuildings(heights []int) (ans []int) {
22
mx := 0
3-
ans := []int{}
43
for i := len(heights) - 1; i >= 0; i-- {
5-
v := heights[i]
6-
if mx < v {
4+
if v := heights[i]; v > mx {
75
ans = append(ans, i)
86
mx = v
97
}
108
}
119
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
1210
ans[i], ans[j] = ans[j], ans[i]
1311
}
14-
return ans
12+
return
1513
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
class Solution {
2-
public int[] findBuildings(int[] heights) {
3-
int mx = 0;
4-
LinkedList<Integer> ans = new LinkedList<>();
5-
for (int i = heights.length - 1; i >= 0; --i) {
6-
int v = heights[i];
7-
if (mx < v) {
8-
ans.addFirst(i);
9-
mx = v;
10-
}
11-
}
12-
return ans.stream().mapToInt(i -> i).toArray();
13-
}
1+
class Solution {
2+
public int[] findBuildings(int[] heights) {
3+
int n = heights.length;
4+
List<Integer> ans = new ArrayList<>();
5+
int mx = 0;
6+
for (int i = heights.length - 1; i >= 0; --i) {
7+
if (heights[i] > mx) {
8+
ans.add(i);
9+
mx = heights[i];
10+
}
11+
}
12+
Collections.reverse(ans);
13+
return ans.stream().mapToInt(Integer::intValue).toArray();
14+
}
1415
}

solution/1700-1799/1762.Buildings With an Ocean View/Solution.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
* @return {number[]}
44
*/
55
var findBuildings = function (heights) {
6+
const ans = [];
67
let mx = 0;
7-
let ans = [];
8-
for (let i = heights.length - 1; i >= 0; --i) {
9-
const v = heights[i];
10-
if (mx < v) {
8+
for (let i = heights.length - 1; ~i; --i) {
9+
if (heights[i] > mx) {
1110
ans.push(i);
12-
mx = v;
11+
mx = heights[i];
1312
}
1413
}
1514
return ans.reverse();
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
class Solution:
2-
def findBuildings(self, heights: List[int]) -> List[int]:
3-
mx = 0
4-
ans = []
5-
for i in range(len(heights) - 1, -1, -1):
6-
v = heights[i]
7-
if mx < v:
8-
ans.append(i)
9-
mx = v
10-
return ans[::-1]
1+
class Solution:
2+
def findBuildings(self, heights: List[int]) -> List[int]:
3+
ans = []
4+
mx = 0
5+
for i in range(len(heights) - 1, -1, -1):
6+
if heights[i] > mx:
7+
ans.append(i)
8+
mx = heights[i]
9+
return ans[::-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function findBuildings(heights: number[]): number[] {
2+
const ans: number[] = [];
3+
let mx = 0;
4+
for (let i = heights.length - 1; ~i; --i) {
5+
if (heights[i] > mx) {
6+
ans.push(i);
7+
mx = heights[i];
8+
}
9+
}
10+
return ans.reverse();
11+
}

0 commit comments

Comments
 (0)