Skip to content

Commit

Permalink
Create longest-mountain-in-array.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jun 3, 2018
1 parent 08ef2a0 commit 3295aac
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions C++/longest-mountain-in-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Time: O(n)
// Space: O(1)

class Solution {
public:
int longestMountain(vector<int>& A) {
int result = 0, up_len = 0, down_len = 0;
for (int i = 1; i < A.size(); ++i) {
if ((down_len && A[i - 1] < A[i]) ||
A[i - 1] == A[i]) {
up_len = down_len = 0;
}
up_len += A[i - 1] < A[i];
down_len += A[i - 1] > A[i];
if (up_len && down_len) {
result= max(result, up_len + down_len + 1);
}
}
return result;
}
};

0 comments on commit 3295aac

Please sign in to comment.