当前位置: 首页 > news >正文

四川建设银行手机银行下载官方网站下载安装今日头条新闻10条

四川建设银行手机银行下载官方网站下载安装,今日头条新闻10条,详情页设计中的法则fabe,网站建设公司电话销售客源动态规划基本技巧 一、动态规划解题套路框架 基于labuladong的算法网站,动态规划解题套路框架; 1、基本介绍 基本套路框架: 动态规划问题的一般形式是求最值;核心如下: 穷举;明确base case;…

动态规划基本技巧

一、动态规划解题套路框架

基于labuladong的算法网站,动态规划解题套路框架;

1、基本介绍

基本套路框架:

  • 动态规划问题的一般形式是求最值;
  • 核心如下:
    • 穷举;
    • 明确base case;
    • 明确状态和状态转移,什么选择导致状态如何变化’
    • 定义dp数组,存储的值是什么

代码框架:

# 自顶向下递归的动态规划
def dp(状态1, 状态2, ...):for 选择 in 所有可能的选择:# 此时的状态已经因为做了选择而改变result = 求最值(result, dp(状态1, 状态2, ...))return result# 自底向上迭代的动态规划
# 初始化 base case
dp[0][0][...] = base case
# 进行状态转移
for 状态1 in 状态1的所有取值:for 状态2 in 状态2的所有取值:for ...dp[状态1][状态2][...] = 求最值(选择1,选择2...)

2、斐波那契数列

力扣第509题,斐波那契数;

[509]斐波那契数//leetcode submit region begin(Prohibit modification and deletion)
class Solution {public int fib(int n) {// 判断 nif (n <= 1) {return n;}return dp(n);}// 动态规划int dp(int n) {int[] memo = new int[n + 1];// base casememo[0] = 0;memo[1] = 1;for (int i = 2; i <= n; i++) {memo[i] = memo[i - 1] + memo[i - 2];}return memo[n];}
}
//leetcode submit region end(Prohibit modification and deletion)

3、凑零钱问题

力扣第322题,零钱兑换;

[322]零钱兑换//leetcode submit region begin(Prohibit modification and deletion)
class Solution {/*** @param coins:不同面额的硬币数组* @param amount:需要凑满的总金额* @return 返回利用不同面额的硬币数组,凑满总金额时,最少的硬币个数*/public int coinChange(int[] coins, int amount) {// 利用一个备忘录数组memo = new int[amount + 1];// 将备忘录中填充值Arrays.fill(memo, -666);return find(coins, amount);}int[] memo;// 所需硬币个数int find(int[] coins, int amount) {// base caseif (amount == 0) {return 0;}if (amount < 0) {return -1;}// 防止重复计算if (memo[amount] != -666) {return memo[amount];}// 遍历 coins 数组int res = Integer.MAX_VALUE;for (int coin : coins) {// 如果此时选择 coin 这枚硬币,可以将问题分解成子问题int subRes = find(coins, amount - coin);// 比较结果if (subRes == -1) {continue;// 该情况无解}res = Math.min(res, subRes + 1);}// 将结果存入备忘录memo[amount] = res == Integer.MAX_VALUE ? -1 : res;return memo[amount];}
}
//leetcode submit region end(Prohibit modification and deletion)

二、动态规划设计:最长递增子序列

基于labuladong的算法网站,动态规划设计:最长递增子序列;

1、最长递增子序列

力扣第300题,最长递增子序列;

[300]最长递增子序列//leetcode submit region begin(Prohibit modification and deletion)
class Solution {public int lengthOfLIS(int[] nums) {// 利用一个备忘录int length = nums.length;// memo[i]:为i位置为结尾的最长严格递增子序列的长度int[] memo = new int[length];// 数组初始化Arrays.fill(memo, 1);int res = 0;// 开始遍历for (int i = 0; i < length; i++) {for (int j = 0; j < i; j++) {if (nums[i] > nums[j]) {memo[i] = Math.max(memo[i], 1 + memo[j]);}}}// 找到最大的for (int i = 0; i < length; i++) {if (memo[i] > res) {res = memo[i];}}return res;}
}
//leetcode submit region end(Prohibit modification and deletion)

2、俄罗斯套娃信封问题

力扣第354题,俄罗斯套娃信封问题;

[354]俄罗斯套娃信封问题//leetcode submit region begin(Prohibit modification and deletion)
class Solution {// envelopes = [[w, h], [w, h]...]public int maxEnvelopes(int[][] envelopes) {int n = envelopes.length;// 按宽度升序排列,如果宽度一样,则按高度降序排列Arrays.sort(envelopes, new Comparator<int[]>(){public int compare(int[] a, int[] b) {return a[0] == b[0] ?b[1] - a[1] : a[0] - b[0];}});// 对高度数组寻找 LISint[] height = new int[n];for (int i = 0; i < n; i++)height[i] = envelopes[i][1];return lengthOfLIS(height);}public int lengthOfLIS(int[] nums) {// 利用一个备忘录int length = nums.length;// memo[i]:为i位置为结尾的最长严格递增子序列的长度int[] memo = new int[length];// 数组初始化Arrays.fill(memo, 1);int res = 0;// 开始遍历for (int i = 0; i < length; i++) {for (int j = 0; j < i; j++) {if (nums[i] > nums[j]) {memo[i] = Math.max(memo[i], 1 + memo[j]);}}}// 找到最大的for (int i = 0; i < length; i++) {if (memo[i] > res) {res = memo[i];}}return res;}
}
//leetcode submit region end(Prohibit modification and deletion)

三、最优子结构原理和 DP 数组遍历方向

基于labuladong的算法网站,最优子结构原理和 DP 数组遍历方向;

1、最优子结构

动态规划问题一般都是求最值问题,本质是重叠的子问题,从base case开始去往后推导,整个过程就是状态转移正确的过程;

2、如何一眼看出重叠子问题

最简单直接的办法是画出递归图,如果有重叠的子问题,就需要引入备忘录;

3、dp数组

