3363. Find the Maximum Number of Fruits Collected Hard

@problem@discussion
#Array#Dynamic Programming#Matrix



1/**
2 * [3363] Find the Maximum Number of Fruits Collected
3 *
4 * There is a game dungeon comprised of n x n rooms arranged in a grid.
5 * You are given a 2D array fruits of size n x n, where fruits[i][j] represents the number of fruits in the room (i, j). Three children will play in the game dungeon, with initial positions at the corner rooms (0, 0), (0, n - 1), and (n - 1, 0).
6 * The children will make exactly n - 1 moves according to the following rules to reach the room (n - 1, n - 1):
7 * 
8 * 	The child starting from (0, 0) must move from their current room (i, j) to one of the rooms (i + 1, j + 1), (i + 1, j), and (i, j + 1) if the target room exists.
9 * 	The child starting from (0, n - 1) must move from their current room (i, j) to one of the rooms (i + 1, j - 1), (i + 1, j), and (i + 1, j + 1) if the target room exists.
10 * 	The child starting from (n - 1, 0) must move from their current room (i, j) to one of the rooms (i - 1, j + 1), (i, j + 1), and (i + 1, j + 1) if the target room exists.
11 * 
12 * When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.
13 * Return the maximum number of fruits the children can collect from the dungeon.
14 *  
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input: <span class="example-io">fruits = [[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]</span>
18 * Output: <span class="example-io">100</span>
19 * Explanation:
20 * <img alt="" src="https://assets.leetcode.com/uploads/2024/10/15/example_1.gif" style="width: 250px; height: 214px;" />
21 * In this example:
22 * 
23 * 	The 1^st child (green) moves on the path (0,0) -> (1,1) -> (2,2) -> (3, 3).
24 * 	The 2^nd child (red) moves on the path (0,3) -> (1,2) -> (2,3) -> (3, 3).
25 * 	The 3^rd child (blue) moves on the path (3,0) -> (3,1) -> (3,2) -> (3, 3).
26 * 
27 * In total they collect 1 + 6 + 11 + 16 + 4 + 8 + 12 + 13 + 14 + 15 = 100 fruits.
28 * </div>
29 * <strong class="example">Example 2:
30 * <div class="example-block">
31 * Input: <span class="example-io">fruits = [[1,1],[1,1]]</span>
32 * Output: <span class="example-io">4</span>
33 * Explanation:
34 * In this example:
35 * 
36 * 	The 1^st child moves on the path (0,0) -> (1,1).
37 * 	The 2^nd child moves on the path (0,1) -> (1,1).
38 * 	The 3^rd child moves on the path (1,0) -> (1,1).
39 * 
40 * In total they collect 1 + 1 + 1 + 1 = 4 fruits.
41 * </div>
42 *  
43 * Constraints:
44 * 
45 * 	2 <= n == fruits.length == fruits[i].length <= 1000
46 * 	0 <= fruits[i][j] <= 1000
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/find-the-maximum-number-of-fruits-collected/
52// discuss: https://leetcode.com/problems/find-the-maximum-number-of-fruits-collected/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57    pub fn max_collected_fruits(fruits: Vec<Vec<i32>>) -> i32 {
58        0
59    }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_3363() {
70    }
71}
72


Back
© 2025 bowen.ge All Rights Reserved.