当前位置:网站首页>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 :

边栏推荐
- Record: why is there no lightning 4 interface graphics card docking station and mobile hard disk?
- [hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
- ThreadLocal巨坑!内存泄露只是小儿科...
- Memory considerations under bug memory management
- Is it safe to open a securities account? Is there any danger
- C | analysis of malloc implementation
- GC is not used in D
- d检查类型是指针
- windows版MySQL软件的安装与卸载
- Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
猜你喜欢

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

Relevant knowledge of information entropy

33. Use rgbd camera for target detection and depth information output

使用 Performance 看看浏览器在做什么

免费的机器学习数据集网站(6300+数据集)

Input text to automatically generate images. It's fun!

Variable declaration of typescript

Calculate the distance between two points (2D, 3D)

布局管理器~登录界面的搭建实例

Network remote access using raspberry pie
随机推荐
Matlab programming related knowledge
Self created notes (unique in the whole network, continuously updated)
永远不要使用Redis过期监听实现定时任务!
Bug STL string
虫子 STL string 下 练习题
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
Common operation and Principle Exploration of stream
Gartner 2022 Top Strategic Technology Trends Report
Introduction to granular computing
Pycharm远程连接服务器来跑代码
ICML 2022 | limo: a new method for rapid generation of targeted molecules
Niuke challenge 53:c. strange magic array
Luogu p4513 xiaobaiguang Park
Bug memory management
Installation and uninstallation of MySQL software for windows
CloudCompare——泊松重建
DOS command
免费的机器学习数据集网站(6300+数据集)
Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
Svn commit error after deleting files locally
https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/