Leetcode周赛 | 2023-8-6

2023-8-6

题1


体会

这道题完全就是唬人,只要想明白了,只要有两个连续的数的和,大于target,那么一定可以,两边一次切一个就好了。

我的代码

题2

我的超时代码

尽力了,想不出来别的方法,只能通过一半的测试用例,其他超时。

将向左和向下的代码去掉后,因为我觉得向左和向右是无意义的,通过的用例多了一些,但还是超时。

python 复制代码
class Solution:
    def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
        note = []
        m = len(grid)
        n = len(grid[0])
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1 :
                    note.append([i,j])
                    if i == 0 and j == 0 :
                        return 0
                    if i == m-1 and j == n-1 :
                        return 0
        
        self.mm = 0
        i = j = 0
        mini = inf
        self.dfs(grid,i,j,mini,note,m,n)
        
        return self.mm
        
    def dfs(self,grid,i,j,mini,note,m,n):
        if i < 0 or i >= m or j < 0 or j >= n :
            return
        if grid[i][j] == 2 :
            return
        
        temp = [[p[0]-i,p[1]-j] for p in note]     
        for k in temp :
            mini = min(mini,abs(k[0])+abs(k[1]))
        
        if mini <= self.mm :
            return
            
        if i == m-1 and j == n-1 :
            self.mm = max(self.mm,mini)
            return                
        grid[i][j] = 2
        self.dfs(grid,i+1,j,mini,note,m,n)
        self.dfs(grid,i-1,j,mini,note,m,n)
        self.dfs(grid,i,j+1,mini,note,m,n)
        self.dfs(grid,i,j-1,mini,note,m,n)
        grid[i][j] = 0
        

改成了动态规划的方法,还是解答错误,但是通过的用例更多了

(983 / 1035)

python 复制代码
class Solution:
    def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
        note = []
        m = len(grid)
        n = len(grid[0])
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1 :
                    note.append([i,j])
                    if i == 0 and j == 0 :
                        return 0
                    if i == m-1 and j == n-1 :
                        return 0
        dp = [[0]*(n) for _ in range(m)]
        for i in range(0,m):
            for j in range(0,n):
                mini = inf
                temp = [[p[0]-(i),p[1]-(j)] for p in note]     
                for k in temp :
                    mini = min(mini,abs(k[0])+abs(k[1]))
                '''               
                if mini < dp[i-1][j] and mini < dp[i][j-1] :
                    dp[i][j] = mini
                elif mini > dp[i-1][j] and mini > dp[i][j-1] :
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1])
                else :
                    dp[i][j] = mini
                '''
                if i == 0 and j != 0:
                    kk = dp[i][j-1]
                elif j == 0 and i!=0:
                    kk =  dp[i-1][j]
                elif j == 0 and i==0:
                    kk =  inf
                else :
                    kk = max(dp[i-1][j],dp[i][j-1])
                if mini < kk :
                    dp[i][j] = mini
                else :
                    dp[i][j] = kk
       
        #return dp
        return dp[-1][-1]
                     

两次动归也不对。

