Skip to content

Commit b22fa00

Browse files
committed
20/12/2020
1 parent 5ff4cf6 commit b22fa00

File tree

3 files changed

+67
-56
lines changed

3 files changed

+67
-56
lines changed

Daily-challenge/Dec/19/README.md

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,22 @@ Just do while loop and convert to decimal
5555
## Code
5656
```python
5757
class Solution:
58-
def decodeString(self, s: str) -> str:
59-
stack = []
60-
curnum = 0
61-
curstring = ''
62-
63-
for c in s:
64-
if c == '[':
65-
stack.append(curstring)
66-
stack.append(curnum)
67-
curstring = ''
68-
curnum = 0
69-
70-
elif c == ']':
71-
num = stack.pop()
72-
prevstring = stack.pop()
73-
curstring = prevstring + num* curstring
74-
elif c.isdigit():
75-
curnum = curnum * 10 + int(c)
76-
else:
77-
curstring += c
78-
79-
return curstring
58+
def cherryPickup(self, grid: List[List[int]]) -> int:
59+
moves = [(1,-1),(1,0),(1,1)]
60+
m,n = len(grid), len(grid[0])
61+
@functools.lru_cache(None)
62+
def dp(r1,c1,r2,c2):
63+
if(c1>c2): return dp(r2,c2,r1,c1)
64+
if not (r1<m and r2<m and 0<=c1<n and 0<=c2<n):
65+
return float("-inf")
66+
if r1==m-1:
67+
return grid[r1][c1]+grid[r2][c2] if r1!=r2 or c1!=c2 else 0
68+
maxi = 0
69+
for d1 in moves:
70+
for d2 in moves:
71+
dr1,dc1=r1+d1[0],c1+d1[1]
72+
dr2,dc2=r2+d2[0],c2+d2[1]
73+
maxi = max(maxi, dp(dr1,dc1,dr2,dc2)+grid[r1][c1]+grid[r2][c2] if r1!=r2 or c1!=c2 else 0)
74+
return maxi
75+
return dp(0,0,0,n-1)
8076
```

Daily-challenge/Dec/19/sol.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
class Solution:
2-
def decodeString(self, s: str) -> str:
3-
stack = []
4-
curnum = 0
5-
curstring = ''
6-
7-
for c in s:
8-
if c == '[':
9-
stack.append(curstring)
10-
stack.append(curnum)
11-
curstring = ''
12-
curnum = 0
13-
14-
elif c == ']':
15-
num = stack.pop()
16-
prevstring = stack.pop()
17-
curstring = prevstring + num* curstring
18-
elif c.isdigit():
19-
curnum = curnum * 10 + int(c)
20-
else:
21-
curstring += c
22-
23-
return curstring
2+
def cherryPickup(self, grid: List[List[int]]) -> int:
3+
moves = [(1,-1),(1,0),(1,1)]
4+
m,n = len(grid), len(grid[0])
5+
@functools.lru_cache(None)
6+
def dp(r1,c1,r2,c2):
7+
if(c1>c2): return dp(r2,c2,r1,c1)
8+
if not (r1<m and r2<m and 0<=c1<n and 0<=c2<n):
9+
return float("-inf")
10+
if r1==m-1:
11+
return grid[r1][c1]+grid[r2][c2] if r1!=r2 or c1!=c2 else 0
12+
maxi = 0
13+
for d1 in moves:
14+
for d2 in moves:
15+
dr1,dc1=r1+d1[0],c1+d1[1]
16+
dr2,dc2=r2+d2[0],c2+d2[1]
17+
maxi = max(maxi, dp(dr1,dc1,dr2,dc2)+grid[r1][c1]+grid[r2][c2] if r1!=r2 or c1!=c2 else 0)
18+
return maxi
19+
return dp(0,0,0,n-1)

Daily-challenge/Dec/20/README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
# Search in Rotated Sorted Array II
2-
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
1+
# Decoded String at Index
2+
An encoded string S is given. To find and write the decoded string to a tape, the encoded string is read one character at a time and the following steps are taken:
33

4-
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
4+
If the character read is a letter, that letter is written onto the tape.
5+
If the character read is a digit (say d), the entire current tape is repeatedly written d-1 more times in total.
6+
Now for some encoded string S, and an index K, find and return the K-th letter (1 indexed) in the decoded string.
57

6-
You are given a target value to search. If found in the array return true, otherwise return false.
8+
79

810
Example 1:
911

10-
Input: nums = [2,5,6,0,0,1,2], target = 0
11-
Output: true
12+
Input: S = "leet2code3", K = 10
13+
Output: "o"
14+
Explanation:
15+
The decoded string is "leetleetcodeleetleetcodeleetleetcode".
16+
The 10th letter in the string is "o".
1217
Example 2:
1318

14-
Input: nums = [2,5,6,0,0,1,2], target = 3
15-
Output: false
16-
Follow up:
17-
18-
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
19-
Would this affect the run-time complexity? How and why?<br>
19+
Input: S = "ha22", K = 5
20+
Output: "h"
21+
Explanation:
22+
The decoded string is "hahahaha". The 5th letter is "h".
23+
Example 3:
24+
25+
Input: S = "a2345678999999999999999", K = 1
26+
Output: "a"
27+
Explanation:
28+
The decoded string is "a" repeated 8301530446056247680 times. The 1st letter is "a".
29+
30+
31+
Constraints:
32+
33+
2 <= S.length <= 100
34+
S will only contain lowercase letters and digits 2 through 9.
35+
S starts with a letter.
36+
1 <= K <= 10^9
37+
It's guaranteed that K is less than or equal to the length of the decoded string.
38+
The decoded string is guaranteed to have less than 2^63 letters.<br>
2039

2140
## Idea
2241
Just do while loop and convert to decimal

0 commit comments

Comments
 (0)