BackeasyDynamic ProgrammingInfosysZomato

Shortest Path Cost Protocol 7 Solution

Problem Statement

Given a complex dataset of length N representing system constraints and values, calculate the shortest path cost using the Knapsack State Optimization methodology.

Example 1
Input
[1, 2, 4, 5, 3], 15
Output
15

Explanation: Step-by-step: Given the input array [1, 2, 4, 5, 3] and the maximum weight 15, we can achieve the maximum value by selecting the elements 1, 2, 4, 5, and 3. The total value is 1 + 2 + 4 + 5 + 3 = 15.

Example 2
Input
[10, 20, 30, 40, 50], 150
Output
150

Explanation: Step-by-step: Given the input array [10, 20, 30, 40, 50] and the maximum weight 150, we can achieve the maximum value by selecting all the elements in the array. The total value is 10 + 20 + 30 + 40 + 50 = 150.

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N) or O(N log N)
  • Space Complexity: O(N) or O(1)
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Shortest Path Cost Protocol 7 — Problem Statement & Solution Guide

Dynamic ProgrammingEasyKnapsack State Optimization
TimeO(N·C)
|
SpaceO(C)

Problem Description

Given a complex dataset of length N representing system constraints and values, calculate the shortest path cost using the Knapsack State Optimization methodology.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Shortest Path Cost Protocol 7"

easy

WHY DOES IT MATTER?

Knapsack State Optimization transforms an exponential search into a polynomial one, enabling solutions for datasets with millions of elements. It is a cornerstone pattern for problems that involve selecting a subset under a global constraint, such as resource allocation, budget planning, and routing.

OPTIMIZATION CHALLENGE

The key insight is to collapse a 2‑D DP table into a 1‑D array and update it in reverse order. This reduces memory from O(N·C) to O(C) and eliminates the need for auxiliary arrays, which is critical when C can be up to 10^5 or more.

REAL-WORLD CONNECTION

In distributed systems, a similar pattern appears when scheduling tasks on a cluster with limited CPU or memory. Each task consumes resources (weight) and contributes to latency (cost); the scheduler must pick a feasible set that minimizes overall latency, exactly mirroring the knapsack DP.

When explaining this to an interviewer, emphasize the reverse iteration trick as the ‘magic bullet’ that preserves the 0/1 property. Show a quick code snippet and point out that the same array can be reused across test cases, saving both time and space.

COMPLEXITY AT A GLANCE

⏱ Time:O(N·C)
💾 Space:O(C)

Core Theory — Why This Approach?

Shortest path cost problems often reduce to selecting a subset of items (or edges) that satisfy constraints while minimizing total cost. When the constraints can be expressed as a capacity or budget, the problem is isomorphic to the classic 0/1 Knapsack: each item has a weight (e.g., resource usage) and a value (e.g., negative cost to be minimized). By treating each position in the dataset as an item, we can formulate a DP state dp[w] that stores the minimum cost achievable with total weight w.

Naïve solutions that enumerate all subsets or perform a full breadth‑first search over the state space explode exponentially (O(2^N) or O(N^2) with large constants), making them infeasible for N > 10^5. Even a simple O(N^2) dynamic programming that keeps a 2‑D table of all prefixes and capacities quickly runs out of memory and time.

The optimal paradigm leverages Knapsack State Optimization: a 1‑D DP array updated in reverse order to avoid reusing the same item multiple times. This reduces the time complexity to O(N·C) where C is the maximum capacity (often bounded by the sum of weights or a problem‑specific limit) and the space complexity to O(C). The reverse iteration guarantees that each item contributes only once per capacity, preserving the 0/1 property while keeping memory usage linear.

Interview Questions on This Problem

Q1How would you adapt the classic 0/1 Knapsack DP to solve a shortest path cost problem where each node has a cost and a weight constraint?

Treat each node as an item with weight equal to its resource usage and value equal to its cost. Build a 1‑D DP array dp[w] initialized to infinity, set dp[0]=0, and iterate over nodes updating dp[w] = min(dp[w], dp[w-weight]+cost) in reverse order to enforce the 0/1 constraint.

Q2What is the time and space complexity of the Knapsack State Optimization approach, and how does it compare to a naïve BFS over all paths?

Time complexity is O(N·C) where N is the number of items and C is the capacity; space complexity is O(C). A naïve BFS over all paths would be exponential in N, typically O(2^N) time and O(N) space for the recursion stack, making it impractical for large inputs.

Q3During an interview, a candidate mistakenly updates the DP array in forward order. What bug does this introduce and how can you correct it?

Updating in forward order allows an item to be used multiple times within the same iteration, effectively turning the 0/1 knapsack into an unbounded knapsack. The fix is to iterate the capacity loop in reverse (from C down to weight) so that each item is considered only once per capacity.

Examples

Example 1

Input

[1, 2, 4, 5, 3], 15

Output

15

Explanation: Step-by-step: Given the input array [1, 2, 4, 5, 3] and the maximum weight 15, we can achieve the maximum value by selecting the elements 1, 2, 4, 5, and 3. The total value is 1 + 2 + 4 + 5 + 3 = 15.

Example 2

Input

[10, 20, 30, 40, 50], 150

Output

150

Explanation: Step-by-step: Given the input array [10, 20, 30, 40, 50] and the maximum weight 150, we can achieve the maximum value by selecting all the elements in the array. The total value is 10 + 20 + 30 + 40 + 50 = 150.

Constraints

  • 1 <= N <= 2 * 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity: O(N) or O(N log N)
  • Space Complexity: O(N) or O(1)

Optimal Approach & Strategy

Use a 1‑D DP array updated in reverse order to implement Knapsack State Optimization, achieving O(N·C) time and O(C) space.

Brute Force Approach

Enumerate all subsets of the dataset and compute the total cost for each subset that satisfies the weight constraint, then return the minimum cost. This approach has exponential time complexity and is infeasible for large N.

Verified Code Solutions

JavaScript Solution
Time: O(N·C)
function solution(nums, maxWeight) {
   if (nums.length === 0 || maxWeight <= 0) {
      return 0;
   }
   let dp = new Array(nums.length + 1).fill(0).map(() => new Array(maxWeight + 1).fill(0));
   for (let i = 1; i <= nums.length; i++) {
      for (let j = 1; j <= maxWeight; j++) {
         if (nums[i - 1] > j) {
            dp[i][j] = dp[i - 1][j];
         } else {
            dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - nums[i - 1]] + nums[i - 1]);
         }
      }
   }
   return dp[nums.length][maxWeight];
}

Asked in Top Tech Interviews

InfosysZomato

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.