Skip to content

Commit 394dffb

Browse files
committed
feat: add solutions to lc problem: No.1762
No.1762.Buildings With an Ocean View
1 parent 87c04c5 commit 394dffb

File tree

16 files changed

+457
-36
lines changed

16 files changed

+457
-36
lines changed

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

+93-1
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,114 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:逆序遍历求右侧最大值**
60+
61+
逆序遍历数组 $height$ 每个元素 $v$,判断 $v$ 与右侧最大元素 $mx$ 的大小关系,若 $mx \lt v$,说明右侧所有元素都比当前元素小,当前位置能看到海景,加入结果数组 $ans$。
62+
63+
最后逆序返回 $ans$。
64+
65+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
66+
5967
<!-- tabs:start -->
6068

6169
### **Python3**
6270

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

6573
```python
66-
74+
class Solution:
75+
def findBuildings(self, heights: List[int]) -> List[int]:
76+
mx = 0
77+
ans = []
78+
for i in range(len(heights) - 1, -1, -1):
79+
v = heights[i]
80+
if mx < v:
81+
ans.append(i)
82+
mx = v
83+
return ans[::-1]
6784
```
6885

6986
### **Java**
7087

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

7390
```java
91+
class Solution {
92+
public int[] findBuildings(int[] heights) {
93+
int mx = 0;
94+
LinkedList<Integer> ans = new LinkedList<>();
95+
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;
100+
}
101+
}
102+
return ans.stream().mapToInt(i -> i).toArray();
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
vector<int> findBuildings(vector<int>& heights) {
113+
int mx = 0;
114+
vector<int> ans;
115+
for (int i = heights.size() - 1; ~i; --i) {
116+
int v = heights[i];
117+
if (mx < v) {
118+
ans.push_back(i);
119+
mx = v;
120+
}
121+
}
122+
reverse(ans.begin(), ans.end());
123+
return ans;
124+
}
125+
};
126+
```
127+
128+
### **Go**
129+
130+
```go
131+
func findBuildings(heights []int) []int {
132+
mx := 0
133+
ans := []int{}
134+
for i := len(heights) - 1; i >= 0; i-- {
135+
v := heights[i]
136+
if mx < v {
137+
ans = append(ans, i)
138+
mx = v
139+
}
140+
}
141+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
142+
ans[i], ans[j] = ans[j], ans[i]
143+
}
144+
return ans
145+
}
146+
```
74147

148+
### **JavaScript**
149+
150+
```js
151+
/**
152+
* @param {number[]} heights
153+
* @return {number[]}
154+
*/
155+
var findBuildings = function (heights) {
156+
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) {
161+
ans.push(i);
162+
mx = v;
163+
}
164+
}
165+
return ans.reverse();
166+
};
75167
```
76168

77169
### **...**

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

