头像

xanxus1111


访客:4087

在线 


活动打卡代码 LeetCode 74. 搜索二维矩阵

xanxus1111
1小时前
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        s = []
        if not matrix or not matrix[0]: return False
        for i in matrix:
            s.extend(i)

        l, r = 0,len(s)-1
        while l < r:
            mid = l+r>>1
            if s[mid]>= target   : r = mid
            else: l = mid + 1
        return  target == s[l]




xanxus1111
9小时前
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        if not len(matrix) or not len(matrix[0]): return 
        n, m = len(matrix), len(matrix[0])
        r0, c0 = 1, 1
        #遍历第一列是否有0
        for i in range(n): 
            if not matrix[i][0]: c0 = 0
        #遍历第一行是否有0
        for i in range(m): 
            if not matrix[0][i]: r0 = 0
        #检查每一列是否有0用第一列记录
        for j in range(1,m):
            for i in range(n):
                if not matrix[i][j]: matrix[0][j] = 0
        #检查每一行是否有0用第一行记录
        for i in range(1,n):
            for j in range(m):
                if not matrix[i][j]: matrix[i][0] = 0
        #如果第j列有0,那么这一列所有数=0
        for j in range(1, m):
            if not matrix[0][j]:
                for i in range(n):
                    matrix[i][j] = 0
        #如果第i行有0,那么这一行所有数=0
        for i in range(1,n):
            if not matrix[i][0]:
                for j in range(m):
                    matrix[i][j] = 0
        #如果r0是0,那么第一行所有数=0
        if not r0: 
            for j in range(m): matrix[0][j] = 0
        #如果c0是0,那么第一列所有数 = 0
        if not c0: 
            for i in range(n): matrix[i][0] = 0



活动打卡代码 LeetCode 73. 矩阵置零

xanxus1111
14小时前
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        if not len(matrix) or not len(matrix[0]): return 
        n, m = len(matrix), len(matrix[0])
        r0, c0 = 1, 1
        for i in range(n): 
            if not matrix[i][0]: c0 = 0
        for i in range(m): 
            if not matrix[0][i]: r0 = 0

        for j in range(1,m):
            for i in range(n):
                if not matrix[i][j]: matrix[0][j] = 0

        for i in range(1,n):
            for j in range(m):
                if not matrix[i][j]: matrix[i][0] = 0

        for j in range(1, m):
            if not matrix[0][j]:
                for i in range(n):
                    matrix[i][j] = 0

        for i in range(1,n):
            if not matrix[i][0]:
                for j in range(m):
                    matrix[i][j] = 0

        if not r0: 
            for j in range(m): matrix[0][j] = 0

        if not c0: 
            for i in range(n): matrix[i][0] = 0



活动打卡代码 LeetCode 72. 编辑距离

def minDistance(self, a: str, b: str) -> int:
        a, b = ' ' + a,' ' + b
        n, m = len(a), len(b)
        f = [[0]*(m) for i in range(n)]

        for i in range(n): f[i][0] = i
        for i in range(1,m): f[0][i] = i

        for i in range(1,n):
            for j in range(1,m):
                f[i][j] = min(f[i-1][j],f[i][j-1])+1
                t = a[i] != b[j]
                f[i][j] = min(f[i][j],f[i-1][j-1]+t)
        return f[n-1][m-1]



活动打卡代码 LeetCode 71. 简化路径

class Solution:
    def simplifyPath(self, path: str) -> str:
        res = []
        x = path.split("/")
        for i in x:
            if i == '' or i == '.':
                continue
            elif i == '..':
                if res:
                    res.pop()
            else:
                res.append(i)
        return "/"+"/".join(str(x) for x in res)




N = 12
M = 1 << N
f = [[0]*M for _ in range(N)]
st = [False]*M

if __name__ == "__main__":
    n,m = map(int, input().split())
    while n or m:
        #初始化数组
        for i in range(len(f)):
            for j in range(len(f[0])):
                f[i][j] = 0
        for i in range(len(st)):
            st[i] = False
        #预处理
        for i in range(1<<n):
            st[i] = True
            cnt = 0
            for j in range(n):
                if i >> j & 1:
                    if cnt & 1:
                        st[i] = False
                    cnt = 0
                else:cnt += 1
            if cnt & 1:st[i] = False
        #
        f[0][0] = 1    
        for i in range(1, m+1):
            for j in range(1<<n):
                for k in range(1<<n):
                    if j&k == 0 and st[j|k]:
                        f[i][j] += f[i-1][k]

        print(f[m][0])   
        n,m = map(int, input().split())



活动打卡代码 AcWing 148. 合并果子

import heapq
if __name__ == '__main__':
    n = int(input())
    h = list(map(int,input().split()))

    heapq.heapify(h)

    res = 0
    while len(h) > 1:
        a = heapq.heappop(h)
        b = heapq.heappop(h)
        a += b
        heapq.heappush(h,a)
        res += a
    print(res)





if __name__ == '__main__':
    n = int(input())
    q = []
    for i in range(n):
        w,s = map(int,input().split())
        q.append([w+s,w,s])

    q.sort()
    res = -float('inf')
    sum = 0
    for i in range(n):
        res = max(res,sum - q[i][2])
        sum += q[i][1]
    print(res)


活动打卡代码 AcWing 125. 耍杂技的牛

if __name__ == '__main__':
    n = int(input())
    q = []
    for i in range(n):
        w,s = map(int,input().split())
        q.append([w+s,w,s])

    q.sort()
    res = -float('inf')
    sum = 0
    for i in range(n):
        res = max(res,sum - q[i][2])
        sum += q[i][1]
    print(res)


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

if __name__=='__main__':
    n = int(input())
    q = list(map(int,input().split()))
    q.sort()
    res = 0
    for i in range(n): 
        res += abs(q[i] - q[n//2])

    print(res)