423. Correctly Arranged Conveyor Segments
Asked in
Correctly Arranged Conveyor Segments

A distribution center places packages in a line on a conveyor belt. Each package has an inspection code.

A package with an even code is domestic, while a package with an odd code is international.

A conveyor segment is correctly arranged if domestic and international packages alternate throughout the segment.

For every requested segment, determine whether it is correctly arranged.

Class

ConveyorInspector

Method

validateSegments

List<Boolean> validateSegments( List<Integer> packageCodes, List<String> inspectionRanges )

Parameters

  • packageCodes: The inspection codes of the packages in conveyor order.
  • inspectionRanges: Each string has the format "start,end" and describes an inclusive segment.

Returns

Return a list containing one boolean value for each inspection range, in the same order as the input.

A value is true if every pair of neighboring packages in that segment contains one even code and one odd code. Otherwise, it is false.

Arrangement Rules

  • Package positions are indexed from 0.
  • An even inspection code represents a domestic package.
  • An odd inspection code represents an international package.
  • Both endpoints of an inspection range are included.
  • A segment containing only one package is correctly arranged.

Constraints

  • 1 ≤ packageCodes.size() ≤ 100,000
  • 1 ≤ packageCodes[i] ≤ 100,000
  • 1 ≤ inspectionRanges.size() ≤ 100,000
  • Every element of inspectionRanges contains exactly two integers separated by one comma, with no spaces.
  • For every range "start,end", 0 ≤ start ≤ end < packageCodes.size() .

Example 1

validateSegments( packageCodes = [18, 7, 12, 5, 9], inspectionRanges = ["0,3", "1,4", "2,2"] )

Output: [true, false, true]

Explanation

  • Segment [18, 7, 12, 5] alternates between even and odd codes.
  • Segment [7, 12, 5, 9] ends with two neighboring odd codes.
  • The segment containing only 12 is correctly arranged.

Example 2

validateSegments( packageCodes = [3, 8, 14, 11, 6, 2], inspectionRanges = ["0,1", "1,3", "2,4", "4,5"] )

Output: [true, false, true, false]

Explanation

  • Codes 3 and 8 have different parity.
  • Codes 8 and 14 are both even.
  • Segment [14, 11, 6] alternates correctly.
  • Codes 6 and 2 are both even.


Please use Laptop/Desktop or any other large screen to add/edit code.