代码随想录算法训练营day48 day50| 42. 接雨水 84.柱状图中的最大的矩形 卡码网98. 所有可达路径

学习资料:https://www.programmercarl.com/0042.接雨水.html

单调栈经典面试题 接雨水 找该元素左边第一个高点和右边第一个高点
图论 邻接矩阵、邻接表 深度优先算法、广度优先算法
学习ACM模式

学习记录:
42.接雨水(递增单调栈;选择左边和右边高点的较小值)

点击查看代码
class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        left_max = [0 for _ in height]
        right_max = [0 for _ in height]
        water = [0 for _ in height]
        
        for i in range(len(height)):
            if i-1 >= 0:
                left_max[i]=max(left_max[i-1], height[i])
            else:
                left_max[i]=height[i]

        for i in range(len(height)-1, -1, -1):
            if i<len(height)-1:
                right_max[i]=max(right_max[i+1], height[i])
            else:
                right_max[i]=height[i]
        
        for i in range(len(height)):
            tmp=min(left_max[i], right_max[i]) - height[i]
            if tmp>0:
                water[i]=tmp
        
        return sum(water)

84.柱状图中的最大的矩形(在数组首尾都插入0,避免直接单调递减;左边和右边第一个矮值)

点击查看代码
class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        # 还是构造一个单调栈,不过是单调递减的
        stack = [0]
        result = 0

        # 如果数组本身就是单调递减的,那么本算法可能找不到值,为此可以在数组前后都插入0
        heights.insert(0, 0)
        heights.append(0)

        for i in range(1, len(heights)):
            if heights[i] > heights[stack[-1]]:
                stack.append(i)
            elif heights[i] == heights[stack[-1]]:
                stack.pop()
                stack.append(i)
            else:
                while stack and heights[i] < heights[stack[-1]]:
                    mid_index = stack[-1]
                    stack.pop()
                    if stack:
                        right_index = i
                        left_index = stack[-1]
                        
                        target_wide = right_index-left_index-1
                        target_area = heights[mid_index] * target_wide
                        result = max(result, target_area)
                stack.append(i)
        return result
        

卡码网98.所有可达路径(图论,我用的邻接矩阵;深度优先搜索;递推+回溯)

点击查看代码
def dfs(graph, x, n, path, result):
    if x == n:                         # 终止条件
        result.append(path.copy())
        return
    for i in range(1, n+1):
        if graph[x][i] == 1:                # 代表x -> i
            path.append(i)
            dfs(graph, i, n, path, result)     # 递归
            path.pop()                         # 回溯

def main():
    """邻接矩阵法"""
    n, m = map(int, input().split())
    # 构建一个矩阵
    graph = [[0]*(n+1) for _ in range(n+1)]
    
    for _ in range(m):
        s, t = map(int, input().split())
        graph[s][t] = 1   # 给矩阵赋值,代表两点连成一条线,s指向t
        
    result = []
    # 递归
    dfs(graph, 1, n, [1], result)
    
    if not result:
        print(-1)
    else:
        for path in result:
            print(' '.join(map(str, path)))

if __name__ == "__main__":
    main()
    
    

PS:降温了,一键入冬的节奏,或者秋高气爽的感觉
看到熊猫上树了好可爱,到图论篇了,没有视频有点难学呀,对于我这种废脑子难
今天收到一个offer,虽然不知道具体情况,继续加油~ 加油学习~ 大胆冲!
累,今天先感受一下邻接矩阵

posted @ 2024-11-18 19:58  Tristan241001  阅读(62)  评论(0)    收藏  举报