My Solution
\n/**\n * Time Complexity: O(n * n)\n * Space Complexity: O(n * n)\n * where `n` is the length of the vector `fruits`\n */\nclass Solution {\n public:\n  int ma
```\n/**\n * Time Complexity: O(n * n)\n * Space Complexity: O(n * n)\n * where `n` is the length of the vector `fruits`\n */\nclass Solution {\n public:\n  int maxCollectedFruits(const vector<vector<int>> &fruits) {\n    constexpr int lower_offset = -1;\n    constexpr int upper_offset = 2;\n    const int n = static_cast<int>(fruits.size());\n    \n    int ret = 0;\n    for (int i = 0; i < n; ++i) {\n      ret += fruits[i][i];\n    }\n    \n    vector<vector<int>> dp(n, vector<int>(n, numeric_limits<int>::min()));\n    dp[n - 1][0] = fruits[n - 1][0];\n    for (int c = 1; c < n - 1; ++c) {\n      for (int r = max(n - c - 1, c + 1); r < n; ++r) {\n        for (int offset_r = lower_offset; offset_r < upper_offset; ++offset_r) {\n          const int pr = r + offset_r;\n          if (pr > max(n - c, c) - 1 && pr < n) {\n            dp[r][c] = max(dp[r][c], plus(dp[pr][c - 1], fruits[r][c]));\n          }\n        }\n      }\n    }\n    ret += dp[n - 1][n - 2];\n    \n    dp = vector<vector<int>>(n, vector<int>(n, numeric_limits<int>::min()));\n    dp[0][n - 1] = fruits[0][n - 1];\n    for (int r = 1; r < n - 1; ++r) {\n      for (int c = max(n - r - 1, r + 1); c < n; ++c) {\n        for (int offset_c = lower_offset; offset_c < upper_offset; ++offset_c) {\n          const int pc = c + offset_c;\n          if (pc > max(n - r, r) - 1 && pc < n) {\n            dp[r][c] = max(dp[r][c], plus(dp[r - 1][pc], fruits[r][c]));\n          }\n        }\n      }\n    }\n    ret += dp[n - 2][n - 1];\n    return ret;\n  }\n  \n private:\n  int plus(const int lhs, const int rhs) {\n    if (lhs == numeric_limits<int>::min() || rhs == numeric_limits<int>::min()) {\n      return numeric_limits<int>::min();\n    }\n    return lhs + rhs;\n  }\n};\n```
[]