Skip to content
Onsite

Money and Shopping

MediumP3Coding

Alice visits N shops in a row. costs[i] = [book, shirt, shoes] are the prices of the three items in shop i. She buys exactly one item in every shop, and she will not buy the same kind of item in two adjacent shops.

Return the minimum total amount she can spend.

Example 1

Input
costs = [[1,5,3],[2,9,4]]
Output
5
Why
Book (1) in the first shop and shoes (4) in the second: total 5.

Example 2

Input
costs = [[7,3,8],[4,5,2],[9,1,6]]
Output
6

Example 3

Input
costs = [[5,5,5]]
Output
5

Constraints

  • 1 <= N <= 10^5
  • costs[i] has 3 integers, 1 <= costs[i][j] <= 1000

Hints

Run checks the 3 visible tests, Submit all 9.