Program to find cost to reach final index of any of given two lists in Python

Suppose we have two lists of numbers nums0 and nums1 of the same length and two other values d as distance and c as cost. If we start from index 0 at either nums0 or nums1 and want to end up at the final index of either list. Now, in each round, we can select to switch to the other list for cost of cost. Then we can jump forward at most d distance away where the cost of landing at an index is the value at that point. So we have to find the minimum total cost possible to complete the task.

So, if the input is like nums0 = [2, 3, 10, 10, 6] nums1 = [10, 10, 4, 5, 100] d = 2 c = 3, then the output will be 18, as we can start from 2, then switch to the second list to 4, again switch back to first list to 6. So cost 2 + 4 + 6 = 12 and switch twice with cost 3 each so total is 18.

Algorithm

To solve this, we will follow these steps ?

  • switch := a map with key 0 for nums0, and key 1 for nums1
  • Define a function search(). This will take idx, nums
  • if idx >= size of switch[nums], then
    • return inf
  • if idx is same as size of switch[nums] − 1, then
    • return switch[nums][-1]
  • c := inf
  • for i in range 1 to dist + 1, do
    • c := minimum of c and switch[nums][idx] + search(idx + i, nums)
    • c := minimum of c and switch[nums][idx] + cost + search(idx + i, invert of nums)
  • return c
  • from the main method do the following −
  • return minimum of search(0, 0) and search(0, 1)

Example

Let us see the following implementation to get better understanding ?

class Solution:
    def solve(self, nums0, nums1, dist, cost):
        switch = {0: nums0, 1: nums1}
        
        def search(idx, nums):
            if idx >= len(switch[nums]):
                return float("inf")
            if idx == len(switch[nums]) - 1:
                return switch[nums][-1]
            
            c = float("inf")
            for i in range(1, dist + 1):
                # Stay in same list
                c = min(c, switch[nums][idx] + search(idx + i, nums))
                # Switch to other list
                c = min(c, switch[nums][idx] + cost + search(idx + i, int(not nums)))
            return c
        
        return min(search(0, 0), search(0, 1))

# Test the solution
ob = Solution()
nums0 = [2, 3, 10, 10, 6]
nums1 = [10, 10, 4, 5, 100]
d = 2
c = 3
print(ob.solve(nums0, nums1, d, c))
18

How It Works

The algorithm uses recursive search with the following key points:

  • Base case: If we reach the last index, return its value as the final cost
  • Jump options: From each position, we can jump 1 to d steps forward
  • Switch option: At each step, we can stay in the same list or switch to the other list (paying the switch cost)
  • Optimization: We choose the minimum cost path among all possible moves

Alternative Implementation with Memoization

For better performance, we can add memoization to avoid recalculating the same subproblems ?

class Solution:
    def solve(self, nums0, nums1, dist, cost):
        switch = {0: nums0, 1: nums1}
        memo = {}
        
        def search(idx, nums):
            if (idx, nums) in memo:
                return memo[(idx, nums)]
                
            if idx >= len(switch[nums]):
                return float("inf")
            if idx == len(switch[nums]) - 1:
                return switch[nums][-1]
            
            c = float("inf")
            for i in range(1, dist + 1):
                # Stay in same list
                c = min(c, switch[nums][idx] + search(idx + i, nums))
                # Switch to other list
                c = min(c, switch[nums][idx] + cost + search(idx + i, int(not nums)))
            
            memo[(idx, nums)] = c
            return c
        
        return min(search(0, 0), search(0, 1))

# Test with memoization
ob = Solution()
nums0 = [2, 3, 10, 10, 6]
nums1 = [10, 10, 4, 5, 100]
d = 2
c = 3
print(ob.solve(nums0, nums1, d, c))
18

Conclusion

This dynamic programming approach explores all possible paths while choosing minimum cost moves. The memoized version improves efficiency by caching results of subproblems, making it suitable for larger inputs.

Updated on: 2026-03-25T13:31:48+05:30

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements