头像

Gyp

一位业余算法爱好者


访客:4236

离线:3天前



Gyp
26天前
class Solution:
    def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
        d = {}

        def dfs(node, k):
            if node != None:
                if node.left != None:
                    d[node.left.val] = [node, k+1]
                    dfs(node.left, k+1)
                if node.right != None:
                    d[node.right.val] = [node, k+1]
                    dfs(node.right, k+1)

        dfs(root, 0)
        return x in d and y in d and (d[x][1] == d[y][1] and d[x][0] != d[y][0])


活动打卡代码 AcWing 826. 单链表

Gyp
1个月前
m = int(input())
e = [0 for _ in range(m)]
ne = [0 for _ in range(m)]
head = -1
idx = 0
for _ in range(m):
    ins = input().split()
    if ins[0] == "H":
       e[idx] = int(ins[1])
       ne[idx] = head
       head = idx
       idx += 1
    elif ins[0] == "I":
        k, val = int(ins[1]), int(ins[2])
        e[idx] = val
        ne[idx] = ne[k-1]
        ne[k-1] = idx
        idx += 1
    else:
        k = int(ins[1])
        if k == 0:
            head = ne[head]
        else:
            ne[k-1] = ne[ne[k-1]]
tmp = head
while tmp != -1:
    print(e[tmp], end = " ")
    tmp = ne[tmp]


活动打卡代码 AcWing 97. 约数之和

Gyp
1个月前
def ksm(n,k,m):
    tmp = 1
    a = n
    while k != 0:
        if k&1:
            tmp = (tmp * a) % m
        k >>= 1
        a = a * a% m
    return tmp


def calc(i,s):
    if s == 0:
        return 1
    if s % 2 == 1:
        return (1 + ksm(i,s//2+1, 9901)) * calc(i,s//2) % 9901
    if s % 2 == 0:
        return ksm(i,s, 9901) + calc(i,s-1)

a,b = map(int, input().split())
i = 2
res = 1
while i<=a:
    s = 0
    while a%i == 0:
        s += 1
        a /= i
    if s != 0:
        res = res * calc(i,b*s) % 9901
    i += 1
if a == 0:
    print("0")
else:
    print(res)


活动打卡代码 AcWing 95. 费解的开关

Gyp
1个月前
import copy

def turn(i,j):
    global a
    dx = [-1,0,0,0,1]
    dy = [0,1,0,-1,0]
    for s in range(5):
        x = i + dx[s]
        y = j + dy[s]
        if 0<=x<5 and 0<=y<5:
            a[x][y] ^= 1

def work():
    res = 30
    for i in range(1<<5):
        global a,co
        count = 0
        a = copy.deepcopy(co)
        for j in range(5):
            if i>>j & 1:
                turn(0,j)
                count += 1
        for m in range(4):
            for k in range(5):
                if a[m][k] == 0:
                    turn(m+1,k)
                    count += 1
        finished = True
        for k in range(5):
            if a[4][k] == 0:
                finished = False
        if finished and count<=6:
            res = min(res,count)
    if res > 6:
        return -1
    return res


n = int(input())
for i in range(n):
    a = []
    for j in range(5):
        line = input().strip("\n")
        c = []
        for k in range(5):
            c.append(ord(line[k])-ord("0"))
        a.append(c)
        co = copy.deepcopy(a)
    print(work())
    if i != n-1:
        input()


活动打卡代码 AcWing 90. 64位整数乘法

Gyp
1个月前
a = int(input())
b = int(input())
p = int(input())

print(a*b%p)


活动打卡代码 AcWing 282. 石子合并

Gyp
1个月前
n = int(input())
a = [0] + list(map(int, input().split()))
dp = [[float("inf") for _ in range(n)] for _ in range(n)]
for i in range(1,n+1):
    a[i] += a[i-1]
for i in range(n):
    dp[i][i] = 0

for l in range(1,n):
    for i in range(0,n-l):
        for k in range(0, l):
            dp[i][i+l] = min(dp[i][i+k] + dp[i+k+1][i+l] + a[i+l+1]-a[i], dp[i][i+l])
print(dp[0][n-1])


活动打卡代码 AcWing 104. 货仓选址

Gyp
1个月前
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(sum(abs(a[i] - a[len(a)//2]) for i in range(len(a))))


活动打卡代码 AcWing 103. 电影

Gyp
1个月前
import collections
n = int(input())
c = collections.Counter(list(map(int, input().split())))
s = collections.defaultdict(int)
k = 3*10**5
ans = -1
curS = -1
m = int(input())
y = list(map(int, input().split()))
z = list(map(int, input().split()))
for i in range(m):
    if c[y[i]] * k + c[z[i]] > curS:
        ans = i
        curS = c[y[i]] * k + c[z[i]]
print(ans + 1)



Gyp
1个月前
import collections
n = int(input())
c = collections.Counter(list(map(int, input().split())))
k = 3*10**5
ans = -1
curS = -1
m = int(input())
y = list(map(int, input().split()))
z = list(map(int, input().split()))
for i in range(m):
    if c[y[i]] * k + c[z[i]] > curS:
        ans = i
        curS = c[y[i]] * k + c[z[i]]
print(ans + 1)



Gyp
1个月前
class Solution:
    def constrainedSubsetSum(self, nums: List[int], k: int) -> int:
        dp = [-float("inf") for i in range(len(nums)+1)]
        for i in range(0,len(nums)):
            dp[i+1] = nums[i]
            for j in range(i,max(i-k,-1),-1):
                dp[i+1] = max(dp[i+1], dp[j] + nums[i])
                if j-1 >= 0 and nums[j-1]>=0:
                    break
        return max(dp)