Skip to content

Commit 2bab51a

Browse files
authored
Merge pull request #8 from adhocore/code
2 parents 964990b + 4a8df3e commit 2bab51a

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

.pylintrc

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ function-naming-style=snake_case
3838
good-names=i,
3939
j,
4040
k,
41+
n,
42+
s,
43+
t,
4144
ex,
4245
Run,
4346
_

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ Just some (n|l)eet code solutions in Python.
44

55
- [Container with max water](./containermaxwater.py)
66
- [Eval reverse Polish notation](./evalrpn.py)
7+
- [Generate balanced parentheses](./generateparens.py)
78
- [Group anagrams together](./groupanagrams.py)
89
- [Is Anagram](./isanagram.py)
910
- [Longest consecutive seq](./longestconsecutiveseq.py)
1011
- [Longest non repeat substr len](./longestsubstrlen.py)
1112
- [Product except self](./productexceptself.py)
13+
- [Queen attack](./queenattack.py)
1214
- [Stock max profit - single txn](./stockmaxprofit.py)
1315
- [String permutations as substring](./substrpermute.py)
1416
- [Three sum to zero](./threesumzero.py)

generateparens.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ def generate_parenthesis(num: int) -> list[str]:
77
Space Complexity: O(1)
88
Time Complexity: O(n)
99
"""
10+
1011
def generate(open_count: int, close_count: int, gen: str):
1112
if open_count == num and close_count == num:
1213
ans.append(gen)
1314
return
1415
if open_count < num:
15-
generate(open_count + 1, close_count, gen + '(')
16+
generate(open_count + 1, close_count, gen + "(")
1617
if open_count > close_count:
17-
generate(open_count, close_count + 1, gen + ')')
18+
generate(open_count, close_count + 1, gen + ")")
1819

1920
ans = []
2021
generate(0, 0, "")

queenattack.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Number of postions a Queen can attack
2+
3+
The number of positions in chess board that a Queen can attack in eight
4+
directions excluding obstacles.
5+
"""
6+
7+
8+
def queen_attack(
9+
board_sz: int, queen_x: int, queen_y: int, obstacles: list[list[int]]
10+
) -> int:
11+
"""
12+
Space Complexity: O(1)
13+
Time Complexity: O(8*n) => O(n)
14+
"""
15+
ans = 0
16+
dirs = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [-1, -1], [1, -1], [-1, 1]]
17+
obs = {(i[0], i[1]) for i in obstacles}
18+
for dr in dirs:
19+
x, y = queen_x + dr[0], queen_y + dr[1]
20+
while 1 <= x <= board_sz and 1 <= y <= board_sz and (x, y) not in obs:
21+
ans, x, y = ans + 1, x + dr[0], y + dr[1]
22+
return ans
23+
24+
25+
if __name__ == "__main__":
26+
print(queen_attack(5, 4, 3, [[5, 5], [4, 2], [2, 3]]))
27+
# => 10

0 commit comments

Comments
 (0)