当前位置:网站首页>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;
}
}
边栏推荐
- LeetCode 0140. 单词拆分 II
- 做自动化测试,你后悔了吗?
- [openvx] VX for basic use of objects_ lut
- Responsive high-end website template source code Gallery material resource download platform source code
- Push chart written by helm to harbor warehouse
- 贪心——53. 最大子数组和
- Xctf attack and defense world web master advanced area unserialize3
- Data rich Computing: m.2 meets AI at the edge
- Interface automation test, complete introduction
- Mouse operation and response
猜你喜欢

A 404 page source code imitating win10 blue screen

Unity simply implements the dialog function

2022 summary of the latest Android handler related interview questions

How to uninstall clean ZABBIX service? (super detailed)

Differences among BRD, MRD and PRD

高等数学(第七版)同济大学 习题3-5 个人解答

Qt:qmessagebox message box, custom signal and slot

STM32 RT thread virtual file system mount operation

单调栈——739. 每日温度

一文读懂Plato Farm的ePLATO,以及其高溢价缘由
随机推荐
Weekly recommended short video: how to correctly understand the word "lean"?
什么是Tor?Tor浏览器更新有什么用?
动态规划——63. 不同路径 II
MangoPapa 的实用小脚本(目录篇)
Push chart written by helm to harbor warehouse
Capacity expansion and reduction of RBD block storage device (VI)
Outlook tutorial, how to use color categories and reminders in outlook?
[openvx] VX for basic use of objects_ convolution
Container related concepts
pip-script. py‘ is not present Verifying transaction: failed
In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial
deepstream 检测结果截图
Airiot Q & A issue 6 | how to use the secondary development engine?
Input upload file and echo FileReader and restrict the type of file selection
动态规划——474. 一和零
STM32 RT thread virtual file system mount operation
Tungsten Fabric SDN — BGP as a Service
Tensorboard usage record
C语言力扣第45题之跳跃游戏 II。遍历跳跃
高等数学(第七版)同济大学 习题3-4 个人解答(前8题)