Skip to content
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

Longest Peak #258

Closed
drkennetz opened this issue Jan 28, 2022 · 8 comments
Closed

Longest Peak #258

drkennetz opened this issue Jan 28, 2022 · 8 comments
Labels
proposed challenge An idea for a future challenge

Comments

@drkennetz
Copy link
Collaborator

drkennetz commented Jan 28, 2022

Longest Peak

Write a function that takes in an array of integers and returns the length of the longest peak in the array.

A peak is defined as adjacent integers in the array that are strictly increasing until they reach a tip (the highest value in the peak), at which point they become strictly decreasing. At least three integers are required to form a peak.

I will make this abundantly clear in the examples.

Business Rules/Errata

  1. A peak must increase and then decrease.
  2. The input can be an empty array, and must be an array of integers.
  3. If equal value numbers appear in the run of a peak, the peak is stopped and restarted.
  4. A peak must contain at least 3 numbers, such as 1,2,1
  5. Numbers can be positive or negative.
  6. Bonus: Optimal time and space: O(n) time | O(1) space - where n is the length of the input array.

Examples

longest_peak([5, 4, 3, 2, 1]) -> 0 # all decreasing
longest_peak([1, 2, 3, 4, 5]) -> 0 # all increasing
longest_peak([5, 4, 3, 2, 1, 2, 10, 12]) -> 0 # all decreasing, then all increasing
longest_peak([]) -> 0 # empty input check mandatory :D
longest_peak([1, 3, 2]) -> 3 # normal peak
longest_peak([1, 2, 3, 4, 5, 1]) -> 6 # peak ends on last number
longest_peak([2,2,3,2]) -> 3 # repeat at the beginning
longest_peak([1, 2, 3, 2, 1, 1]) -> 5 # repeat at the end
longest_peak([1, 2, 3, 3, 2, 1]) -> 0 # equality at peak tip, 0 is returned
longest_peak([1, 1, 1, 2, 3, 10, 12, -3, 2, 3, 45, 800, 99, 98, 0, -1, 2, 3, 4, 5, 0, -1]) -> 9 # peak in the middle -3, 2, 3, 45, 800, 99, 98, 0, -1
@drkennetz drkennetz added the proposed challenge An idea for a future challenge label Jan 28, 2022
@mrumiker
Copy link
Collaborator

mrumiker commented Feb 7, 2022

Cool problem. I noticed that the examples never tested a scenario where a peak is ended by an immediate uphill (you always had repeated numbers doing this job). Edited some of the plateaus out of the last example to remedy this.

@xanderyzwich
Copy link
Collaborator

It seems to be inferred but not directly said that the decline of the peak must be the same length as the incline. Is that correct?

@mrumiker
Copy link
Collaborator

@xanderyzwich Doesn't have to be - check out the [2, 2, 3, 2] example

@xanderyzwich
Copy link
Collaborator

@mrumiker That's a run of 3 [2, 3, 2] with rise and fall both being 1

@mrumiker
Copy link
Collaborator

@xanderyzwich You're right! From the instructions, I interpret a peak as any increasing run of numbers followed immediately by a decreasing run. Any "plateau" (a pair of equal numbers) would end the peak, as would any increasing pair occurring on the downhill portion.

@xanderyzwich
Copy link
Collaborator

So it seems like I'm wrong if this one is right

longest_peak([1, 2, 3, 4, 5, 1]) -> 6 # peak ends on last number

@xanderyzwich
Copy link
Collaborator

closed by #272

@mrumiker
Copy link
Collaborator

mrumiker commented Mar 1, 2022

Sorry about the mixup on this - I realize my comment where I said "You're right!" wasn't clear. I was trying to say "You're right about the example I mentioned, but I still don't think the increase and decrease have to be equal based on what the directions say." I wish I'd actually noticed the example that proved my point!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposed challenge An idea for a future challenge
Projects
None yet
Development

No branches or pull requests

3 participants