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

feat: add solutions to lc problem: No.1762 #1716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 2 commits into main from dev
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 39 additions & 27 deletions solution/1700-1799/1762.Buildings With an Ocean View/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@

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

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

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

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

<!-- tabs:start -->

Expand All @@ -73,13 +73,12 @@
```python
class Solution:
def findBuildings(self, heights: List[int]) -> List[int]:
mx = 0
ans = []
mx = 0
for i in range(len(heights) - 1, -1, -1):
v = heights[i]
if mx < v:
if heights[i] > mx:
ans.append(i)
mx = v
mx = heights[i]
return ans[::-1]
```

Expand All @@ -90,16 +89,17 @@ class Solution:
```java
class Solution {
public int[] findBuildings(int[] heights) {
int n = heights.length;
List<Integer> ans = new ArrayList<>();
int mx = 0;
LinkedList<Integer> ans = new LinkedList<>();
for (int i = heights.length - 1; i >= 0; --i) {
int v = heights[i];
if (mx < v) {
ans.addFirst(i);
mx = v;
if (heights[i] > mx) {
ans.add(i);
mx = heights[i];
}
}
return ans.stream().mapToInt(i -> i).toArray();
Collections.reverse(ans);
return ans.stream().mapToInt(Integer::intValue).toArray();
}
}
```
Expand All @@ -110,13 +110,12 @@ class Solution {
class Solution {
public:
vector<int> findBuildings(vector<int>& heights) {
int mx = 0;
vector<int> ans;
int mx = 0;
for (int i = heights.size() - 1; ~i; --i) {
int v = heights[i];
if (mx < v) {
if (heights[i] > mx) {
ans.push_back(i);
mx = v;
mx = heights[i];
}
}
reverse(ans.begin(), ans.end());
Expand All @@ -128,20 +127,34 @@ public:
### **Go**

```go
func findBuildings(heights []int) []int {
func findBuildings(heights []int) (ans []int) {
mx := 0
ans := []int{}
for i := len(heights) - 1; i >= 0; i-- {
v := heights[i]
if mx < v {
if v := heights[i]; v > mx {
ans = append(ans, i)
mx = v
}
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return ans
return
}
```

### **TypeScript**

```ts
function findBuildings(heights: number[]): number[] {
const ans: number[] = [];
let mx = 0;
for (let i = heights.length - 1; ~i; --i) {
if (heights[i] > mx) {
ans.push(i);
mx = heights[i];
}
}
return ans.reverse();
}
```

Expand All @@ -153,13 +166,12 @@ func findBuildings(heights []int) []int {
* @return {number[]}
*/
var findBuildings = function (heights) {
const ans = [];
let mx = 0;
let ans = [];
for (let i = heights.length - 1; i >= 0; --i) {
const v = heights[i];
if (mx < v) {
for (let i = heights.length - 1; ~i; --i) {
if (heights[i] > mx) {
ans.push(i);
mx = v;
mx = heights[i];
}
}
return ans.reverse();
Expand Down
60 changes: 36 additions & 24 deletions solution/1700-1799/1762.Buildings With an Ocean View/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@
```python
class Solution:
def findBuildings(self, heights: List[int]) -> List[int]:
mx = 0
ans = []
mx = 0
for i in range(len(heights) - 1, -1, -1):
v = heights[i]
if mx < v:
if heights[i] > mx:
ans.append(i)
mx = v
mx = heights[i]
return ans[::-1]
```

Expand All @@ -67,16 +66,17 @@ class Solution:
```java
class Solution {
public int[] findBuildings(int[] heights) {
int n = heights.length;
List<Integer> ans = new ArrayList<>();
int mx = 0;
LinkedList<Integer> ans = new LinkedList<>();
for (int i = heights.length - 1; i >= 0; --i) {
int v = heights[i];
if (mx < v) {
ans.addFirst(i);
mx = v;
if (heights[i] > mx) {
ans.add(i);
mx = heights[i];
}
}
return ans.stream().mapToInt(i -> i).toArray();
Collections.reverse(ans);
return ans.stream().mapToInt(Integer::intValue).toArray();
}
}
```
Expand All @@ -87,13 +87,12 @@ class Solution {
class Solution {
public:
vector<int> findBuildings(vector<int>& heights) {
int mx = 0;
vector<int> ans;
int mx = 0;
for (int i = heights.size() - 1; ~i; --i) {
int v = heights[i];
if (mx < v) {
if (heights[i] > mx) {
ans.push_back(i);
mx = v;
mx = heights[i];
}
}
reverse(ans.begin(), ans.end());
Expand All @@ -105,20 +104,34 @@ public:
### **Go**

```go
func findBuildings(heights []int) []int {
func findBuildings(heights []int) (ans []int) {
mx := 0
ans := []int{}
for i := len(heights) - 1; i >= 0; i-- {
v := heights[i]
if mx < v {
if v := heights[i]; v > mx {
ans = append(ans, i)
mx = v
}
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return ans
return
}
```

### **TypeScript**

```ts
function findBuildings(heights: number[]): number[] {
const ans: number[] = [];
let mx = 0;
for (let i = heights.length - 1; ~i; --i) {
if (heights[i] > mx) {
ans.push(i);
mx = heights[i];
}
}
return ans.reverse();
}
```

Expand All @@ -130,13 +143,12 @@ func findBuildings(heights []int) []int {
* @return {number[]}
*/
var findBuildings = function (heights) {
const ans = [];
let mx = 0;
let ans = [];
for (let i = heights.length - 1; i >= 0; --i) {
const v = heights[i];
if (mx < v) {
for (let i = heights.length - 1; ~i; --i) {
if (heights[i] > mx) {
ans.push(i);
mx = v;
mx = heights[i];
}
}
return ans.reverse();
Expand Down
29 changes: 14 additions & 15 deletions solution/1700-1799/1762.Buildings With an Ocean View/Solution.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
class Solution {
public:
vector<int> findBuildings(vector<int>& heights) {
int mx = 0;
vector<int> ans;
for (int i = heights.size() - 1; ~i; --i) {
int v = heights[i];
if (mx < v) {
ans.push_back(i);
mx = v;
}
}
reverse(ans.begin(), ans.end());
return ans;
}
class Solution {
public:
vector<int> findBuildings(vector<int>& heights) {
vector<int> ans;
int mx = 0;
for (int i = heights.size() - 1; ~i; --i) {
if (heights[i] > mx) {
ans.push_back(i);
mx = heights[i];
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
func findBuildings(heights []int) []int {
func findBuildings(heights []int) (ans []int) {
mx := 0
ans := []int{}
for i := len(heights) - 1; i >= 0; i-- {
v := heights[i]
if mx < v {
if v := heights[i]; v > mx {
ans = append(ans, i)
mx = v
}
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return ans
return
}
27 changes: 14 additions & 13 deletions solution/1700-1799/1762.Buildings With an Ocean View/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
class Solution {
public int[] findBuildings(int[] heights) {
int mx = 0;
LinkedList<Integer> ans = new LinkedList<>();
for (int i = heights.length - 1; i >= 0; --i) {
int v = heights[i];
if (mx < v) {
ans.addFirst(i);
mx = v;
}
}
return ans.stream().mapToInt(i -> i).toArray();
}
class Solution {
public int[] findBuildings(int[] heights) {
int n = heights.length;
List<Integer> ans = new ArrayList<>();
int mx = 0;
for (int i = heights.length - 1; i >= 0; --i) {
if (heights[i] > mx) {
ans.add(i);
mx = heights[i];
}
}
Collections.reverse(ans);
return ans.stream().mapToInt(Integer::intValue).toArray();
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
* @return {number[]}
*/
var findBuildings = function (heights) {
const ans = [];
let mx = 0;
let ans = [];
for (let i = heights.length - 1; i >= 0; --i) {
const v = heights[i];
if (mx < v) {
for (let i = heights.length - 1; ~i; --i) {
if (heights[i] > mx) {
ans.push(i);
mx = v;
mx = heights[i];
}
}
return ans.reverse();
Expand Down
19 changes: 9 additions & 10 deletions solution/1700-1799/1762.Buildings With an Ocean View/Solution.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class Solution:
def findBuildings(self, heights: List[int]) -> List[int]:
mx = 0
ans = []
for i in range(len(heights) - 1, -1, -1):
v = heights[i]
if mx < v:
ans.append(i)
mx = v
return ans[::-1]
class Solution:
def findBuildings(self, heights: List[int]) -> List[int]:
ans = []
mx = 0
for i in range(len(heights) - 1, -1, -1):
if heights[i] > mx:
ans.append(i)
mx = heights[i]
return ans[::-1]
11 changes: 11 additions & 0 deletions solution/1700-1799/1762.Buildings With an Ocean View/Solution.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function findBuildings(heights: number[]): number[] {
const ans: number[] = [];
let mx = 0;
for (let i = heights.length - 1; ~i; --i) {
if (heights[i] > mx) {
ans.push(i);
mx = heights[i];
}
}
return ans.reverse();
}
Loading

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