-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargest zigzag sequence .txt
56 lines (54 loc) · 1.29 KB
/
Largest zigzag sequence .txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
int T=Integer.parseInt(br.readLine());
while(T-->0)
{
int n=Integer.parseInt(br.readLine());
String[] s=br.readLine().trim().split("\\s");
int count=0;
int[][] arr=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=Integer.parseInt(s[count++]);
}
}
int[][] dp=new int[n][n+1];
for(int[] x:dp)
{
Arrays.fill(x,-1);
}
int ans=zigzagsum(arr,0,arr.length,dp);
sb.append(ans+"\n");
}
System.out.print(sb);
}
public static int zigzagsum(int[][] arr,int row,int prev,int[][] dp)
{
if(row==arr.length)
{
return 0;
}
if(dp[row][prev]!=-1)
{
return dp[row][prev];
}
int ret=0;
for(int i=0;i<arr.length;i++)
{
if(prev!=i)
{
ret=Math.max(ret,arr[row][i]+zigzagsum(arr,row+1,i,dp));
}
}
return dp[row][prev]=ret;
}
}