当前位置:网站首页>Sword finger offer 10 Ⅰ 10Ⅱ. 63 dynamic planning (simple)
Sword finger offer 10 Ⅰ 10Ⅱ. 63 dynamic planning (simple)
2022-06-26 14:13:00 【hedgehog:】
10.Ⅰ
subject :


idea : F(N) = F(N - 1) + F(N - 2), Recursive time complexity is too high , Use dynamic programming , Move ab.
Code :
class Solution {
public int fib(int n) {
int a = 0, b = 1, sum;
for(int i = 0; i < n; i++){
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return a;
}
}result :

10.Ⅱ
subject :


idea :( Steal a picture )
still fib problem , It's just f(0)=f(1)=1.
Code :
class Solution {
public int numWays(int n) {
int a = 1, b = 1, sum;
for(int i = 0; i < n; i++){
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return a;
}
}result :

63.
subject :


idea :( Steal a picture )

Remember to buy at the cheapest time !
Code :
// violence Time complexity is too high
class Solution {
public int maxProfit(int[] prices) {
int max=0;
for(int i=prices.length-1;i>=0;i--){
for(int j=i-1;j>=0;j--){
int tmp=prices[i]-prices[j];
if(tmp>max)
max=tmp;
}
}
return max;
}
}
//DP. Buy at the lowest price
class Solution {
public int maxProfit(int[] prices) {
int min = Integer.MAX_VALUE;
int max=0;
for(int i=0;i<prices.length;i++){
if(prices[i]<min)
min=prices[i];
else if(prices[i]-min>max)
max=prices[i]-min;
}
return max;
}
}result :

边栏推荐
- Nexys A7开发板资源使用技巧
- 虫子 运算符重载的一个好玩的
- Matlab programming related knowledge
- Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
- Freefilesync folder comparison and synchronization software
- GC is not used in D
- Stream常用操作以及原理探索
- Reprint - easy to use wechat applet UI component library
- Difference between classification and regression
- Hard (magnetic) disk (I)
猜你喜欢

Comparison of disk partition modes (MBR and GPT)

array

windows版MySQL软件的安装与卸载

9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》

FreeFileSync 文件夹比较与同步软件

Gartner 2022年顶级战略技术趋势报告
![[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system](/img/03/a1885e4740bbfdbdee2446e3dd81d0.png)
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system

Wechat applet -picker component is repackaged and the disabled attribute is added -- below

Codeforces Global Round 21A~D

Basic type of typescript
随机推荐
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
A solution to the problem that the display of newff function in neural network cannot be converted from double to struct
免费的机器学习数据集网站(6300+数据集)
Bucket of P (segment tree + linear basis)
Assert and constd13
Go language - pipeline channel
虫子 类和对象 上
Global variable vs local variable
Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient
Installation and uninstallation of MySQL software for windows
I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
Network remote access using raspberry pie
mysql配置提高数据插入效率
Exercise set 1
CF676C Vasya and String
ICML 2022 | LIMO: 一种快速生成靶向分子的新方法
Postman自动化接口测试
Solutions to the failure of last child and first child styles of wechat applet
爱可可AI前沿推介(6.26)
Zero basics of C language lesson 7: break & continue
https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/