Skip to content

Commit

Permalink
new file: bytedance/DNA序列编辑距离.py
Browse files Browse the repository at this point in the history
	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
  • Loading branch information
sakuralggm committed Oct 23, 2024
1 parent aad044f commit bd79018
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 10 deletions.
15 changes: 15 additions & 0 deletions bytedance/DNA序列编辑距离.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def solution(dna1, dna2):
dp = [[0 for _ in range(len(dna2) + 1)] for _ in range(len(dna1) + 1)]
for i in range(1, len(dna1) + 1):
dp[i][0] = i
for i in range(1, len(dna2) + 1):
dp[0][i] = i
for i in range(1, len(dna1) + 1):
for j in range(1, len(dna2) + 1):
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]))
return dp[-1][-1]

if __name__ == "__main__":
# You can add more test cases here
print(solution("AGCTTAGC", "AGCTAGCT") == 2 )
print(solution("AGCCGAGC", "GCTAGCT") == 4)
35 changes: 25 additions & 10 deletions bytedance/test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
def solution(numbers):
def solution(n: int, H: int, A: int, h: list, a: list) -> int:
monsters = sorted(zip(h, a), reverse=True)
dp = [0 for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
dp[i] = 1
for j in range(n - 1, i, -1):
if monsters[j][0] < monsters[i][0] and monsters[j][1] < monsters[i][1]:
dp[i] = max(dp[i], dp[j] + 1)
ans = 0
def dfs(u, sum):
if u == len(numbers):
nonlocal ans
if sum % 2 == 0:
ans += 1
for i in range(0, len(str(numbers[u]))):
dfs(u + 1, sum + int(str(numbers[u])[i]))
dfs(0, 0)
return ans
for i in range(n):
if monsters[i][0] < H and monsters[i][1] < A:
ans = max(ans, dp[i])
return ans

# 3 4 5 第一行n H A
# 1 2 3 第二行h
# 3 2 1 第三行a
if __name__ == "__main__":
data = input().split()
n = int(data[0])
H = int(data[1])
A = int(data[2])
h = list(map(int, input().split()))
a = list(map(int, input().split()))

print(solution(n, H, A, h, a))
13 changes: 13 additions & 0 deletions bytedance/二进制之和.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def solution(binary1, binary2):
# Convert binary to decimal
decimal1 = int(binary1, 2)
decimal2 = int(binary2, 2)
# Add the two decimal numbers and convert the result to binary
ans = decimal1 + decimal2
return str(ans)
if __name__ == "__main__":
# You can add more test cases here
print(solution("101", "110") == "11")
print(solution("111111", "10100") == "83")
print(solution("111010101001001011", "100010101001") == "242420")
print(solution("111010101001011", "10010101001") == "31220")
19 changes: 19 additions & 0 deletions bytedance/小E的怪物挑战.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def solution(n: int, H: int, A: int, h: list, a: list) -> int:
monsters = sorted(zip(h, a), reverse=True)
dp = [0 for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
dp[i] = 1
for j in range(n - 1, i, -1):
if monsters[j][0] < monsters[i][0] and monsters[j][1] < monsters[i][1]:
dp[i] = max(dp[i], dp[j] + 1)
ans = 0
for i in range(n):
if monsters[i][0] < H and monsters[i][1] < A:
ans = max(ans, dp[i])
return ans


if __name__ == '__main__':
print(solution(3, 4, 5, [1, 2, 3], [3, 2, 1]) == 1)
print(solution(5, 10, 10, [6, 9, 12, 4, 7], [8, 9, 10, 2, 5]) == 2)
print(solution(4, 20, 25, [10, 15, 18, 22], [12, 18, 20, 26]) == 3)
19 changes: 19 additions & 0 deletions bytedance/数字翻译成字符串的可能性.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def solution(num):
num = str(num)
ans = 0
def dfs(u):
nonlocal ans
if u >= len(num) - 1:
ans += 1
return
dfs(u + 1)
if num[u:u + 2] <= '25' and num[u:u + 2] >= '10':
dfs(u + 2)
dfs(0)
return ans

if __name__ == "__main__":
# You can add more test cases here
print(solution(12258) == 5)
print(solution(1400112) == 6)
print(solution(2110101) == 10)
15 changes: 15 additions & 0 deletions bytedance/有限制的楼梯攀登.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def solution(n):
dp = [[0 for _ in range(2)] for _ in range(n + 1)]
dp[0][0] = 1
dp[1][0] = 1
dp[1][1] = 0
for i in range(2, n + 1):
dp[i][0] = dp[i - 1][0] + dp[i - 1][1]
dp[i][1] = dp[i - 2][0]
return dp[n][0] + dp[n][1]
if __name__ == "__main__":
# Add your test cases here

print(solution(2) == 2) # 样例1
print(solution(3) == 3) # 样例2
print(solution(4) == 4) # 样例3
8 changes: 8 additions & 0 deletions bytedance/构造特定数组的逆序拼接.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def solution(n: int) -> list:
ans = [j for i in range(n) for j in range(n, i, -1)]
return ans

if __name__ == '__main__':
print(solution(3) == [3, 2, 1, 3, 2, 3])
print(solution(4) == [4, 3, 2, 1, 4, 3, 2, 4, 3, 4])
print(solution(5) == [5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5])
23 changes: 23 additions & 0 deletions bytedance/比赛配对问题.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 打表找规律,发现结果都是n-1,所以直接返回n-1即可
# def solution(x):
# ans = 0
# while x > 1:
# if x % 2 == 0:
# ans += x // 2
# x //= 2
# else:
# ans += (x - 1) // 2
# x = (x + 1) // 2
# return ans

# if __name__ == '__main__':
# for i in range(1, 100):
# print(i, solution(i))

def solution(n: int) -> int:
return n - 1

if __name__ == '__main__':
print(solution(7) == 6)
print(solution(14) == 13)
print(solution(1) == 0)
72 changes: 72 additions & 0 deletions bytedance/简单四则运算解析器.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
def solution(expression):
def precedence(op):
if op == '+' or op == '-':
return 1
if op == '*' or op == '/':
return 2
return 0

def apply_op(a, b, op):
if op == '+':
return a + b
if op == '-':
return a - b
if op == '*':
return a * b
if op == '/':
return a // b

def evaluate(tokens):
values = []
ops = []
i = 0

while i < len(tokens):
if tokens[i] == ' ':
i += 1
continue

if tokens[i] == '(':
ops.append(tokens[i])

elif tokens[i].isdigit():
val = 0
while (i < len(tokens) and tokens[i].isdigit()):
val = (val * 10) + int(tokens[i])
i += 1
values.append(val)
i -= 1

elif tokens[i] == ')':
while len(ops) != 0 and ops[-1] != '(':
val2 = values.pop()
val1 = values.pop()
op = ops.pop()
values.append(apply_op(val1, val2, op))
ops.pop()

else:
while (len(ops) != 0 and precedence(ops[-1]) >= precedence(tokens[i])):
val2 = values.pop()
val1 = values.pop()
op = ops.pop()
values.append(apply_op(val1, val2, op))
ops.append(tokens[i])
i += 1

while len(ops) != 0:
val2 = values.pop()
val1 = values.pop()
op = ops.pop()
values.append(apply_op(val1, val2, op))

return values[-1]

return evaluate(expression)

if __name__ == "__main__":
# You can add more test cases here
print(solution("1+1") == 2)
print(solution("3+4*5/(3+2)") == 7)
print(solution("4+2*5-2/1") == 12)
print(solution("(1+(4+5+2)-3)+(6+8)") == 23)
16 changes: 16 additions & 0 deletions bytedance/队列.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def solution(n: int, a: list) -> int:
cur = 1
ans = []
for i in a:
if i == cur:
cur += 1
ans.append(i)
if len(ans) == 0:
return -1
else:
return n - len(ans)

if __name__ == '__main__':
print(solution(5, [1, 4, 2, 3, 5]) == 2)
print(solution(3, [3, 3, 2]) == -1)
print(solution(5, [1, 2, 3, 4, 5]) == 0)

0 comments on commit bd79018

Please sign in to comment.