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

Appearance settings

Latest commit

 

History

History
History
22 lines (21 loc) · 727 Bytes

File metadata and controls

22 lines (21 loc) · 727 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package leetcode;
public class leetcode845 {
public static void main(String[] args) {
new leetcode845().longestMountain(new int[]{
2,1,4,7,3,2,5
});
}
//最简单的贪心法
public int longestMountain(int[] A) {
int result=0;
for(int i=1;i<A.length;i++){
int rightIndex=i;
int leftIndex=i;
while(rightIndex+1<A.length&&A[rightIndex]>A[rightIndex+1]){rightIndex++;}
while(leftIndex-1>-1&&A[leftIndex]>A[leftIndex-1]){leftIndex--;}
int temp=rightIndex-leftIndex+1<3||rightIndex==i||leftIndex==i?0:rightIndex-leftIndex+1;
if(result<temp) result=temp;
}
return result;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.