House Robber with Selected Indices
Updated
Problem statement
Each element of points is the number of points available at one index. Choose any set of indices such that no two chosen indices are adjacent and the sum of their values is as large as possible.
Return a ragged long[][] with two rows. Row 0 contains only the maximum sum. Row 1 contains the chosen zero-based indices in increasing order.
Use this deterministic tie rule: while reconstructing from right to left using optimal prefix sums, choose index i only when taking it gives a strictly larger sum than skipping it; on equality, skip i. Choosing no index is allowed.
Function
robWithIndices(points: int[]) → long[][]Examples
Example 1
points = [2,7,9,3,1]return = [[12],[0,2,4]]Indices 0, 2, and 4 are non-adjacent and contribute 2 + 9 + 1 = 12.
Example 2
points = [2,2]return = [[2],[0]]Either index gives sum 2. At index 1 taking and skipping tie, so the rule skips it and keeps index 0.
Example 3
points = [0,0,0]return = [[0],[]]Every take decision ties with skipping, so the deterministic strategy returns the empty selection with maximum sum zero.
Constraints
0 <= points.length <= 2000000 <= points[i] <= 1000000000- The maximum sum must be computed with signed 64-bit arithmetic.
More Google Phone Screen questions
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026
- Minimum Total Layover WaitPHONE SCREEN · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Fewest Coins CombinationBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions
- House RobberVisa · PHONE SCREENVisa interview questions
- Best Improvement Period of at Least K IntervalsAmazon · PHONE SCREEN
- Longest Increasing SubsequenceAngel One · PHONE SCREEN
- Maximum Product of a Contiguous SubarrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Minimum Cost for a Target WeightPace Stock Broking Services · PHONE SCREEN
- Minimum-Cost Color SequenceOpenAI · OA · PHONE SCREENOpenAI interview questions