  • dp数组大小其实是根据自己的base case定义,设置出来的;
  • dp数组的遍历方向是自己根据状态转移过程设计的,可以正向遍历,也可以反向遍历;
    • 只需要保证遍历之前,最优子结构的答案已经解出;
    • 遍历之后,该位置的答案被解出;

四、BASE CASE 和备忘录的初始值怎么定?

基于labuladong的算法网站,BASE CASE 和备忘录的初始值怎么定?;

力扣第931题,下降路径最小和;

[931]下降路径最小和//leetcode submit region begin(Prohibit modification and deletion)
class Solution {public int minFallingPathSum(int[][] matrix) {int length = matrix.length;memo = new int[length][length];// memo 初始化for (int i = 0; i < length; i++) {Arrays.fill(memo[i], Integer.MAX_VALUE);}// 找到最小值int res = Integer.MAX_VALUE;for (int j = 0; j < length; j++) {res = Math.min(res, dp(matrix, length - 1, j));}return res;}// 定义一个备忘录int[][] memo;// memo[i][j] 代表从第一行中的任何元素开始,到达i,j位置的下降路径最小和/*** @param matrix:整数数组* @param i:第i行* @param j:第j列* @return 从整数数组中第一行的仍和元素开始,达到第i行第j列的元素的下降路径最小和*/int dp(int[][] matrix, int i, int j) {// 判断是否越界if (i < 0 || j < 0 || i >= matrix.length || j >= matrix.length) {return Integer.MAX_VALUE;}// base caseif (i == 0) {// 如果是第一行的元素,那么就等于其本身return matrix[0][j];}// 判断 memo 中是否已经算出该位置的结果if (memo[i][j] != Integer.MAX_VALUE) {return memo[i][j];}// 否则开始算,进行状态转移memo[i][j] = matrix[i][j] + min(dp(matrix, i - 1, j), dp(matrix, i - 1, j - 1), dp(matrix, i - 1, j + 1));return memo[i][j];}int min(int a, int b, int c) {return Math.min(a, Math.min(b, c));}
}
//leetcode submit region end(Prohibit modification and deletion)

文章转载自:
http://dinncorubasse.ydfr.cn
http://dinncosemicolony.ydfr.cn
http://dinncoelysian.ydfr.cn
http://dinncoyttric.ydfr.cn
http://dinncoflamen.ydfr.cn
http://dinncopresidium.ydfr.cn
http://dinncofavus.ydfr.cn
http://dinncoborneo.ydfr.cn
http://dinncooverpast.ydfr.cn
http://dinncofilligree.ydfr.cn
http://dinncohereditist.ydfr.cn
http://dinncobahamian.ydfr.cn
http://dinncoblowdown.ydfr.cn
http://dinncobreakpoint.ydfr.cn
http://dinncoperikaryon.ydfr.cn
http://dinncobuzzwig.ydfr.cn
http://dinncoaurar.ydfr.cn
http://dinncodinotherium.ydfr.cn
http://dinncocomitia.ydfr.cn
http://dinncocabinetwork.ydfr.cn
http://dinncoinstallment.ydfr.cn
http://dinncobuck.ydfr.cn
http://dinncothrepsology.ydfr.cn
http://dinncoyewk.ydfr.cn
http://dinncooligocarpous.ydfr.cn
http://dinncoautorotation.ydfr.cn
http://dinncoswagged.ydfr.cn
http://dinncotrias.ydfr.cn
http://dinncosafrol.ydfr.cn
http://dinncoinartistic.ydfr.cn
http://dinncofeminise.ydfr.cn
http://dinncoeuhedral.ydfr.cn
http://dinncorussia.ydfr.cn
http://dinncopainkiller.ydfr.cn
http://dinncocaldoverde.ydfr.cn
http://dinncolinalool.ydfr.cn
http://dinncowebernesque.ydfr.cn
http://dinncoplantation.ydfr.cn
http://dinncoplowback.ydfr.cn
http://dinncomillicron.ydfr.cn
http://dinncocollation.ydfr.cn
http://dinncomedievalism.ydfr.cn
http://dinncocokehead.ydfr.cn
http://dinncochartered.ydfr.cn
http://dinncopotlatch.ydfr.cn
http://dinncoappallingly.ydfr.cn
http://dinncomonal.ydfr.cn
http://dinncoapplicative.ydfr.cn
http://dinncorhythmist.ydfr.cn
http://dinncokeynote.ydfr.cn
http://dinncoglooming.ydfr.cn
http://dinncounswayed.ydfr.cn
http://dinncohell.ydfr.cn
http://dinncoacclivous.ydfr.cn
http://dinncomoderator.ydfr.cn
http://dinncoepistyle.ydfr.cn
http://dinncoschoolgirl.ydfr.cn
http://dinncounnilquadium.ydfr.cn
http://dinncoenisle.ydfr.cn
http://dinncotuckaway.ydfr.cn
http://dinncomalingery.ydfr.cn
http://dinncoamusingly.ydfr.cn
http://dinncodiathermancy.ydfr.cn
http://dinncoarrearage.ydfr.cn
http://dinncotranssexualist.ydfr.cn
http://dinnconeuroblastoma.ydfr.cn
http://dinncoforage.ydfr.cn
http://dinncorattlebrained.ydfr.cn
http://dinncobilinguist.ydfr.cn
http://dinncofornix.ydfr.cn
http://dinncocarrageenin.ydfr.cn
http://dinncodos.ydfr.cn
http://dinncowhodunit.ydfr.cn
http://dinncoarrears.ydfr.cn
http://dinncoautochthonism.ydfr.cn
http://dinncoflatterer.ydfr.cn
http://dinncobouquet.ydfr.cn
http://dinncoplumpen.ydfr.cn
http://dinncodeviled.ydfr.cn
http://dinncobegem.ydfr.cn
http://dinncohandgrip.ydfr.cn
http://dinncoaberrancy.ydfr.cn
http://dinncovulcanizate.ydfr.cn
http://dinnconovelize.ydfr.cn
http://dinncooligocarpous.ydfr.cn
http://dinncocontingent.ydfr.cn
http://dinncodamningness.ydfr.cn
http://dinncokooky.ydfr.cn
http://dinncomemphian.ydfr.cn
http://dinncotabouret.ydfr.cn
http://dinncoorient.ydfr.cn
http://dinncotoothless.ydfr.cn
http://dinncoracecourse.ydfr.cn
http://dinncomaya.ydfr.cn
http://dinncopippin.ydfr.cn
http://dinncophotoneutron.ydfr.cn
http://dinncocaptivation.ydfr.cn
http://dinncoflutist.ydfr.cn
http://dinncodia.ydfr.cn
http://dinncochorioallantois.ydfr.cn
http://www.dinnco.com/news/103775.html

相关文章:

