-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxNumberOfFruitsCollected.cpp
More file actions
133 lines (105 loc) · 3.35 KB
/
Copy pathMaxNumberOfFruitsCollected.cpp
File metadata and controls
133 lines (105 loc) · 3.35 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//Memoisation
// Time Complexity: O(n²)
// Space Complexity: O(n²)
class Solution {
public:
int n;
vector<vector<int>>t;
// child1 (0,0) can only move diagonal as then only
// we can reach final cell in n-1 moves
int child1collect(vector<vector<int>>& fruits){
int cnt=0;
for(int i=0;i<n;i++){
cnt+=fruits[i][i];
}
return cnt;
}
//child2 (0,n-1) movements we can do recursion if i>j and i==j so we can't choose it as then it will not reach in n-1 steps
int child2collect(int i,int j,vector<vector<int>>& fruits){
if(i>=n || j<0 || j>=n){
return 0;
}
if(i==n-1 && j==n-1){
return 0;
}
if(i==j ||i >j){
return 0;
}
if(t[i][j]!=-1){
return t[i][j];
}
int bottomleft=fruits[i][j]+child2collect(i+1,j-1,fruits);
int bottomdown=fruits[i][j]+child2collect(i+1,j,fruits);
int diagonal=fruits[i][j]+child2collect(i+1,j+1,fruits);
return t[i][j]=max({bottomleft,bottomdown,diagonal});
}
//child3 (n-1,0) we can do recursion
//if i<j and i==j we can't choose them as then it will not reach in n-1 steps
int child3collect(int i,int j,vector<vector<int>>& fruits){
if(i>=n || j<0 || j>=n){
return 0;
}
if(i==n-1 && j==n-1){
return 0;
}
if(i==j ||i <j){
return 0;
}
if(t[i][j]!=-1){
return t[i][j];
}
int topright=fruits[i][j]+child3collect(i-1,j+1,fruits);
int right=fruits[i][j]+child3collect(i,j+1,fruits);
int diagonal=fruits[i][j]+child3collect(i+1,j+1,fruits);
return t[i][j]= max({topright,right,diagonal});
}
int maxCollectedFruits(vector<vector<int>>& fruits) {
n=fruits.size();
t.resize(n,vector<int>(n,-1));
int c1=child1collect(fruits);
int c2=child2collect(0,n-1,fruits);
int c3=child3collect(n-1,0,fruits);
return c1+c2+c3;
}
};
//bottom up dp
// Time Complexity: O(n²)
// Space Complexity: O(n²)
int maxCollectedFruits(vector<vector<int>>& fruits) {
//bottom up
int n=fruits.size();
vector<vector<int>>t(n,vector<int>(n,0));
//child1collect -diagonal sum
int res=0;
for(int i=0;i<n;i++){
res+=fruits[i][i];
}
//before child2 and child3, nullify all the cells which can't be visited
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i<j && i+j<n-1){
t[i][j]=0;
}
else if(i>j && i+j<n-1){
t[i][j]=0;
}
else{
t[i][j]=fruits[i][j];
}
}
}
//child2collect start from 0,n-1 and collect when i>j
for(int i=1;i<n;i++){
for(int j=i+1;j<n;j++){
t[i][j]+=max({t[i-1][j-1] , t[i-1][j] , (j+1<n)?t[i-1][j+1]:0});
}
}
//child3collect start from n-1,0 and collect when i<j
for(int j=1;j<n;j++){
for(int i=j+1;i<n;i++){
t[i][j]+=max({t[i-1][j-1] , t[i][j-1] , (i+1<n)?t[i+1][j-1]:0});
}
}
return res+t[n-2][n-1] +t[n-1][n-2];
}
};