113. Minimum Cost Array

Asked in

Minimum Cost Array
Given a list of costs, find the minimum total cost needed to cross the array or reach the end of the array.

Explanation:
  • the movement rules in this problem use 1-based indexing
  • index 1 means the first element of the input list
  • from any visited index, you may move exactly two indices forward or exactly one index backward
  • if you land on an index, you must add that index cost to your total
  • each index may be visited at most once
  • if costs = List.of(2, 5, 8), then:
    • index 1 has cost 2
    • index 2 has cost 5
    • index 3 has cost 8
You begin already standing on index 1, so the cost at index 1 is included in the total.

You succeed if:
  • you land on index n, where n is the number of elements, or
  • you move to any index greater than n, which means you have crossed the array
Moving to an index less than 1 is not allowed.

Return the minimum possible total cost.

Method Signature

int minimumCost(List<Integer> costs)

Parameters

  • costs is a list of positive integers
  • costs.get(0) represents the cost at index 1
  • costs.get(i) represents the cost at index i + 1

Return Value

  • return the minimum total cost needed to reach index n or cross beyond it

Rules

  • you start at index 1
  • starting on index 1 adds its cost immediately
  • from index i, you may move only to:
    • i + 2
    • i - 1
  • you may visit any valid array index at most one time
  • if you move to an index greater than n, no additional cost is added for that move
  • all costs are positive, so revisiting indices would never help and is explicitly forbidden

Suggested Constraints

  • 1 ≤ costs.size() ≤ 100,000
  • 1 ≤ costs.get(i) ≤ 10,000

Notes

  • because the problem starts at index 1, the input list must contain at least one value
  • comparison is based on the actual minimum total cost
  • you must never use a path that visits the same valid index more than once

Examples

Example 1

Method call:
  • minimumCost(costs = List.of(2, 5, 8))
Output:
  • 10
Explanation:
  • start at index 1, pay 2
  • jump forward by 2 to index 3, pay 8
  • you have reached the end of the array
  • total cost = 2 + 8 = 10

Example 2

Method call:
  • minimumCost(costs = List.of(4))
Output:
  • 4
Explanation:
  • the array has only one index, and you start at index 1
  • you are already at the end, so the answer is just the starting cost

Example 3

Method call:
  • minimumCost(costs = List.of(3, 100, 2, 1))
Output:
  • 5
Explanation:
  • start at index 1, pay 3
  • jump to index 3, pay 2
  • jump to index 5, which crosses the array
  • total cost = 3 + 2 = 5

Example 4

Method call:
  • minimumCost(costs = List.of(5, 1, 7, 1, 1))
Output:
  • 13
Explanation:
  • start at index 1, pay 5
  • jump to index 3, pay 7
  • jump to index 5, pay 1
  • you have reached the end of the array
  • total cost = 5 + 7 + 1 = 13

Example 5

Method call:
  • minimumCost(costs = List.of(1000000, 1, 1000000, 1, 1000000, 1))
Output:
  • 2000003
Explanation:
  • start at index 1, pay 1000000
  • jump to index 3, pay 1000000
  • move backward to index 2, pay 1
  • jump to index 4, pay 1
  • jump to index 6, pay 1
  • you have reached the end of the array
  • total cost = 1000000 + 1000000 + 1 + 1 + 1 = 2000003




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