Leaderboard
Showing:
Largest zigzag sequence
Easy Accuracy: 25.71% Submissions: 35 Points: 2

Given a matrix M of size n * n, the task is to find sum of the zigzag sequence with the largest sum. A zigzag sequence starts from the top and ends at the bottom. Two consecutive elements of sequence cannot belong to same column.


Example 1:

Input: n = 3
M = {{3, 1, 2}, 
     {4, 8, 5}, 
     {6, 9, 7}}
Output: 18
Explaination: The sequence is (3, 8, 7).


Example 2:

Input: n = 3
M = {{1, 2, 4}, 
     {3, 9, 6}, 
     {11, 3, 15}}
Output: 28
Explaination: The sequence is 4, 9, 15.


Your Task:
You do not need to read input or print anything. Your task is to complete the function zigzagSequence() which takes n and M as input parameters and returns the highest zigzag sum.


Expected Time Complexity: O(n3)
Expected Auxiliary Space: O(n2)  


Constraints:
1 ≤ n ≤ 100
1 ≤ M[i][j] ≤ 1000 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Largest zigzag sequence

Output Window