|
1 |
| -# Decode String |
2 |
| -Given an encoded string, return its decoded string. |
| 1 | +# Cherry Pickup II |
| 2 | +Given a rows x cols matrix grid representing a field of cherries. Each cell in grid represents the number of cherries that you can collect. |
3 | 3 |
|
4 |
| -The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. |
| 4 | +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) of the grid. |
5 | 5 |
|
6 |
| -You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc. |
7 |
| - |
8 |
| -Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4]. |
| 6 | +Return the maximum number of cherries collection using both robots by following the rules below: |
9 | 7 |
|
| 8 | +From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1). |
| 9 | +When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0). |
| 10 | +When both robots stay on the same cell, only one of them takes the cherries. |
| 11 | +Both robots cannot move outside of the grid at any moment. |
| 12 | +Both robots should reach the bottom row in the grid. |
10 | 13 |
|
11 | 14 |
|
12 | 15 | Example 1:
|
13 | 16 |
|
14 |
| -Input: s = "3[a]2[bc]" |
15 |
| -Output: "aaabcbc" |
| 17 | + |
| 18 | + |
| 19 | +Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] |
| 20 | +Output: 24 |
| 21 | +Explanation: Path of robot #1 and #2 are described in color green and blue respectively. |
| 22 | +Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. |
| 23 | +Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. |
| 24 | +Total of cherries: 12 + 12 = 24. |
16 | 25 | Example 2:
|
17 | 26 |
|
18 |
| -Input: s = "3[a2[c]]" |
19 |
| -Output: "accaccacc" |
| 27 | + |
| 28 | + |
| 29 | +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]] |
| 30 | +Output: 28 |
| 31 | +Explanation: Path of robot #1 and #2 are described in color green and blue respectively. |
| 32 | +Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17. |
| 33 | +Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11. |
| 34 | +Total of cherries: 17 + 11 = 28. |
20 | 35 | Example 3:
|
21 | 36 |
|
22 |
| -Input: s = "2[abc]3[cd]ef" |
23 |
| -Output: "abcabccdcdcdef" |
| 37 | +Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]] |
| 38 | +Output: 22 |
24 | 39 | Example 4:
|
25 | 40 |
|
26 |
| -Input: s = "abc3[cd]xyz" |
27 |
| -Output: "abccdcdcdxyz" |
| 41 | +Input: grid = [[1,1],[1,1]] |
| 42 | +Output: 4 |
28 | 43 |
|
29 | 44 |
|
30 | 45 | Constraints:
|
31 | 46 |
|
32 |
| -1 <= s.length <= 30 |
33 |
| -s consists of lowercase English letters, digits, and square brackets '[]'. |
34 |
| -s is guaranteed to be a valid input. |
35 |
| -All the integers in s are in the range [1, 300].<br> |
| 47 | +rows == grid.length |
| 48 | +cols == grid[i].length |
| 49 | +2 <= rows, cols <= 70 |
| 50 | +0 <= grid[i][j] <= 100 <br> |
36 | 51 |
|
37 | 52 | ## Idea
|
38 | 53 | Just do while loop and convert to decimal
|
|
0 commit comments