diff --git a/_includes/code/cherry-pickup-ii/solution.java b/_includes/code/cherry-pickup-ii/solution.java new file mode 100644 index 0000000..396acca --- /dev/null +++ b/_includes/code/cherry-pickup-ii/solution.java @@ -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; + } +} \ No newline at end of file diff --git a/_includes/linkedlist.html b/_includes/linkedlist.html deleted file mode 100644 index e69de29..0000000 diff --git a/posts/_hard/cherry-pickup-ii.md b/posts/_hard/cherry-pickup-ii.md new file mode 100644 index 0000000..28a16b8 --- /dev/null +++ b/posts/_hard/cherry-pickup-ii.md @@ -0,0 +1,65 @@ +--- +layout: post +author: Rajat Srivastava +title: Cherry Pickup II +topics: array dynamic-programming matrix +langs: java +tc: O(mn2) +sc: O(mn2) +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` + + \ No newline at end of file diff --git a/scripts/leetcode.py b/scripts/leetcode.py index b0c741d..635928c 100644 --- a/scripts/leetcode.py +++ b/scripts/leetcode.py @@ -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 @@ -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__':