python 复制代码
class Solution:
    def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
        note = []
        m = len(grid)
        n = len(grid[0])
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1 :
                    note.append([i,j])
                    if i == 0 and j == 0 :
                        return 0
                    if i == m-1 and j == n-1 :
                        return 0
        dp = [[0]*(n) for _ in range(m)]
        for i in range(0,m):
            for j in range(0,n):
                mini = inf
                temp = [[p[0]-(i),p[1]-(j)] for p in note]     
                for k in temp :
                    mini = min(mini,abs(k[0])+abs(k[1]))
                '''               
                if mini < dp[i-1][j] and mini < dp[i][j-1] :
                    dp[i][j] = mini
                elif mini > dp[i-1][j] and mini > dp[i][j-1] :
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1])
                else :
                    dp[i][j] = mini
                '''
                if i == 0 and j != 0:
                    kk = dp[i][j-1]
                elif j == 0 and i!=0:
                    kk =  dp[i-1][j]
                elif j == 0 and i==0:
                    kk =  inf
                else :
                    kk = max(dp[i-1][j],dp[i][j-1])
                if mini < kk :
                    dp[i][j] = mini
                else :
                    dp[i][j] = kk
        dp2 = [[0]*(n) for _ in range(m)]
        for i in range(m-1,-1,-1):
            for j in range(n-1,-1,-1):
                mini = inf
                temp = [[p[0]-(i),p[1]-(j)] for p in note]     
                for k in temp :
                    mini = min(mini,abs(k[0])+abs(k[1]))
                '''               
                if mini < dp[i-1][j] and mini < dp[i][j-1] :
                    dp[i][j] = mini
                elif mini > dp[i-1][j] and mini > dp[i][j-1] :
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1])
                else :
                    dp[i][j] = mini
                '''
                if i == m-1 and j != n-1:
                    kk = dp[i][j+1]
                elif j == n-1 and i!=m-1:
                    kk =  dp[i+1][j]
                elif j == n-1 and i==m-1:
                    kk =  inf
                else :
                    kk = max(dp[i+1][j],dp[i][j+1])
                if mini < kk :
                    dp[i][j] = mini
                else :
                    dp[i][j] = kk
        
        #return dp
        return max(dp[-1][-1],dp2[-1][-1])
                
      

题目




体会

不能用DP的原因:

因为不仅可以向右和向下走,还可以向上和向左,所以不能dp。

从来没接触过这道题的解法,也看不懂。

python 复制代码
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))

    def find(self, a):
        a = self.parent[a]
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a

    def merge(self, a, b):
        pa, pb = self.find(a), self.find(b)
        if pa == pb: return False
        self.parent[pb] = pa
        return True

class Solution:
    def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
        n = len(grid)
        
        dist = [[inf] * n for _ in range(n)]
        tmp = deque([(i, j) for i in range(n) for j in range(n) if grid[i][j]])
        for i, j in tmp:
            dist[i][j] = 0
        while tmp:
            i, j = tmp.popleft()
            for dx, dy in pairwise([-1, 0, 1, 0, -1]):
                if 0 <= i + dx < n and 0 <= j + dy < n and dist[i+dx][j+dy] == inf:
                    dist[i+dx][j+dy] = dist[i][j] + 1
                    tmp.append((i+dx, j+dy))
        
        l, r = 0, 2 * n - 2
        while l <= r:
            m = (l + r) // 2
            union = UnionFind(n * n)
            for i in range(n):
                for j in range(n):
                    if dist[i][j] >= m:
                        if i < n - 1 and dist[i+1][j] >= m: union.merge(i * n + j, (i + 1) * n + j)
                        if j < n - 1 and dist[i][j+1] >= m: union.merge(i * n + j, i * n + j + 1)
            if union.find(0) == union.find(n * n - 1): l = m + 1
            else: r = m - 1
        return r

我的代码

题3



体会

python 复制代码
class Solution:
    def findMaximumElegance(self, items: List[List[int]], k: int) -> int:
        items.sort(reverse=True)
        chosen = set()
        to_delete = []

        tot = 0
        for i in range(k):
            x, t = items[i]
            tot += x
            if t in chosen: heappush(to_delete, x)
            else: chosen.add(t)
        
        ans = tot + len(chosen) ** 2
        for i in range(k, len(items)):
            x, t = items[i]
            if t not in chosen and len(to_delete):
                chosen.add(t)
                tot -= heappop(to_delete)
                tot += x
                ans = max(ans, tot + len(chosen) ** 2)
        return ans

我的代码

相关推荐
好奇龙猫2 小时前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】
人工智能·算法
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01
人工智能·深度学习·神经网络·算法·机器学习·语言模型·数据挖掘
香菜大丸3 小时前
链表的归并排序
数据结构·算法·链表
jrrz08283 小时前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
oliveira-time3 小时前
golang学习2
算法
南宫生4 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步5 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝
Ni-Guvara5 小时前
函数对象笔记
c++·算法
泉崎5 小时前
11.7比赛总结
数据结构·算法
你好helloworld5 小时前
滑动窗口最大值
数据结构·算法·leetcode