Skip to content

Commit 48db2a9

Browse files
committed
2 parents a46e47b + 72998aa commit 48db2a9

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```Python
2+
class Solution:
3+
def gridGame(self, grid, k, rules):
4+
if not grid or grid == []: return grid
5+
if len(grid) == 0 or len(grid[0]) == 0: return grid
6+
rules = set(rules)
7+
8+
for i in range(k):
9+
grid = self.updateGrid(grid, rules)
10+
11+
return grid
12+
13+
def updateGrid(self, grid, rules):
14+
m, n = len(grid), len(grid[0])
15+
new_grid = [[0 for _ in range(n)] for _ in range(m)]
16+
directions = [(1,0),(0,1),(-1,0),(0,-1),(-1,-1),(-1,1),(1,-1),(1,1)]
17+
18+
for i in range(m):
19+
for j in range(n):
20+
count = 0
21+
for di, dj in directions:
22+
if 0 <= i + di < m and 0 <= j + dj < n and grid[i+di][j+dj] == 1:
23+
count += 1
24+
if count in rules: new_grid[i][j] = 1
25+
26+
return new_grid
27+
28+
s = Solution()
29+
grid = [[0,1,1,0], [1,1,0,0]]
30+
k = 2
31+
rules = [3,5]
32+
33+
print(s.gridGame(grid, k, rules))
34+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Compression String
2+
Given an input string of a certain length, design an algorithm that compresses the string.
3+
4+
The string should be compressed such that consecutive duplicates of characters are replaced with the character and followed by the number of consecutive duplicates.
5+
6+
For example, if the input string is “wwwwaaadexxxxxx”, then the function should return “w4a3dex6”.
7+
8+
```Python
9+
10+
def compress(string):
11+
12+
res = ""
13+
14+
count = 1
15+
16+
#Add in first character
17+
res += string[0]
18+
19+
#Iterate through loop, skipping last one
20+
for i in range(len(string)-1):
21+
if(string[i] == string[i+1]):
22+
count+=1
23+
else:
24+
if(count > 1):
25+
#Ignore if no repeats
26+
res += str(count)
27+
res += string[i+1]
28+
count = 1
29+
#print last one
30+
if(count > 1):
31+
res += str(count)
32+
return res
33+
34+
compress('abaasass')
35+
```
36+

0 commit comments

Comments
 (0)