DP

一.什么是动态规划?


“递推”:   

Example 1: 斐波那契数列

1 1 2 3 5 8 13 21 ……

Example 2: 求最短路径

“本质”:——状态,状态转移方程,无后效性 - 局部最优全局最优

Example3: f[i][j]=min(f[i-1][j],f[i][j-1])+a[i][j]

无后效性:局部最优    不会导致     全局不是最优


二.常见题型


LEET CODE 300. 

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,

Given [10, 9, 2, 5, 3, 7, 101, 18],

The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

LEET CODE 265.

There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Note:

All costs are positive integers.

Follow up:

Could you solve it in O(nk) runtime?


01背包问题:

你有一个容量为M的背包。现在有N个物品摆在你的面前,每个物品有Ai的价值,占Bi的空间。如何用M的空间装走尽量多价值的物品。

FOLLOW UP:并且输出你选择了哪几个物品



LEET CODE 486

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.

Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.






300.f[i]=f[j]+1  (a[j]<=a[i]  && j<i)

265.f[i][j]=min(f[i-1][k]  ( k != j)  )+a[i][j]

486. f[i][j] = max (g[i][j]-f[i+1][j],g[i][j]-f[i][j-1])   

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,053评论 0 23
  • 英雄联盟职业比赛和路人排位区别是很大的,职业比赛里的强势英雄在路人局未必好用,而有些路人局胜率逆天的英雄,却不一定...
    萌虎游戏阅读 666评论 0 0
  • 这首诗正在写 背景音乐是正在弹奏的钢琴曲 很舒缓很流畅很安详 我还不知道音乐的曲名 是小乔在完成练习 中午时候 她...
    微风LG阅读 520评论 3 4
  • 去高铁站接人 本来我是拒绝的 但还是因为道德绑架还是去做 一早就知道 在那人潮挤挤中 我无法仅凭孤身一人去找到两个...
    丁FF阅读 329评论 0 2