Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: 13 Dec, 2016

Minimum Sum in matrix

Easy
Asked in companies
AmazonTata Consultancy Services (TCS)Kellton Tech Solutions Limited

Problem statement

Given an integer 2D matrix (of size n x 3), you need to find the smallest sum possible while taking one element from each row. The rules for selecting elements are as follows -

1. In a row, after selecting an element at a given position, you cannot select the element directly below it
2. You can only select elements which are not directly below it.

Keeping the following rules in mind, you have to return the smallest sum that is possible while covering all the rows.

No. of columns are fixed i.e. 3.
Input format :
Line 1 : n (total number of rows)
Next n lines : 3 elements of each row (separated by space).
Output format :
Line 1 : smallest possible sum
Constraints :

1 <= n <= 10^4