|
| 1 | +/* |
| 2 | +Geek's Training |
| 3 | +MediumAccuracy: 49.98 |
| 4 | +
|
| 5 | +Geek is going for n days training program, he can perform any one of these three activities Running, Fighting, and Learning Practice. |
| 6 | +Each activity has some point on each day. as Geek wants to improve all his skills, he can't do the same activity on two consecutive days, |
| 7 | +help Geek to maximize his merit points as we were given a 2D array of n*3 points corresponding to each day and activity. |
| 8 | +
|
| 9 | +Example: |
| 10 | +Input: |
| 11 | +n = 3 |
| 12 | +point= [[1,2,5],[3,1,1],[3,3,3]] |
| 13 | +Output: |
| 14 | +11 |
| 15 | +Explanation: |
| 16 | +Geek will learn a new move and earn 5 point then on second |
| 17 | +day he will do running and earn 3 point and on third day |
| 18 | +he will do fighting and earn 3 points so, maximum point is 11. |
| 19 | +Your Task: |
| 20 | +You don't have to read input or print anything. Your task is to complete the function maximumPoints() which takes the integer n and 2 D array points and returns the maximum point he can earn. |
| 21 | +
|
| 22 | +Expected Time Complexity: O(n) |
| 23 | +Expected Space Complexity: O(n2) |
| 24 | +
|
| 25 | +Constraint: |
| 26 | +1 <= n <= 100000 |
| 27 | +1 <= point[i] <= 100 |
| 28 | +*/ |
| 29 | +class Solution{ |
| 30 | + static int[][] memo; |
| 31 | + public int maximumPoints(int points[][],int N){ |
| 32 | + // Recursion - TLE |
| 33 | + |
| 34 | + // Memoization - |
| 35 | + // TC : O(n*4*3) |
| 36 | + //SC : O(n+4) |
| 37 | + memo = new int[N+1][4]; |
| 38 | + Arrays.stream(memo).forEach(a->Arrays.fill(a,-1)); |
| 39 | + //return solveRec(N,-1,points); |
| 40 | + |
| 41 | + //// TC : O(n*4*3) |
| 42 | + //SC : O(n+4) |
| 43 | + //return solveTab(N,points); |
| 44 | + |
| 45 | + //TC : O(n*4*3) |
| 46 | + // SC : O(4) |
| 47 | + return solveOpt(N,points); |
| 48 | + } |
| 49 | + public int solveRec(int n,int opt,int[][] points){ |
| 50 | + //Base Case: |
| 51 | + |
| 52 | + if(n<=0) return 0; |
| 53 | + if(memo[n-1][opt+1]!=-1){ |
| 54 | + return memo[n-1][opt+1]; |
| 55 | + } |
| 56 | + int maxscore=0; |
| 57 | + for(int i=0;i<3;i++){ |
| 58 | + if(i!=opt){ |
| 59 | + int currsum = points[n-1][i] + solveRec(n-1,i,points); |
| 60 | + maxscore = Math.max(currsum,maxscore); |
| 61 | + } |
| 62 | + } |
| 63 | + return memo[n-1][opt+1]=maxscore; |
| 64 | + } |
| 65 | + public int solveTab(int n,int[][] points){ |
| 66 | + int[][] tab = new int[n][4]; |
| 67 | + tab[0][0] = Math.max(points[0][1], points[0][2]); |
| 68 | + tab[0][1] = Math.max(points[0][0], points[0][2]); |
| 69 | + tab[0][2] = Math.max(points[0][0], points[0][1]); |
| 70 | + tab[0][3] = Math.max(points[0][0], Math.max(points[0][1], points[0][2])); |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + for(int i=1;i<n;i++){ |
| 75 | + for(int j=0;j<4;j++){ |
| 76 | + tab[i][j]=0; |
| 77 | + for(int k=0;k<3;k++){ |
| 78 | + if(k!=j){ |
| 79 | + int currsum = points[i][k] + tab[i-1][k]; |
| 80 | + tab[i][j] = Math.max(currsum,tab[i][j]); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + return tab[n-1][3]; |
| 87 | + } |
| 88 | + // Space Optimised : |
| 89 | + public int solveOpt(int n,int[][] points){ |
| 90 | + int[] prev = new int[4]; |
| 91 | + prev[0] = Math.max(points[0][1], points[0][2]); |
| 92 | + prev[1] = Math.max(points[0][0], points[0][2]); |
| 93 | + prev[2] = Math.max(points[0][0], points[0][1]); |
| 94 | + prev[3] = Math.max(points[0][0], Math.max(points[0][1], points[0][2])); |
| 95 | + |
| 96 | + for(int i=1;i<n;i++){ |
| 97 | + int[] curr = new int[4]; |
| 98 | + for(int j=0;j<4;j++){ |
| 99 | + curr[j]=0; |
| 100 | + for(int k=0;k<3;k++){ |
| 101 | + if(k!=j){ |
| 102 | + curr[j] = Math.max(points[i][k] + prev[k],curr[j]); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + prev = curr; |
| 107 | + |
| 108 | + } |
| 109 | + return prev[3]; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments