Skip to content

Commit 81c1043

Browse files
committed
added Parsing a boolean expression
1 parent d975de8 commit 81c1043

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import functools
2+
import operator
3+
4+
class Solution:
5+
def parseBoolExpr(self, expression: str) -> bool:
6+
def dfs(s: int, e: int) -> list[str]:
7+
if s == e:
8+
return True if expression[s] == 't' else False
9+
10+
exps = []
11+
layer = 0
12+
13+
for i in range(s, e + 1):
14+
c = expression[i]
15+
if layer == 0 and c in '!&|':
16+
op = c
17+
elif c == '(':
18+
layer += 1
19+
if layer == 1:
20+
left = i + 1
21+
elif c == ')':
22+
layer -= 1
23+
if layer == 0:
24+
exps.append(dfs(left, i - 1))
25+
elif c == ',' and layer == 1:
26+
exps.append(dfs(left, i - 1))
27+
left = i + 1
28+
29+
if op == '|':
30+
return functools.reduce(operator.or_, exps)
31+
if op == '&':
32+
return functools.reduce(operator.and_, exps)
33+
if op == '!':
34+
return not exps[0]
35+
36+
return dfs(0, len(expression) - 1)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import functools
2+
import operator
3+
4+
class Solution:
5+
def countMaxOrSubsets(self, nums: list[int]) -> int:
6+
ors = functools.reduce(operator.or_, nums)
7+
ans = 0
8+
9+
def dfs(i: int, path: int) -> None:
10+
nonlocal ans
11+
if i == len(nums):
12+
if path == ors:
13+
ans += 1
14+
return
15+
dfs(i + 1, path)
16+
dfs(i + 1, path | nums[i])
17+
18+
dfs(0, 0)
19+
20+
return ans
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def findKthBit(self, n: int, k: int) -> str:
3+
if n == 1:
4+
return '0'
5+
midIndex = pow(2, n - 1) # 1-indexed
6+
if k == midIndex:
7+
return '1'
8+
if k < midIndex:
9+
return self.findKthBit(n - 1, k)
10+
11+
return '1' if self.findKthBit(n - 1, midIndex * 2 - k) == '0' else '0'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from heapq import heappop, heappush
2+
3+
class Solution:
4+
def longestDiverseString(self, a: int, b: int, c: int) -> str:
5+
h = []
6+
if a > 0:
7+
heappush(h, [-a, 'a'])
8+
if b > 0:
9+
heappush(h, [-b, 'b'])
10+
if c > 0:
11+
heappush(h, [-c, 'c'])
12+
13+
ans = []
14+
while len(h) > 0:
15+
cur = heappop(h)
16+
if len(ans) >= 2 and ans[-1] == cur[1] and ans[-2] == cur[1]:
17+
if len(h) == 0:
18+
break
19+
nxt = heappop(h)
20+
ans.append(nxt[1])
21+
if -nxt[0] > 1:
22+
nxt[0] += 1
23+
heappush(h, nxt)
24+
heappush(h, cur)
25+
else:
26+
ans.append(cur[1])
27+
if -cur[0] > 1:
28+
cur[0] += 1
29+
heappush(h, cur)
30+
31+
return ''.join(ans)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from ast import List
2+
import heapq
3+
4+
class Solution:
5+
def maxKelements(self, nums: List[int], k: int) -> int:
6+
ans = 0
7+
maxHeap = [-num for num in nums]
8+
heapq.heapify(maxHeap)
9+
10+
for _ in range(k):
11+
num = -heapq.heappop(maxHeap)
12+
ans += num
13+
heapq.heappush(maxHeap, -math.ceil(num / 3))
14+
15+
return ans
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import string
2+
3+
class Solution:
4+
def maximumSwap(self, num: int) -> int:
5+
s = list(str(num))
6+
dict = {c: i for i, c in enumerate(s)}
7+
8+
for i, c in enumerate(s):
9+
for digit in reversed(string.digits):
10+
if digit <= c:
11+
break
12+
if digit in dict and dict[digit] > i:
13+
s[i], s[dict[digit]] = digit, s[i]
14+
return int(''.join(s))
15+
16+
return num
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def minimumSteps(self, s: str) -> int:
3+
ans = 0
4+
ones = 0
5+
6+
for c in s:
7+
if c == '1':
8+
ones += 1
9+
else: # Move 1s to the front of the current '0'.
10+
ans += ones
11+
12+
return ans

0 commit comments

Comments
 (0)