Skip to content

Commit c45bac4

Browse files
committed
feat: add solutions to lc problem: No.1732
No.1732.Find the Highest Altitude
1 parent 5d4b5dc commit c45bac4

File tree

7 files changed

+141
-89
lines changed

7 files changed

+141
-89
lines changed

solution/1700-1799/1732.Find the Highest Altitude/README.md

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,25 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
求前 N 项和的最大值即可。
45+
**方法一:前缀和(差分数组)**
46+
47+
我们假设每个点的海拔为 $h_i$,由于 $gain[i]$ 表示第 $i$ 个点和第 $i + 1$ 个点的海拔差,因此 $gain[i] = h_{i + 1} - h_i$。那么:
48+
49+
$$
50+
\sum_{i = 0}^{n-1} gain[i] = h_1 - h_0 + h_2 - h_1 + \cdots + h_n - h_{n - 1} = h_n - h_0 = h_n
51+
$$
52+
53+
即:
54+
55+
$$
56+
h_{i+1} = \sum_{j = 0}^{i} gain[j]
57+
$$
58+
59+
可以发现,每个点的海拔都可以通过前缀和的方式计算出来。因此,我们只需要遍历一遍数组,求出前缀和的最大值,即为最高点的海拔。
60+
61+
> 实际上题目中的 $gain$ 数组是一个差分数组,对差分数组求前缀和即可得到原海拔数组。然后求出原海拔数组的最大值即可。
62+
63+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $gain$ 的长度。
4664

4765
<!-- tabs:start -->
4866

@@ -53,11 +71,17 @@
5371
```python
5472
class Solution:
5573
def largestAltitude(self, gain: List[int]) -> int:
56-
res = t = 0
57-
for h in gain:
58-
t += h
59-
res = max(res, t)
60-
return res
74+
return max(accumulate(gain, initial=0))
75+
```
76+
77+
```python
78+
class Solution:
79+
def largestAltitude(self, gain: List[int]) -> int:
80+
ans = s = 0
81+
for v in gain:
82+
s += v
83+
ans = max(ans, s)
84+
return ans
6185
```
6286

