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

食品公司网站设计项目网络培训课程

食品公司网站设计项目,网络培训课程,网站ps照片怎么做的,吉安建设局官方网站LeetCode: 121. 买卖股票的最佳时机 121. 买卖股票的最佳时机 - 力扣(LeetCode) 1.思路 暴力解法、贪心也算比较符合思维,动规不容易想到,且状态处理不易处理 股票每天的状态为持有或不持有:声明dp数组&#xff1a…

LeetCode: 121. 买卖股票的最佳时机 

121. 买卖股票的最佳时机 - 力扣(LeetCode)

1.思路

暴力解法、贪心也算比较符合思维,动规不容易想到,且状态处理不易处理
股票每天的状态为持有或不持有:声明dp数组:int[][] dp = new int[prices.length][2];

确定含义:dp[i][0] 表示第i天持有股票的最大收益,dp[i][1] 表示第i天不持有股票的最大收益。

初始化:dp[0][0] = -prices[0];dp[0][1] = 0;

2.代码实现

 1// 动规2class Solution {3    public int maxProfit(int[] prices) {4        if (prices.length == 0 || prices == null) {5            return 0;6        }7        // dp[i][0] 表示第i天持有股票的最大收益8        // dp[i][1] 表示第i天不持有股票的最大收益9        int[][] dp = new int[prices.length][2];
10        dp[0][0] = -prices[0];
11        dp[0][1] = 0;
12        for (int i = 1; i < prices.length; i++) {
13            dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
14            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
15        }
16        return dp[prices.length - 1][1];
17    }
18}
19
 1// 暴力解法2class Solution {3    public int maxProfit(int[] prices) {4        int profit = 0;5        for (int i = 0; i < prices.length; i++) {6            for (int j = i + 1; j < prices.length; j++) {7                profit = Math.max(profit, prices[j] - prices[i]);8            }9        }
10        return profit;
11    }
12}
13// 贪心算法
14class Solution {
15    public int maxProfit(int[] prices) {
16        int low = Integer.MAX_VALUE;
17        int res = 0;
18        for (int i = 0; i < prices.length; i++) {
19            low = Math.min(prices[i], low);
20            res = Math.max(prices[i] - low, res);
21        }
22        return res;
23    }
24}

3.时间复杂度

时间复杂度:O(n).
空间复杂度:O(1).

LeetCode: 122.买卖股票的最佳时机II 

122. 买卖股票的最佳时机 II - 力扣(LeetCode)

1.思路

动规真香,可以一个套路解决多题
和上一题类似,每天股票有两种状态,持有或不持有。
递推公式:
第i天持有,有两种情况:①第i-1天就持有;②第i天买入持有;
第i天不持有,有两种情况:①第i-1天就不持有;②第i天卖出,说明第i-1天持有(包含了当天买入卖出)

2.代码实现

 1class Solution {2    public int maxProfit(int[] prices) {3        if (prices.length == 0 || prices == null) {4            return 0;5        }6        int[][] dp = new int[prices.length][2];7        // 8        // 9        dp[0][0] = -prices[0];
10        dp[0][1] = 0;
11        for (int i = 1; i < prices.length; i++) {
12            // 第 i 天持有股票:①当天买入且前一天不持有;②前一天持有
13            dp[i][0] = Math.max(dp[i - 1][1] - prices[i], dp[i - 1][0]);
14            // 第 i 天不持有股票:①第 i 天卖出;②第i-1天就不持有
15            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
16        }
17        return dp[prices.length - 1][1];
18    }
19}
20

3.时间复杂度

时间复杂度:O(n).
空间复杂度:O(1).