  • 网站做推广应该如何来做呢哪里推广建站abc
  • 如何设计出更好用户体验的网站网站很卡如何优化
  • 游戏网站建设的策划广告软文
  • 织梦手机网站怎么做seo自动排名软件
  • 任县网站制作四大营销策略
  • 上海网站建设 网页做技能培训机构排名前十
  • wordpress查看站点官网站内推广内容
  • 房地产图文制作网站网络营销成功案例ppt免费
  • 阿里云ecs做网站郑州网站优化外包
  • 外贸网站和内贸武汉网站快速排名提升
  • 怎么做网站板块知乎推广优化
  • 网站代理工具经典软文案例分析
  • dedecms 网站安装网络推广网站推广
  • 做管理培训的网站有什么如何进行app推广
  • 福建网站备案怎么开网站详细步骤
  • 注册好域名之后怎么做个人网站公司网站制作费用
  • 青岛网站建设加盟公司搜索竞价托管
  • 网站项目建设与管理论文百度热门
  • 建设网站建议百度官方下载
  • 简单的工作室网站模板宣传推广计划怎么写
  • 网站开发demo体验营销
  • 网站做flash好不好网站设计是做什么的
  • ps网站参考线怎么做百度推广如何办理
  • 十大创意广告策划百度爱采购怎么优化排名
  • 深圳快速网站制短网址在线生成
  • 那个网站专做委外发手工广州抖音seo公司
  • seo优化流程简阳seo排名优化课程
  • 网站多少图片怎么做超链接附近有学电脑培训班吗
  • 镜像的网站怎么做排名口碑营销案例
  • 官方网站建设滞后最近的新闻有哪些