Skip to content

Commit

Permalink
Merge pull request #10 from rajat19/cherry-pickup-ii
Browse files Browse the repository at this point in the history
add solution for cherry-pickup -ii
  • Loading branch information
rajat19 committed Feb 11, 2024
2 parents 120be35 + 92a1aa9 commit d38b7d9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
48 changes: 48 additions & 0 deletions _includes/code/cherry-pickup-ii/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution {
public int cherryPickup(int[][] grid) {
int dir[] = new int[]{-1, 0, 1};

int row = grid.length;
int col = grid[0].length;
int dp[][][] = new int[row][col][col];

for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
for(int k = 0; k < col; k++){
dp[i][j][k] = -1;
}
}
}
int col1 = 0;
int col2 = col - 1;

dp[0][col1][col2] = grid[0][col1] + grid[0][col2];
int max = dp[0][col1][col2];

for(int i = 1; i < row; i++){
for(int c1 = 0; c1 < col; c1++){
for(int c2 = 0; c2 < col; c2++){
int prev = dp[i - 1][c1][c2];
if(prev >= 0){
for(int d1: dir){
col1 = d1 + c1;
for(int d2: dir){
col2 = d2 + c2;
if(inRange(col1, col) && inRange(col2, col)){
dp[i][col1][col2] = Math.max(dp[i][col1][col2], prev+(col1 == col2 ? grid[i][col1] : (grid[i][col1] + grid[i][col2])));
max = Math.max(max, dp[i][col1][col2]);
}
}
}
}

}
}
}
return max;
}

public boolean inRange(int val, int limit){
return 0 <= val && val < limit;
}
}
Empty file removed _includes/linkedlist.html
Empty file.
65 changes: 65 additions & 0 deletions posts/_hard/cherry-pickup-ii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
layout: post
author: Rajat Srivastava
title: Cherry Pickup II
topics: array dynamic-programming matrix
langs: java
tc: O(mn<sup>2</sup>)
sc: O(mn<sup>2</sup>)
leetid: 1559
companies:
---
You are given a `rows x cols` matrix `grid` representing a field of cherries where `grid[i][j]` represents the number of cherries that you can collect from the `(i, j)` cell.
You have two robots that can collect cherries for you:

* **Robot #1** is located at the **top-left corner** `(0, 0)`, and

* **Robot #2** is located at the **top-right corner** `(0, cols - 1)`.
Return *the maximum number of cherries collection using both robots by following the rules below*:

* From a cell `(i, j)`, robots can move to cell `(i + 1, j - 1)`, `(i + 1, j)`, or `(i + 1, j + 1)`.

* When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.

* When both robots stay in the same cell, only one takes the cherries.

* Both robots cannot move outside of the grid at any moment.

* Both robots should reach the bottom row in `grid`.

---
## Test Cases
**Example 1:**

{% include matrix.html rows=4 cols=3 matrix='3:$green,1,1:$blue,2:$green,5:$blue,1,1,5:$green,5:$blue,2:$green,1,1:$blue' %}

```
Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output: 24
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
Total of cherries: 12 + 12 = 24.
```
**Example 2:**

```
Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
Output: 28
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
Total of cherries: 17 + 11 = 28.
```

**Constraints:**

* `rows == grid.length`

* `cols == grid[i].length`

* `2 <= rows, cols <= 70`

* `0 <= grid[i][j] <= 100`

15 changes: 13 additions & 2 deletions scripts/leetcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class Leetcode:
def __init__(self, question, time_complexity, space_complexity, languages):
def __init__(self, question: str, time_complexity: str, space_complexity: str, languages: str):
self.question = question
self.time_complexity = time_complexity
self.space_complexity = space_complexity
Expand Down Expand Up @@ -100,13 +100,24 @@ def create_file(self, content, ext='md'):
os.makedirs(os.path.dirname(file_name), exist_ok=True)
with open(file_name, 'w') as text_file:
text_file.write(content)
print(os.path.abspath(file_name))

def create_solution_files(self):
path = os.path.join('..', '_includes', 'code', self.question)
for lang in self.languages.split(' '):
file_name = os.path.join(path, 'solution.{}'.format(lang))
os.makedirs(os.path.dirname(file_name), exist_ok=True)
with open(file_name, 'w') as text_file:
text_file.write('')
print(os.path.abspath(file_name))

def generate_question(self):
self.retrieve_question_data()
post_data = self.create_post_data()
print('Created following files::')
self.create_file(post_data, 'md')
# self.create_file(self.format_content(self.question_data['content']), 'html')
print('Successfully created post for ', self.question)
self.create_solution_files()


if __name__ == '__main__':
Expand Down

0 comments on commit d38b7d9

Please sign in to comment.