6387
### **Java**
@@ -67,13 +91,12 @@ class Solution:
6791
```java
6892
class Solution {
6993
public int largestAltitude(int[] gain) {
70-
int res = 0;
71-
int t = 0;
72-
for (int h : gain) {
73-
t += h;
74-
res = Math.max(res, t);
94+
int ans = 0, s = 0;
95+
for (int v : gain) {
96+
s += v;
97+
ans = Math.max(ans, s);
7598
}
76-
return res;
99+
return ans;
77100
}
78101
}
79102
```
@@ -84,34 +107,44 @@ class Solution {
84107
class Solution {
85108
public:
86109
int largestAltitude(vector<int>& gain) {
87-
int res = 0, t = 0;
88-
for (int h : gain) {
89-
t += h;
90-
res = max(res, t);
91-
}
92-
return res;
110+
int ans = 0, s = 0;
111+
for (int v : gain) s += v, ans = max(ans, s);
112+
return ans;
93113
}
94114
};
95115
```
96116
97117
### **Go**
98118
99119
```go
100-
func largestAltitude(gain []int) int {
101-
res, t := 0, 0
102-
for _, h := range gain {
103-
t += h
104-
res = max(res, t)
120+
func largestAltitude(gain []int) (ans int) {
121+
s := 0
122+
for _, v := range gain {
123+
s += v
124+
if ans < s {
125+
ans = s
126+
}
105127
}
106-
return res
128+
return
107129
}
130+
```
108131

109-
func max(a, b int) int {
110-
if a > b {
111-
return a
112-
}
113-
return b
114-
}
132+
### **JavaScript**
133+
134+
```js
135+
/**
136+
* @param {number[]} gain
137+
* @return {number}
138+
*/
139+
var largestAltitude = function (gain) {
140+
let ans = 0;
141+
let s = 0;
142+
for (const v of gain) {
143+
s += v;
144+
ans = Math.max(ans, s);
145+
}
146+
return ans;
147+
};
115148
```
116149

117150
### **...**

solution/1700-1799/1732.Find the Highest Altitude/README_EN.md

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,30 @@
4343
```python
4444
class Solution:
4545
def largestAltitude(self, gain: List[int]) -> int:
46-
res = t = 0
47-
for h in gain:
48-
t += h
49-
res = max(res, t)
50-
return res
46+
return max(accumulate(gain, initial=0))
47+
```
48+
49+
```python
50+
class Solution:
51+
def largestAltitude(self, gain: List[int]) -> int:
52+
ans = s = 0
53+
for v in gain:
54+
s += v
55+
ans = max(ans, s)
56+
return ans
5157
```
5258

5359
### **Java**
5460

5561
```java
5662
class Solution {
5763
public int largestAltitude(int[] gain) {
58-
int res = 0;
59-
int t = 0;
60-
for (int h : gain) {
61-
t += h;
62-
res = Math.max(res, t);
64+
int ans = 0, s = 0;
65+
for (int v : gain) {
66+
s += v;
67+
ans = Math.max(ans, s);
6368
}
64-
return res;
69+
return ans;
6570
}
6671
}
6772
```
@@ -72,34 +77,44 @@ class Solution {
7277
class Solution {
7378
public:
7479
int largestAltitude(vector<int>& gain) {
75-
int res = 0, t = 0;
76-
for (int h : gain) {
77-
t += h;
78-
res = max(res, t);
79-
}
80-
return res;
80+
int ans = 0, s = 0;
81+
for (int v : gain) s += v, ans = max(ans, s);
82+
return ans;
8183
}
8284
};
8385
```
8486
8587
### **Go**
8688
8789
```go
88-
func largestAltitude(gain []int) int {
89-
res, t := 0, 0
90-
for _, h := range gain {
91-
t += h
92-
res = max(res, t)
90+
func largestAltitude(gain []int) (ans int) {
91+
s := 0
92+
for _, v := range gain {
93+
s += v
94+
if ans < s {
95+
ans = s
96+
}
9397
}
94-
return res
98+
return
9599
}
100+
```
96101

97-
func max(a, b int) int {
98-
if a > b {
99-
return a
100-
}
101-
return b
102-
}
102+
### **JavaScript**
103+
104+
```js
105+
/**
106+
* @param {number[]} gain
107+
* @return {number}
108+
*/
109+
var largestAltitude = function (gain) {
110+
let ans = 0;
111+
let s = 0;
112+
for (const v of gain) {
113+
s += v;
114+
ans = Math.max(ans, s);
115+
}
116+
return ans;
117+
};
103118
```
104119

105120
### **...**
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
class Solution {
22
public:
33
int largestAltitude(vector<int>& gain) {
4-
int res = 0, t = 0;
5-
for (int h : gain) {
6-
t += h;
7-
res = max(res, t);
8-
}
9-
return res;
4+
int ans = 0, s = 0;
5+
for (int v : gain) s += v, ans = max(ans, s);
6+
return ans;
107
}
118
};
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
func largestAltitude(gain []int) int {
2-
res, t := 0, 0
3-
for _, h := range gain {
4-
t += h
5-
res = max(res, t)
1+
func largestAltitude(gain []int) (ans int) {
2+
s := 0
3+
for _, v := range gain {
4+
s += v
5+
if ans < s {
6+
ans = s
7+
}
68
}
7-
return res
8-
}
9-
10-
func max(a, b int) int {
11-
if a > b {
12-
return a
13-
}
14-
return b
9+
return
1510
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution {
22
public int largestAltitude(int[] gain) {
3-
int res = 0;
4-
int t = 0;
5-
for (int h : gain) {
6-
t += h;
7-
res = Math.max(res, t);
3+
int ans = 0, s = 0;
4+
for (int v : gain) {
5+
s += v;
6+
ans = Math.max(ans, s);
87
}
9-
return res;
8+
return ans;
109
}
1110
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number[]} gain
3+
* @return {number}
4+
*/
5+
var largestAltitude = function (gain) {
6+
let ans = 0;
7+
let s = 0;
8+
for (const v of gain) {
9+
s += v;
10+
ans = Math.max(ans, s);
11+
}
12+
return ans;
13+
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def largestAltitude(self, gain: List[int]) -> int:
3-
res = t = 0
4-
for h in gain:
5-
t += h
6-
res = max(res, t)
7-
return res
3+
ans = s = 0
4+
for v in gain:
5+
s += v
6+
ans = max(ans, s)
7+
return ans

0 commit comments

Comments
 (0)