Maximum Gain on a Budgeted Returning Walk
Updated
Problem statement
An undirected graph has a nonnegative gain at each vertex and a positive cost on each edge. Start at vertex start, traverse zero or more edges, and finish at that same vertex with total edge cost at most budget. Return the greatest total gain possible.
- The gain of
startcounts even if you do not move. - Each other vertex contributes its gain only on the first visit. Later visits contribute no additional gain.
- Vertices and edges may be revisited. Parallel edges are allowed. Each row of
edgesis[u, v, cost]and may be used in either direction.
Function
maxGainWalk(gains: int[], edges: int[][], start: int, budget: int) → intExamples
Example 1
gains = [5, 10, 7]edges = [[0, 1, 2], [1, 2, 2], [0, 2, 6]]start = 0budget = 8return = 22The walk 0 to 1 to 2 to 1 to 0 costs 8 and gains 5 + 10 + 7. Revisiting 1 does not count it twice.
Example 2
gains = [4, 100, 3]edges = [[0, 1, 3], [0, 2, 1]]start = 0budget = 5return = 7Node 1 needs cost 6 for a round trip; 0 to 2 to 0 costs 2 and gains 4 + 3.
Example 3
gains = [1, 7, 9, 100]edges = [[0, 1, 1], [0, 2, 1], [2, 3, 8]]start = 0budget = 4return = 17Visit 1 and 2 on separate round trips through 0 for cost 4. Node 3 is too far away.
Example 4
gains = [3, 50]edges = [[0, 1, 1]]start = 0budget = 0return = 3Staying at the starting node costs nothing and includes its gain.
Constraints
1 <= gains.length <= 12;0 <= gains[i] <= 1000.0 <= edges.length <= 100; each endpoint is a distinct valid vertex index and1 <= cost <= 100.0 <= start < gains.length;0 <= budget <= 1000.
More NVIDIA Onsite Interview questions
- Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
- Implement MallocONSITE INTERVIEW · Seen Sep 2026
- Implement PrintfONSITE INTERVIEW · Seen Sep 2026
- Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
- Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
- Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
- Reverse A StringONSITE INTERVIEW · Seen Sep 2026
- Maximum Fish in a GridONSITE INTERVIEW · Seen Sep 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- First Register Overflow TickONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Minimum Path Cost with Edge DiscountsSkyscanner · ONSITE INTERVIEWSkyscanner interview questions
- Cheapest Flight with Its PathAirbnb · OA · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Cheapest Flights Within K StopsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- All-Pairs Shortest PathsMorgan Stanley · OA · ONSITE INTERVIEWMorgan Stanley interview questions
- Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
- Cities on Shortest Delivery RoutesMathWorks · OAMathWorks interview questions
- Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
- Cheapest Flights Within K StopsInMobi · OAInMobi interview questions
- Parallel Courses IIISnowflake · ONSITE INTERVIEWSnowflake interview questions
- Sum of Weighted Distances in a TreeGoogle · ONSITE INTERVIEWGoogle interview questions