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.

题解

这又是一道比较简单的动态规划的题目了。设dp(i, j)为经过第i层第j点到达顶点的代价,则dp(i, j) = triangle(i, j) + min(dp(i + 1, j), dp(i+1, j+1))。由于空间复杂度要求是O(n),我们之间在原数组上操作,给出一个空间复杂度为O(1)的算法。

代码

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        for (int i = triangle.size() - 2; i >= 0; i--) {
            List<Integer> list = triangle.get(i);
            List<Integer> next = triangle.get(i + 1);
            for (int j = 0; j <= i; j++) {
                list.set(j, list.get(j) + Math.min(next.get(j), next.get(j + 1)));
            }
        }
        return triangle.get(0).get(0);
    }

    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作者hyb1996,源码Auto.js,一个主要由无障碍服务实现的不需要Root权限的类似按键精灵的自动操作软件,可以实现自动点击、滑动、输入文字、打开应用等。 同时有Sublime Text 插件可提供基础的在桌面开发的功能。 下载地址:酷安 Alpha版本:Releases 特性 简单易用的自动操作函数 悬浮窗录制和运行 更专业&强大的选择器API,提供对屏幕上的控件的寻找、遍历、获取信息、操作等。类似于Google的UI测试框架UiAutomator,您也可以把他当做移动版UI测试框架使用 采用JavaScript为脚本语言,并支持代码补全、变量重命名、代码格式化、查找替换等功能,可以作为一个JavaScript IDE使用 支持使用e4x编写界面,并可以将JavaScript打包为apk文件,您可以用它来开发小工具应用 支持使用Root权限以提供更强大的屏幕点击、滑动、录制功能和运行shell命令。录制录制可产生js文件或二进制文件,录制动作的回放比较流畅 提供截取屏幕、保存截图、图片找色等函数,可进行简单的游戏脚本制作;未来将加入找图功能 可作为Tasker插件使用,结合Tasker可胜任日常工作流 带有界面分析工具,类似Android Studio的LayoutInspector,可以分析界面层次和范围、获取界面上的控件信息 与脚本精灵、按键精灵等软件的区别是: Auto.js主要以自动化、工作流为目标,更多地是方便日常生活工作,例如启动游戏时自动屏蔽通知、一键与特定联系人微信视频(知乎上出现过该问题,老人难以进行复杂的操作和子女进行微信视频)等 Auto.js兼容性更好。以坐标为基础的按键精灵、脚本精灵很容易出现分辨率问题,而以控件为基础的Auto.js则没有这个问题 Auto.js执行大部分任务不需要root权限。只有需要精确坐标点击、滑动的相关函数才需要root权限 尽管如此,Auto.js的大部分用户仍然是用来点赞、签到、刷游戏的:)
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、付费专栏及课程。

余额充值