FastPrepπŸ¦‰ Collect Sticks

πŸ¦‰ Collect Sticks

Instacart logoInstacart● EasyFULLTIMEOA
Learn

Problem statement

You are given an array forest and the bird's starting position bird.

forest[i] represents the length of the stick at position i.

If forest[i] == 0, it means there is no stick at position i.

The bird's starting position bird will always be an index where there is no stick (forest[bird] == 0).

The bird follows this process:

  • Starting from the initial position, the bird flies right until it finds the first stick.
  • It picks up the stick and brings it back to the starting position.
  • Next, the bird flies left until it finds the next stick.
  • It picks up that stick and brings it back to the starting position.
  • The bird repeats this process: alternating right and left until it collects sticks with a total length of at least 100.

You need to output the indices of the sticks that the bird picks up, in the order they are collected.

Note:

  • There will always be a stick available in the current flying direction.
  • No edge cases like "flying right and finding no sticks" will occur.

Function

collectSticks(forest: int[], bird: int) β†’ List<Integer>

Examples

Example 1

forest = [0, 50, 0, 30, 0, 25]bird = 2return = [3, 1, 5]

Pls ignore the explanation in the source image.

Constraints

  • πŸ¦‰

More Instacart problems

See Instacart hiring insights
public List<Integer> collectSticks(int[] forest, int bird) {
  // write your code here
}
forest[0, 50, 0, 30, 0, 25]
bird2
expected[3, 1, 5]
Checking account…