+85-1
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,97 @@
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def findBuildings(self, heights: List[int]) -> List[int]:
55+
mx = 0
56+
ans = []
57+
for i in range(len(heights) - 1, -1, -1):
58+
v = heights[i]
59+
if mx < v:
60+
ans.append(i)
61+
mx = v
62+
return ans[::-1]
5463
```
5564

5665
### **Java**
5766

5867
```java
68+
class Solution {
69+
public int[] findBuildings(int[] heights) {
70+
int mx = 0;
71+
LinkedList<Integer> ans = new LinkedList<>();
72+
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;
77+
}
78+
}
79+
return ans.stream().mapToInt(i -> i).toArray();
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
vector<int> findBuildings(vector<int>& heights) {
90+
int mx = 0;
91+
vector<int> ans;
92+
for (int i = heights.size() - 1; ~i; --i) {
93+
int v = heights[i];
94+
if (mx < v) {
95+
ans.push_back(i);
96+
mx = v;
97+
}
98+
}
99+
reverse(ans.begin(), ans.end());
100+
return ans;
101+
}
102+
};
103+
```
104+
105+
### **Go**
106+
107+
```go
108+
func findBuildings(heights []int) []int {
109+
mx := 0
110+
ans := []int{}
111+
for i := len(heights) - 1; i >= 0; i-- {
112+
v := heights[i]
113+
if mx < v {
114+
ans = append(ans, i)
115+
mx = v
116+
}
117+
}
118+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
119+
ans[i], ans[j] = ans[j], ans[i]
120+
}
121+
return ans
122+
}
123+
```
59124

125+
### **JavaScript**
126+
127+
```js
128+
/**
129+
* @param {number[]} heights
130+
* @return {number[]}
131+
*/
132+
var findBuildings = function (heights) {
133+
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) {
138+
ans.push(i);
139+
mx = v;
140+
}
141+
}
142+
return ans.reverse();
143+
};
60144
```
61145

62146
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func findBuildings(heights []int) []int {
2+
mx := 0
3+
ans := []int{}
4+
for i := len(heights) - 1; i >= 0; i-- {
5+
v := heights[i]
6+
if mx < v {
7+
ans = append(ans, i)
8+
mx = v
9+
}
10+
}
11+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
12+
ans[i], ans[j] = ans[j], ans[i]
13+
}
14+
return ans
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} heights
3+
* @return {number[]}
4+
*/
5+
var findBuildings = function (heights) {
6+
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) {
11+
ans.push(i);
12+
mx = v;
13+
}
14+
}
15+
return ans.reverse();
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [2371. Minimize Maximum Value in a Grid](https://leetcode.cn/problems/minimize-maximum-value-in-a-grid)
2+
3+
[English Version](/solution/2300-2399/2371.Minimize%20Maximum%20Value%20in%20a%20Grid/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given an <code>m x n</code> integer matrix <code>grid</code> containing <strong>distinct</strong> positive integers.</p>
10+
11+
<p>You have to replace each integer in the matrix with a positive integer satisfying the following conditions:</p>
12+
13+
<ul>
14+
<li>The <strong>relative</strong> order of every two elements that are in the same row or column should stay the <strong>same</strong> after the replacements.</li>
15+
<li>The <strong>maximum</strong> number in the matrix after the replacements should be as <strong>small</strong> as possible.</li>
16+
</ul>
17+
18+
<p>The relative order stays the same if for all pairs of elements in the original matrix such that <code>grid[r<sub>1</sub>][c<sub>1</sub>] &gt; grid[r<sub>2</sub>][c<sub>2</sub>]</code> where either <code>r<sub>1</sub> == r<sub>2</sub></code> or <code>c<sub>1</sub> == c<sub>2</sub></code>, then it must be true that <code>grid[r<sub>1</sub>][c<sub>1</sub>] &gt; grid[r<sub>2</sub>][c<sub>2</sub>]</code> after the replacements.</p>
19+
20+
<p>For example, if <code>grid = [[2, 4, 5], [7, 3, 9]]</code> then a good replacement could be either <code>grid = [[1, 2, 3], [2, 1, 4]]</code> or <code>grid = [[1, 2, 3], [3, 1, 4]]</code>.</p>
21+
22+
<p>Return <em>the <strong>resulting</strong> matrix.</em> If there are multiple answers, return <strong>any</strong> of them.</p>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Example 1:</strong></p>
26+
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/09/grid2drawio.png" style="width: 371px; height: 121px;" />
27+
<pre>
28+
<strong>Input:</strong> grid = [[3,1],[2,5]]
29+
<strong>Output:</strong> [[2,1],[1,2]]
30+
<strong>Explanation:</strong> The above diagram shows a valid replacement.
31+
The maximum number in the matrix is 2. It can be shown that no smaller value can be obtained.
32+
</pre>
33+
34+
<p><strong>Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> grid = [[10]]
38+
<strong>Output:</strong> [[1]]
39+
<strong>Explanation:</strong> We replace the only number in the matrix with 1.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>m == grid.length</code></li>
47+
<li><code>n == grid[i].length</code></li>
48+
<li><code>1 &lt;= m, n &lt;= 1000</code></li>
49+
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
50+
<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>9</sup></code></li>
51+
<li><code>grid</code> consists of distinct integers.</li>
52+
</ul>
53+
54+
55+
## 解法
56+
57+
<!-- 这里可写通用的实现逻辑 -->
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
<!-- 这里可写当前语言的特殊实现逻辑 -->
64+
65+
```python
66+
67+
```
68+
69+
### **Java**
70+
71+
<!-- 这里可写当前语言的特殊实现逻辑 -->
72+
73+
```java
74+
75+
```
76+
77+
### **TypeScript**
78+
79+
```ts
80+
81+
```
82+
83+
### **...**
84+
85+
```
86+
87+
```
88+
89+
<!-- tabs:end -->

0 commit comments

Comments
 (0)