From a90e6a2f95411ccb3c7d9690abca977b8ad4dffe Mon Sep 17 00:00:00 2001 From: chayan das Date: Thu, 7 Aug 2025 17:32:33 +0530 Subject: [PATCH] Create 3363. Find the Maximum Number of Fruits Collected --- ...ind the Maximum Number of Fruits Collected | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 3363. Find the Maximum Number of Fruits Collected diff --git a/3363. Find the Maximum Number of Fruits Collected b/3363. Find the Maximum Number of Fruits Collected new file mode 100644 index 0000000..2cf5962 --- /dev/null +++ b/3363. Find the Maximum Number of Fruits Collected @@ -0,0 +1,30 @@ +class Solution { +public: + int maxCollectedFruits(vector>& fruits) { + int f_cnt = 0, i = 0, j = 0, n = fruits.size(); + while(i < n){ + f_cnt += fruits[i][j]; + fruits[i][j] = 0; + i++,j++; + } + auto temp = fruits; + for(j = 0; j < n-1; j++){ + for(i = n-1; i >= n-1-j && i > j; i--){ + if(i-1 >= 0) temp[i-1][j+1] = max(temp[i-1][j+1], fruits[i-1][j+1]+temp[i][j]); + temp[i][j+1] = max(temp[i][j+1], fruits[i][j+1]+temp[i][j]); + if(i+1 < n) temp[i+1][j+1] = max(temp[i+1][j+1], fruits[i+1][j+1]+temp[i][j]); + } + } + f_cnt += temp[n-1][n-1]; + temp = fruits; + for(i = 0; i < n-1; i++){ + for(j = n-1; j >= n-1-i && j > i; j--){ + if(j-1 >= 0)temp[i+1][j-1] = max(temp[i+1][j-1], fruits[i+1][j-1]+temp[i][j]); + temp[i+1][j] = max(temp[i+1][j], fruits[i+1][j]+temp[i][j]); + if(j+1 < n) temp[i+1][j+1] = max(temp[i+1][j+1], fruits[i+1][j+1]+temp[i][j]); + } + } + f_cnt += temp[n-1][n-1]; + return f_cnt; + } +};