Skip to content

Commit bd79018

Browse files
committed
new file: bytedance/DNA序列编辑距离.py
modified: bytedance/test.py new file: bytedance/二进制之和.py new file: bytedance/小E的怪物挑战.py new file: bytedance/数字翻译成字符串的可能性.py new file: bytedance/有限制的楼梯攀登.py modified: bytedance/构造特定数组的逆序拼接.py new file: bytedance/比赛配对问题.py new file: bytedance/简单四则运算解析器.py new file: bytedance/队列.py
1 parent aad044f commit bd79018

10 files changed

+225
-10
lines changed

bytedance/DNA序列编辑距离.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def solution(dna1, dna2):
2+
dp = [[0 for _ in range(len(dna2) + 1)] for _ in range(len(dna1) + 1)]
3+
for i in range(1, len(dna1) + 1):
4+
dp[i][0] = i
5+
for i in range(1, len(dna2) + 1):
6+
dp[0][i] = i
7+
for i in range(1, len(dna1) + 1):
8+
for j in range(1, len(dna2) + 1):
9+
dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (dna1[i - 1] != dna2[j - 1]))
10+
return dp[-1][-1]
11+
12+
if __name__ == "__main__":
13+
# You can add more test cases here
14+
print(solution("AGCTTAGC", "AGCTAGCT") == 2 )
15+
print(solution("AGCCGAGC", "GCTAGCT") == 4)

bytedance/test.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
def solution(numbers):
1+
def solution(n: int, H: int, A: int, h: list, a: list) -> int:
2+
monsters = sorted(zip(h, a), reverse=True)
3+
dp = [0 for _ in range(n + 1)]
4+
for i in range(n - 1, -1, -1):
5+
dp[i] = 1
6+
for j in range(n - 1, i, -1):
7+
if monsters[j][0] < monsters[i][0] and monsters[j][1] < monsters[i][1]:
8+
dp[i] = max(dp[i], dp[j] + 1)
29
ans = 0
3-
def dfs(u, sum):
4-
if u == len(numbers):
5-
nonlocal ans
6-
if sum % 2 == 0:
7-
ans += 1
8-
for i in range(0, len(str(numbers[u]))):
9-
dfs(u + 1, sum + int(str(numbers[u])[i]))
10-
dfs(0, 0)
11-
return ans
10+
for i in range(n):
11+
if monsters[i][0] < H and monsters[i][1] < A:
12+
ans = max(ans, dp[i])
13+
return ans
14+
15+
# 3 4 5 第一行n H A
16+
# 1 2 3 第二行h
17+
# 3 2 1 第三行a
18+
if __name__ == "__main__":
19+
data = input().split()
20+
n = int(data[0])
21+
H = int(data[1])
22+
A = int(data[2])
23+
h = list(map(int, input().split()))
24+
a = list(map(int, input().split()))
25+
26+
print(solution(n, H, A, h, a))

bytedance/二进制之和.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def solution(binary1, binary2):
2+
# Convert binary to decimal
3+
decimal1 = int(binary1, 2)
4+
decimal2 = int(binary2, 2)
5+
# Add the two decimal numbers and convert the result to binary
6+
ans = decimal1 + decimal2
7+
return str(ans)
8+
if __name__ == "__main__":
9+
# You can add more test cases here
10+
print(solution("101", "110") == "11")
11+
print(solution("111111", "10100") == "83")
12+
print(solution("111010101001001011", "100010101001") == "242420")
13+
print(solution("111010101001011", "10010101001") == "31220")

bytedance/小E的怪物挑战.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def solution(n: int, H: int, A: int, h: list, a: list) -> int:
2+
monsters = sorted(zip(h, a), reverse=True)
3+
dp = [0 for _ in range(n + 1)]
4+
for i in range(n - 1, -1, -1):
5+
dp[i] = 1
6+
for j in range(n - 1, i, -1):
7+
if monsters[j][0] < monsters[i][0] and monsters[j][1] < monsters[i][1]:
8+
dp[i] = max(dp[i], dp[j] + 1)
9+
ans = 0
10+
for i in range(n):
11+
if monsters[i][0] < H and monsters[i][1] < A:
12+
ans = max(ans, dp[i])
13+
return ans
14+
15+
16+
if __name__ == '__main__':
17+
print(solution(3, 4, 5, [1, 2, 3], [3, 2, 1]) == 1)
18+
print(solution(5, 10, 10, [6, 9, 12, 4, 7], [8, 9, 10, 2, 5]) == 2)
19+
print(solution(4, 20, 25, [10, 15, 18, 22], [12, 18, 20, 26]) == 3)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def solution(num):
2+
num = str(num)
3+
ans = 0
4+
def dfs(u):
5+
nonlocal ans
6+
if u >= len(num) - 1:
7+
ans += 1
8+
return
9+
dfs(u + 1)
10+
if num[u:u + 2] <= '25' and num[u:u + 2] >= '10':
11+
dfs(u + 2)
12+
dfs(0)
13+
return ans
14+
15+
if __name__ == "__main__":
16+
# You can add more test cases here
17+
print(solution(12258) == 5)
18+
print(solution(1400112) == 6)
19+
print(solution(2110101) == 10)

