Practice

Problem 10 of 12

Problem · Medium · 25 min

The longest run of few kinds

  • Medium
  • Sliding window
  • Arrays

A bird watcher writes down the bird that she sees each minute. The list has one name for each minute, for example ["owl", "wren", "owl", "crow"].

Write longestRun(sightings, maxKinds). It returns the length of the longest run of neighbouring minutes in which she saw at most maxKinds different birds.

  • A run is a part of the list without gaps: the positions from i to j for some i <= j.
  • maxKinds is a whole number that is at least 1.
  • For an empty list, the answer is 0.

Examples

Call Result
longestRun(['owl', 'wren', 'owl', 'crow'], 2) 3
longestRun(['owl', 'owl', 'owl'], 1) 3
longestRun([], 2) 0

Tasks

2 to do

  1. Task 1, not checked yet. Make longestRun pass the example tests.

  2. Task 2, not checked yet. Make longestRun pass the hidden tests.

Task 1 of 2

Make longestRun pass the example tests.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.