diff --git "a/bytedance/DNA\345\272\217\345\210\227\347\274\226\350\276\221\350\267\235\347\246\273.py" "b/bytedance/DNA\345\272\217\345\210\227\347\274\226\350\276\221\350\267\235\347\246\273.py" new file mode 100644 index 0000000..4e8a00c --- /dev/null +++ "b/bytedance/DNA\345\272\217\345\210\227\347\274\226\350\276\221\350\267\235\347\246\273.py" @@ -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) \ No newline at end of file diff --git a/bytedance/test.py b/bytedance/test.py index 7c66574..ddc7e86 100644 --- a/bytedance/test.py +++ b/bytedance/test.py @@ -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 \ No newline at end of file + 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)) \ No newline at end of file diff --git "a/bytedance/\344\272\214\350\277\233\345\210\266\344\271\213\345\222\214.py" "b/bytedance/\344\272\214\350\277\233\345\210\266\344\271\213\345\222\214.py" new file mode 100644 index 0000000..f311cbe --- /dev/null +++ "b/bytedance/\344\272\214\350\277\233\345\210\266\344\271\213\345\222\214.py" @@ -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") \ No newline at end of file diff --git "a/bytedance/\345\260\217E\347\232\204\346\200\252\347\211\251\346\214\221\346\210\230.py" "b/bytedance/\345\260\217E\347\232\204\346\200\252\347\211\251\346\214\221\346\210\230.py" new file mode 100644 index 0000000..bec5c22 --- /dev/null +++ "b/bytedance/\345\260\217E\347\232\204\346\200\252\347\211\251\346\214\221\346\210\230.py" @@ -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) \ No newline at end of file diff --git "a/bytedance/\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\345\217\257\350\203\275\346\200\247.py" "b/bytedance/\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\345\217\257\350\203\275\346\200\247.py" new file mode 100644 index 0000000..ec5ac1e --- /dev/null +++ "b/bytedance/\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262\347\232\204\345\217\257\350\203\275\346\200\247.py" @@ -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) \ No newline at end of file diff --git "a/bytedance/\346\234\211\351\231\220\345\210\266\347\232\204\346\245\274\346\242\257\346\224\200\347\231\273.py" "b/bytedance/\346\234\211\351\231\220\345\210\266\347\232\204\346\245\274\346\242\257\346\224\200\347\231\273.py" new file mode 100644 index 0000000..99d9506 --- /dev/null +++ "b/bytedance/\346\234\211\351\231\220\345\210\266\347\232\204\346\245\274\346\242\257\346\224\200\347\231\273.py" @@ -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 \ No newline at end of file diff --git "a/bytedance/\346\236\204\351\200\240\347\211\271\345\256\232\346\225\260\347\273\204\347\232\204\351\200\206\345\272\217\346\213\274\346\216\245.py" "b/bytedance/\346\236\204\351\200\240\347\211\271\345\256\232\346\225\260\347\273\204\347\232\204\351\200\206\345\272\217\346\213\274\346\216\245.py" index e69de29..793a289 100644 --- "a/bytedance/\346\236\204\351\200\240\347\211\271\345\256\232\346\225\260\347\273\204\347\232\204\351\200\206\345\272\217\346\213\274\346\216\245.py" +++ "b/bytedance/\346\236\204\351\200\240\347\211\271\345\256\232\346\225\260\347\273\204\347\232\204\351\200\206\345\272\217\346\213\274\346\216\245.py" @@ -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]) \ No newline at end of file diff --git "a/bytedance/\346\257\224\350\265\233\351\205\215\345\257\271\351\227\256\351\242\230.py" "b/bytedance/\346\257\224\350\265\233\351\205\215\345\257\271\351\227\256\351\242\230.py" new file mode 100644 index 0000000..b46477a --- /dev/null +++ "b/bytedance/\346\257\224\350\265\233\351\205\215\345\257\271\351\227\256\351\242\230.py" @@ -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) \ No newline at end of file diff --git "a/bytedance/\347\256\200\345\215\225\345\233\233\345\210\231\350\277\220\347\256\227\350\247\243\346\236\220\345\231\250.py" "b/bytedance/\347\256\200\345\215\225\345\233\233\345\210\231\350\277\220\347\256\227\350\247\243\346\236\220\345\231\250.py" new file mode 100644 index 0000000..773ca1c --- /dev/null +++ "b/bytedance/\347\256\200\345\215\225\345\233\233\345\210\231\350\277\220\347\256\227\350\247\243\346\236\220\345\231\250.py" @@ -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) \ No newline at end of file diff --git "a/bytedance/\351\230\237\345\210\227.py" "b/bytedance/\351\230\237\345\210\227.py" new file mode 100644 index 0000000..dff46c6 --- /dev/null +++ "b/bytedance/\351\230\237\345\210\227.py" @@ -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) \ No newline at end of file