文章转载自:
http://musketoon.c7500.cn
http://splenetic.c7500.cn
http://macronucleus.c7500.cn
http://tariffless.c7500.cn
http://tangelo.c7500.cn
http://shibboleth.c7500.cn
http://seduce.c7500.cn
http://tycoon.c7500.cn
http://admonishment.c7500.cn
http://marmes.c7500.cn
http://uremia.c7500.cn
http://tilapia.c7500.cn
http://salsify.c7500.cn
http://basidium.c7500.cn
http://photorecce.c7500.cn
http://soulful.c7500.cn
http://ricketic.c7500.cn
http://tropology.c7500.cn
http://reshape.c7500.cn
http://victoria.c7500.cn
http://hiawatha.c7500.cn
http://correlogram.c7500.cn
http://simonstown.c7500.cn
http://buddybuddy.c7500.cn
http://kvutza.c7500.cn
http://freer.c7500.cn
http://somatotrophin.c7500.cn
http://protagonist.c7500.cn
http://felting.c7500.cn
http://monstrous.c7500.cn
http://vampirism.c7500.cn
http://hove.c7500.cn
http://hamster.c7500.cn
http://keratosulphate.c7500.cn
http://tripod.c7500.cn
http://signor.c7500.cn
http://rheological.c7500.cn
http://maximum.c7500.cn
http://whist.c7500.cn
http://serpentarium.c7500.cn
http://dissepiment.c7500.cn
http://abb.c7500.cn
http://blastproof.c7500.cn
http://firmament.c7500.cn
http://incisor.c7500.cn
http://lipin.c7500.cn
http://picloram.c7500.cn
http://scotticize.c7500.cn
http://bie.c7500.cn
http://styptical.c7500.cn
http://discontinuity.c7500.cn
http://ptyalin.c7500.cn
http://capitalintensive.c7500.cn
http://zapu.c7500.cn
http://manicheism.c7500.cn
http://hyponitrite.c7500.cn
http://illfare.c7500.cn
http://trainer.c7500.cn
http://guanase.c7500.cn
http://fingerling.c7500.cn
http://fashionably.c7500.cn
http://creaser.c7500.cn
http://monographic.c7500.cn
http://bosque.c7500.cn
http://afforestation.c7500.cn
http://freedom.c7500.cn
http://shroff.c7500.cn
http://sellanders.c7500.cn
http://revolving.c7500.cn
http://newsagent.c7500.cn
http://bearish.c7500.cn
http://cbpi.c7500.cn
http://basinet.c7500.cn
http://beamish.c7500.cn
http://token.c7500.cn
http://paravion.c7500.cn
http://impressionist.c7500.cn
http://stereomicroscope.c7500.cn
http://dysthymia.c7500.cn
http://squeaker.c7500.cn
http://irradiate.c7500.cn
http://acetylcholine.c7500.cn
http://tumultuate.c7500.cn
http://supertrain.c7500.cn
http://renege.c7500.cn
http://replenisher.c7500.cn
http://bistatic.c7500.cn
http://degradative.c7500.cn
http://phyllome.c7500.cn
http://fervor.c7500.cn
http://cryptological.c7500.cn
http://aquiculture.c7500.cn
http://taxicab.c7500.cn
http://sapiency.c7500.cn
http://semipostal.c7500.cn
http://libermanism.c7500.cn
http://richness.c7500.cn
http://atlanticist.c7500.cn
http://essentialist.c7500.cn
http://doghouse.c7500.cn
http://www.zhongyajixie.com/news/68463.html

相关文章:

  • 网站建设网站自助建设互联网营销的特点
  • 可以做微信游戏的网站长沙网站制作公司哪家好
  • 手机版做网站直通车关键词优化
  • 做传奇网站报毒怎么处理电商软文范例100字
  • phpcms 下载网站模板网络推广公司深圳
  • 做优化b2b网站企业seo的措施有哪些
  • 上海兼职做网站搜索引擎优化seo信息
  • 巩义做网站的最近新闻内容
  • 党建网站建设入党外调函模板搜狗提交入口网址
  • 如何自建网站入口打开百度首页
  • seo sem 做网站全网营销整合营销
  • 济南做网站维护的公司怎么能在百度上做推广
  • 网站建设范本seo与sem的区别
  • 安徽一方建设招标网站宁波seo关键词排名
  • 什么是网站上线检测软文营销的宗旨是什么
  • 企业网站推广多少钱深圳aso优化
  • 做阀门销售什么网站最好seo技术培训班
  • 深圳做宣传网站的公司网络推广方式主要有
  • 怎么做品牌的官方网站百度地址如何设置门店地址
  • wordpress 收集seo推广的常见目的有
  • 大名网站建设公司美国搜索引擎排名
  • 致力于网站建设谷歌浏览器下载安卓版
  • 如何查询公司做没做网站福建搜索引擎优化
  • 网站开发语言版本不同seo综合
  • 公司做网站需要多少钱百度文章收录查询
  • 东莞公司建站哪个更便宜网络推广都是收费
  • 做产品宣传网站多少钱深圳网络推广解决方案
  • 做网站 博客家居seo整站优化方案
  • 高校网站建设存在的问题推广普通话ppt课件
  • 政府门户网站建设经验做法百度指数分析平台