FastPrepBook Reading Evenings

Book Reading Evenings

Google logoGoogle● EasyINTERNNEW GRADOA

Problem statement

A book contains chapters that must be read in order. The integer array chapter gives the minutes needed to finish each chapter, and evening[j] gives the minutes available on the j-th evening.

During each evening, read as many consecutive unread chapters as possible. A chapter may be started only when it can be finished during that same evening, so a chapter is never split across evenings. Any unused time at the end of an evening is discarded.

Return the one-based number of the evening on which the final chapter is finished. If the book cannot be finished within the supplied evenings, return -1.

Function

solution(chapter: int[], evening: int[]) → int

Examples

Example 1

chapter = [3,2,1]evening = [2,5,3,3]return = 3

No chapter fits on evening 1. The first two chapters fit on evening 2, and the last chapter finishes on evening 3.

Example 2

chapter = [5,8,5]evening = [9,4,5,3,8]return = -1

The first chapter finishes on evening 1. The second chapter cannot finish until evening 5, leaving no evening for the final chapter.

Example 3

chapter = [3,4,7]evening = [5,6,7]return = 3

Exactly one chapter is completed on each evening, so the book finishes on evening 3.

Constraints

  • 1 <= chapter.length, evening.length <= 100000.
  • 1 <= chapter[i], evening[j] <= 10^9.

Source note: The gallery combines the original Google Coding Exercise statement with a later screenshot of all three original examples; FastPrep's extra bounds and language wrappers are practice adapters.

More Google problems

See Google hiring insights
Saved
public int solution(int[] chapter, int[] evening) {
  // write your code here
}
chapter[3,2,1]
evening[2,5,3,3]
expected3
Checking account…