Data Structures and Algorithms: Cherry Pickup I and II

beginner
20 min

Data Structures and Algorithms: Cherry Pickup I and II

Welcome to our deep dive into the fascinating world of Data Structures and Algorithms! Today, we'll explore two exciting problems known as Cherry Pickup I and II. These problems are excellent for understanding dynamic programming and how to solve real-world problems using algorithms. Let's get started!

Introduction

šŸ“ Note: In these problems, we'll navigate through a grid where each cell contains either a cherry or an empty space. Our goal is to maximize the number of cherries collected.

šŸŽÆ Objective: Learn to solve Cherry Pickup I and II using dynamic programming.

Cherry Pickup I

Problem Statement

Given a grid of size m x n, where each cell can contain either a cherry (1) or an empty space (0). Two robots, located at the top and right edges of the grid, need to move to the bottom left corner and collect the maximum number of cherries.

Solution

Let's break down this problem into smaller steps:

  1. Define the dp array: We'll use a 2D dp array dp[m][n] to store the maximum number of cherries that can be collected by the two robots when they are located at the given row and column.

  2. Base case: If either robot is at the bottom (row 0) or left (column 0), the number of cherries they can collect is equal to the number of cherries in their current cell.

  3. Recursive formula: To move to the next step, the robots can either move one step down or one step right (or both). The maximum number of cherries they can collect is the maximum of the following three possibilities:

    • Moving down and collecting the cherry in the current cell plus the maximum number of cherries that can be collected when the left robot is in the current column and the right robot is in the next row.
    • Moving right and collecting the cherry in the current cell plus the maximum number of cherries that can be collected when the left robot is in the next column and the right robot is in the current row.
    • Not moving and collecting the maximum number of cherries that can be collected when the left robot is in the current column and the right robot is in the next row (or the other way around).

Here's a Python example:

python
def cherryPickupI(grid): m, n = len(grid), len(grid[0]) dp = [[0] * n for _ in range(m)] for i in range(m): for j in range(n): if i == 0 and j == 0: dp[i][j] = grid[i][j] elif i == 0: dp[i][j] = dp[i][j - 1] + grid[i][j] elif j == 0: dp[i][j] = dp[i - 1][j] + grid[i][j] else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + grid[i][j] return dp[m - 1][n - 1]

Cherry Pickup II

Problem Statement

Now, the two robots can move in any direction (up, down, left, or right), but they must return to their starting positions before moving to the bottom left corner. The goal is to maximize the number of cherries they can collect.

Solution

To solve this problem, we'll modify the dp array and recursive formula from Cherry Pickup I. Here's how:

  1. Define the dp array: We'll use a 3D dp array dp[m][n][2] to store the maximum number of cherries that can be collected when the two robots are located at the given row, column, and direction (0 for down, 1 for right).

  2. Base case: If either robot is at the starting position, the number of cherries they can collect is 0.

  3. Recursive formula: To move to the next step, the robots can move in any of the four directions. The maximum number of cherries they can collect is the maximum of the following four possibilities:

    • Moving down and collecting the cherry in the current cell plus the maximum number of cherries that can be collected when the left robot is in the current column and the right robot is in the next row, moving down (direction 0).
    • Moving right and collecting the cherry in the current cell plus the maximum number of cherries that can be collected when the left robot is in the next column and the right robot is in the current row, moving right (direction 1).
    • Moving up and collecting the cherry in the current cell plus the maximum number of cherries that can be collected when the left robot is in the current column and the right robot is in the previous row, moving up (direction 2).
    • Moving left and collecting the cherry in the current cell plus the maximum number of cherries that can be collected when the left robot is in the previous column and the right robot is in the current row, moving left (direction 3).

Here's a Python example:

python
def cherryPickupII(grid): m, n = len(grid), len(grid[0]) dp = [[[0] * 4 for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): if i == 0 and j == 0: dp[i][j] = [0] * 4 elif i == 0: dp[i][j] = dp[i][j - 1][3] elif j == 0: dp[i][j] = dp[i - 1][j][0] else: dp[i][j] = [dp[i - 1][j][0], dp[i][j - 1][3], dp[i - 1][j - 1][2], dp[i - 1][j - 1][1]] for k in range(4): dp[i][j][k] = max(dp[i][j][k], grid[i][j] + dp[i + 1][j][(k + 1) % 4], grid[i][j] + dp[i][j + 1][(k + 3) % 4]) return dp[m - 1][n - 1][0]

šŸ“ Note: In the dp array, direction 0 represents moving down, direction 1 represents moving right, direction 2 represents moving up, and direction 3 represents moving left.

Quiz

Quick Quiz
Question 1 of 1

How many directions can the two robots move in Cherry Pickup II?

We hope you enjoyed this deep dive into Cherry Pickup I and II! With the knowledge you've gained today, you're well on your way to mastering data structures and algorithms. Keep practicing, and don't forget to come back for more exciting lessons here at CodeYourCraft! šŸ’šŸ’šŸ’