bytedance/有限制的楼梯攀登.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def solution(n):
2+
dp = [[0 for _ in range(2)] for _ in range(n + 1)]
3+
dp[0][0] = 1
4+
dp[1][0] = 1
5+
dp[1][1] = 0
6+
for i in range(2, n + 1):
7+
dp[i][0] = dp[i - 1][0] + dp[i - 1][1]
8+
dp[i][1] = dp[i - 2][0]
9+
return dp[n][0] + dp[n][1]
10+
if __name__ == "__main__":
11+
# Add your test cases here
12+
13+
print(solution(2) == 2) # 样例1
14+
print(solution(3) == 3) # 样例2
15+
print(solution(4) == 4) # 样例3
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def solution(n: int) -> list:
2+
ans = [j for i in range(n) for j in range(n, i, -1)]
3+
return ans
4+
5+
if __name__ == '__main__':
6+
print(solution(3) == [3, 2, 1, 3, 2, 3])
7+
print(solution(4) == [4, 3, 2, 1, 4, 3, 2, 4, 3, 4])
8+
print(solution(5) == [5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5])

bytedance/比赛配对问题.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 打表找规律,发现结果都是n-1,所以直接返回n-1即可
2+
# def solution(x):
3+
# ans = 0
4+
# while x > 1:
5+
# if x % 2 == 0:
6+
# ans += x // 2
7+
# x //= 2
8+
# else:
9+
# ans += (x - 1) // 2
10+
# x = (x + 1) // 2
11+
# return ans
12+
13+
# if __name__ == '__main__':
14+
# for i in range(1, 100):
15+
# print(i, solution(i))
16+
17+
def solution(n: int) -> int:
18+
return n - 1
19+
20+
if __name__ == '__main__':
21+
print(solution(7) == 6)
22+
print(solution(14) == 13)
23+
print(solution(1) == 0)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
def solution(expression):
2+
def precedence(op):
3+
if op == '+' or op == '-':
4+
return 1
5+
if op == '*' or op == '/':
6+
return 2
7+
return 0
8+
9+
def apply_op(a, b, op):
10+
if op == '+':
11+
return a + b
12+
if op == '-':
13+
return a - b
14+
if op == '*':
15+
return a * b
16+
if op == '/':
17+
return a // b
18+
19+
def evaluate(tokens):
20+
values = []
21+
ops = []
22+
i = 0
23+
24+
while i < len(tokens):
25+
if tokens[i] == ' ':
26+
i += 1
27+
continue
28+
29+
if tokens[i] == '(':
30+
ops.append(tokens[i])
31+
32+
elif tokens[i].isdigit():
33+
val = 0
34+
while (i < len(tokens) and tokens[i].isdigit()):
35+
val = (val * 10) + int(tokens[i])
36+
i += 1
37+
values.append(val)
38+
i -= 1
39+
40+
elif tokens[i] == ')':
41+
while len(ops) != 0 and ops[-1] != '(':
42+
val2 = values.pop()
43+
val1 = values.pop()
44+
op = ops.pop()
45+
values.append(apply_op(val1, val2, op))
46+
ops.pop()
47+
48+
else:
49+
while (len(ops) != 0 and precedence(ops[-1]) >= precedence(tokens[i])):
50+
val2 = values.pop()
51+
val1 = values.pop()
52+
op = ops.pop()
53+
values.append(apply_op(val1, val2, op))
54+
ops.append(tokens[i])
55+
i += 1
56+
57+
while len(ops) != 0:
58+
val2 = values.pop()
59+
val1 = values.pop()
60+
op = ops.pop()
61+
values.append(apply_op(val1, val2, op))
62+
63+
return values[-1]
64+
65+
return evaluate(expression)
66+
67+
if __name__ == "__main__":
68+
# You can add more test cases here
69+
print(solution("1+1") == 2)
70+
print(solution("3+4*5/(3+2)") == 7)
71+
print(solution("4+2*5-2/1") == 12)
72+
print(solution("(1+(4+5+2)-3)+(6+8)") == 23)

bytedance/队列.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def solution(n: int, a: list) -> int:
2+
cur = 1
3+
ans = []
4+
for i in a:
5+
if i == cur:
6+
cur += 1
7+
ans.append(i)
8+
if len(ans) == 0:
9+
return -1
10+
else:
11+
return n - len(ans)
12+
13+
if __name__ == '__main__':
14+
print(solution(5, [1, 4, 2, 3, 5]) == 2)
15+
print(solution(3, [3, 3, 2]) == -1)
16+
print(solution(5, [1, 2, 3, 4, 5]) == 0)

0 commit comments

Comments
 (0)