Leetcode_c++: Triangle (120)
2016-06-10 23:02
405 查看
题目
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).
算法
O(N^2)设状态为f(i,j),表示从从位置(i,j)出发,路径的最小和,则状态转移方程为
f(i,j)=min{f(i,j+1),f(i+1,j+1)}+(i,j)
//原数组上修改
// LeetCode, Triangle // 时间复杂度O(n^2),空间复杂度O(1) class Solution { public: int minimumTotal (vector<vector<int>>& triangle) { for (int i = triangle.size() - 2; i >= 0; --i) for (int j = 0; j < i + 1; ++j) triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]); return triangle [0][0]; } };
相关文章推荐
- c语言:递归求最大公约数
- VC++6.0玩OpenGL(一)
- Leetcode_c++: Pascal's TriangleII (119)
- C++操作符函数
- 找出字符串中的所有数字字符串(C/C++实现)
- Leetcode_c++: Pascal's Triangle(118)
- C++中的const用法
- Leetcode_c++:Construct Binary Tree from Inorder and Postorder Traversal (106)
- SCPPO(三):禅道的使用—项目管理员、开发等人员
- 指针和引用
- win7旗舰版安装VC++6.0番茄插件和行号显示
- C++ 删除指定路径文件夹
- C语言选择排序详解及其实现
- C++解压zip文件
- C++11中的std::bind
- SCPPO(二):禅道的使用—管理员
- 【C语言】15-预处理指令1-宏定义
- C++中常用的STL 函数
- C++11多线程之yield
- 在C/C++代码中使用SSE等指令集的指令(1)介绍