LeetCode 3363: Find the Maximum Number of Fruits Collected Solution

Master LeetCode problem 3363 (Find the Maximum Number of Fruits Collected), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3363. Find the Maximum Number of Fruits Collected

Problem Explanation

Explanation:

To solve this problem, we can use dynamic programming to keep track of the maximum number of fruits collected by each child at each cell in the dungeon. We can then calculate the total fruits collected by all children at the destination cell by summing up the fruits collected by each child.

  1. Initialize a 3D DP array of size n x n x 3 where dp[i][j][k] represents the maximum number of fruits that can be collected by the k-th child starting from cell (i, j).
  2. Iterate through the dungeon cells row by row and column by column, updating the DP array based on the rules mentioned in the problem description.
  3. Finally, return the sum of fruits collected by all children at the destination cell.

Time Complexity: O(n^2) where n is the size of the dungeon grid. Space Complexity: O(n^2) for the DP array.

Solution Code

class Solution {
    public int maxFruits(int[][] fruits) {
        int n = fruits.length;
        int[][][] dp = new int[n][n][3];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < 3; k++) {
                    dp[i][j][k] = fruits[i][j];
                    if (i > 0 && j > 0) dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - 1][k] + fruits[i][j]);
                    if (i > 0) dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j][k] + fruits[i][j]);
                    if (j > 0) dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j - 1][k] + fruits[i][j]);
                }
            }
        }

        return dp[n - 1][n - 1][0] + dp[n - 1][n - 1][1] + dp[n - 1][n - 1][2] - fruits[n - 1][n - 1];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3363 (Find the Maximum Number of Fruits Collected)?

This page provides optimized solutions for LeetCode problem 3363 (Find the Maximum Number of Fruits Collected) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3363 (Find the Maximum Number of Fruits Collected)?

The time complexity for LeetCode 3363 (Find the Maximum Number of Fruits Collected) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3363 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3363 in Java, C++, or Python.

Back to LeetCode Solutions