120. Triangle

120. Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

点击打开原题链接     

点击打开分析链接

分析:三角形看起来类似树形结构,假设x和y是k的两个子节点,一旦从底到x或者y的pathsum确定了,那么只要在x和y中取最小值,再加上k就是当前的pathsum。它是具有最优子结构性质的。这里从上到下的动态规划和从下到上的动态规划是不同的,如果top-bottom,你至少需要和这个三角形同样大小的结构来存储每个点的pathsum,空间复杂度是O(n^2)。如果是采用从下往上,我们有minpath[i][k]=min(minpath[i+1][k],minpath[i+1][k+1])+triangle[i][k],当你完全获取了第i层的信息后,第i+1层的值就不会再用到,因为计算第i-1层时只需用到第i层。所以,可以用O(n)的空间来存储,而不是O(n^2),即minpath[k]=min(minpath[k],minpath[k+1])+triangle[i][k].

第一种:top--to--bottom

class Solution {
public:
    int minimumTotal(vector
   
   
    
    
     
     >& triangle) {
        int n=triangle.size();
        if(n==0)return 0;
        vector
     
     
      
      
       
       > dp(n);
        for(int i=0;i
       
       
         =0) dp[i][j]=min(dp[i-1][j-1],dp[i][j]); if(j==i) dp[i][j]=dp[i-1][j-1]; dp[i][j]+=triangle[i][j]; } int r=dp[n-1][0]; for(int i=0;i 
         
       
      
      
     
     
    
    
   
   
空间:O(n^2)

第二种:bottom--to--up

class Solution {
public:
    int minimumTotal(vector
    
    
     
     
      
      >& triangle) {
        int n=triangle.size();
        if(n==0)return 0;
        vector
      
      
       
        dp(n);
        for(int i=0;i
       
       
        
        =0;layer--)
        for(int i=0;i
        
        
       
       
      
      
     
     
    
    
空间:O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Serpinski's Triangle and Carpet are two famous fractal patterns named after the Polish mathematician Wacław Sierpiński. These patterns are created through a recursive process of dividing shapes into smaller copies of themselves. 1. Serpinski's Triangle: Serpinski's Triangle is a fractal pattern that starts with an equilateral triangle. In each iteration, the triangle is divided into four smaller triangles by connecting the midpoints of its sides. The central triangle is then removed, and the process is repeated for the remaining three triangles. This recursive division continues indefinitely, creating a pattern of smaller triangles within the original triangle. 2. Serpinski's Carpet: Serpinski's Carpet is a fractal pattern that starts with a square. In each iteration, the square is divided into nine smaller squares by removing the central square and dividing the remaining eight squares into nine equal-sized squares. The process is then repeated for each of the remaining eight squares. This recursive division continues indefinitely, creating a pattern of smaller squares within the original square. Both Serpinski's Triangle and Carpet exhibit self-similarity, meaning that they contain smaller copies of themselves at different scales. These fractal patterns have been widely studied and appreciated for their intricate and visually appealing structures. If you want to generate Serpinski's Triangle or Carpet using Python, you can use recursion and graphical libraries like Turtle or Matplotlib to draw the patterns. Here's an example code snippet for generating Serpinski's Triangle using Turtle: ```python import turtle def draw_triangle(length, depth): if depth == 0: for _ in range(3): turtle.forward(length) turtle.left(120) else: draw_triangle(length / 2, depth - 1) turtle.forward(length / 2) draw_triangle(length / 2, depth - 1) turtle.backward(length / 2) turtle.left(60) turtle.forward(length / 2) turtle.right(60) draw_triangle(length / 2, depth - 1) turtle.left(60) turtle.backward(length / 2) turtle.right(60) # Set up the turtle turtle.speed(0) turtle.penup() turtle.goto(-200, -200) turtle.pendown() # Draw Serpinski's Triangle draw_triangle(400, 4) # Hide the turtle turtle.hideturtle() # Keep the window open turtle.done() ``` This code will generate Serpinski's Triangle with a depth of 4. You can adjust the depth parameter to control the level of detail in the pattern.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值