当前位置:网站首页>Dynamic programming - 509. Fibonacci number
Dynamic programming - 509. Fibonacci number
2022-07-28 03:44:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
Fibonacci number ( Usually use F(n) Express ) The sequence formed is called Fibonacci sequence . The sequence is composed of 0 and 1 Start , Each of the following numbers is the sum of the first two numbers . That is to say :
F(0) = 0,F(1) = 1
F(n) = F(n - 1) + F(n - 2), among n > 1
Given n , Please calculate F(n) .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/fibonacci-number
2 Title Example
Example 1:
Input :n = 2
Output :1
explain :F(2) = F(1) + F(0) = 1 + 0 = 1
Example 2:
Input :n = 3
Output :2
explain :F(3) = F(2) + F(1) = 1 + 1 = 2
Example 3:
Input :n = 4
Output :3
explain :F(4) = F(3) + F(2) = 2 + 1 = 3
3 Topic tips
0 <= n <= 30
4 Ideas
The boundary condition of Fibonacci number is F(0)=0 and F(1)=1. When n >1 when , Every time — The sum of terms is equal to the sum of the first two terms , Therefore, there is the following recurrence relationship :
F(n)= F(n- 1)+F(n -2)
Because the Fibonacci number has a recursive relationship , Therefore, dynamic programming can be used to solve . The state transition equation of dynamic programming is the above recursive relationship , The boundary condition is F(0) and F(1).
According to the state transition equation and boundary conditions , We can get that the time complexity and space complexity are O(n) The implementation of the . because F(nz) And the only F(n—1) And F(n ―2) of , So you can use 「 The idea of scrolling arrays 」 Optimize the space complexity to O(1). This implementation is given in the following code .
Complexity analysis
Time complexity :O(n).·
Spatial complexity :O(1).
Method 2 : Matrix fast power
Method — The time complexity of is o(n). Using the fast power of matrix can reduce the time complexity .
First, we can construct such a recursive relationship :
So as long as we can calculate the matrix quickly M Of n The next power , You can get F(n) Value . If you get it directly Mn, The time complexity is O(n), Matrix multiplication can be defined , Then use the fast power algorithm to speed up here Mn The solution of .
5 My answer
class Solution {
public int fib(int n) {
if (n < 2) {
return n;
}
int p = 0, q = 0, r = 1;
for (int i = 2; i <= n; ++i) {
p = q;
q = r;
r = p + q;
}
return r;
}
}
Method 2 :
class Solution {
public int fib(int n) {
if (n < 2) {
return n;
}
int[][] q = {
{
1, 1}, {
1, 0}};
int[][] res = pow(q, n - 1);
return res[0][0];
}
public int[][] pow(int[][] a, int n) {
int[][] ret = {
{
1, 0}, {
0, 1}};
while (n > 0) {
if ((n & 1) == 1) {
ret = multiply(ret, a);
}
n >>= 1;
a = multiply(a, a);
}
return ret;
}
public int[][] multiply(int[][] a, int[][] b) {
int[][] c = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j];
}
}
return c;
}
}
边栏推荐
- Light year admin background management system template
- LabVIEW loads and uses custom symbols in tree control projects
- 【P4】解决本地文件修改与库文件间的冲突问题
- Tensorboard usage record
- 构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!
- 数据挖掘-02
- Screenshot of deepstream detection results
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-6 personal solutions
- Billions of asset addresses are blacklisted? How to use the tether address freezing function?
- Leetcode58. 最后一个单词的长度
猜你喜欢

ES6 from getting started to mastering 09: symbol type

Tungsten Fabric SDN — BGP as a Service

What is tor? What is the use of tor browser update?

Responsive high-end website template source code Gallery material resource download platform source code

Build an "industrial brain" and improve the park's operation, management and service capabilities with "digitalization"!

8000 word explanation of OBSA principle and application practice

Vertical align align the elements in the row are vertically centered

Ch340 RTS DTR pin programming drives OLED

Integrate SSM to realize search of addition, deletion, modification and query
![[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix](/img/6c/b5fd3350886fd74557439f5361e7f8.png)
[force deduction] 1337. Row K with the weakest combat effectiveness in the matrix
随机推荐
Tungsten Fabric SDN — BGP as a Service
玩一玩WolframAlpha计算知识引擎
Interface automation test, complete introduction
【OPENVX】对象基本使用之vx_distribution
[P4] check the differences between the two historical versions of the library file
收藏|0 基础开源数据可视化平台 FlyFish 大屏开发指南
deepstream 检测结果截图
《剑指offer》| 刷题小记
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-6 personal solutions
Data rich Computing: m.2 meets AI at the edge
单调栈——42. 接雨水——面大厂必须会的困难题
Mysql基础篇(创建、管理、增删改表)
动态规划——1049. 最后一块石头的重量 II
Screenshot of deepstream detection results
How to uninstall clean ZABBIX service? (super detailed)
ES6 从入门到精通 # 09:Symbol 类型
一篇文章掌握Postgresql中对于日期类数据的计算和处理
ASEMI整流桥GBPC3510在直流有刷电机中的妙用
【论文笔记】基于深度学习的移动机器人自主导航实验平台
贪心——55. 跳跃游戏