Skip to content

Commit

Permalink
Add 509_Fibonacci_Number
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyuangong committed Jun 16, 2019
1 parent 886ae35 commit 5a693a6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Also, there are open source implementations for basic data structs and algorithm
| 475 | [Heaters](https://leetcode.com/problems/heaters/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/475_Heaters.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/475_Heaters.java) | 1. Binary search hourse in heater array, O(nlogn) and O(1)<br> 2. Two points, O(nlogn) and O(1) |
| 479 | [Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/479_Largest_Palindrome_Product.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/479_Largest_Palindrome_Product.java) | 1. Product max palindrome than check, O(n^2) and O(1)<br>2. [Math](# https://leetcode.com/problems/largest-palindrome-product/discuss/96305/Python-Solution-Using-Math-In-48ms) |
| 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/482_License_Key_Formatting.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/482_License_Key_Formatting.java) | String processing, lower and len % K, O(n) and O(n) |
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/509_Fibonacci_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/509_Fibonacci_Number.java) | 1. Recursive, O(n) <br>2. DP with memo, O(n). Note that N<=30, which means that we can keep a memo from 0 to 30. |
| 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/538_Convert_BST_to_Greater_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/538_Convert_BST_to_Greater_Tree.java) | Right first DFS with a variable recording sum of node.val and right.val. 1. Recursive.<br>2. Stack 3. Reverse Morris In-order Traversal |
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/543_Diameter_of_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/543_Diameter_of_Binary_Tree.java) | DFS with O(1) for max answer |
| 547 | [Friend Circles](https://leetcode.com/problems/friend-circles/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/547_Friend_Circles.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/547_Friend_Circles.java) | 1. DFS, O(n^2) and O(1)<br>2. BFS, O(n^2) and O(1)<br>3. Union-find, O(n^2) and O(n)|
Expand Down Expand Up @@ -205,7 +206,7 @@ Also, there are open source implementations for basic data structs and algorithm
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/contest/weekly-contest-114/problems/verifying-an-alien-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/953_Verifying_an_Alien_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/953_Verifying_an_Alien_Dictionary.java) | Use hashmap to store index of each value, then create a comparator based on this index, O(n) and O(n) |
| 954 | [Array of Doubled Pairs](https://leetcode.com/contest/weekly-contest-114/problems/array-of-doubled-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/954_Array_of_Doubled_Pairs.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/954_Array_of_Doubled_Pairs.java) | Sort, then use hashmap to store the frequency of each value. Then, check n, 2 * n in hashmap, O(nlogn) and O(n) |
| 961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/submissions/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/961_N-Repeated_Element_in_Size_2N_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/961_N-Repeated_Element_in_Size_2N_Array.java) | Hash and count number, O(n) and O(n) |
| 962 | [Maximum Width Ramp](https://leetcode.com/problems/maximum-width-ramp/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/962_Maximum_Width_Ramp.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/962_Maximum_Width_Ramp.java) | 1. Sort index by value, then transfer problem into finding max gap between index, O(nlogn) and O(1)<br> |
| 962 | [Maximum Width Ramp](https://leetcode.com/problems/maximum-width-ramp/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/962_Maximum_Width_Ramp.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/962_Maximum_Width_Ramp.java) | 1. Sort index by value, then transfer problem into finding max gap between index, O(nlogn) and O(1)<br>2. Binary Search for candicates, O(nlogn) and O(n) |

| # | To Understand |
|---| ----- |
Expand Down
25 changes: 25 additions & 0 deletions java/509_Fibonacci_Number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
/*public int fib(int N) {
// Recursively, O(n)
if (N == 0) return 0;
if (N == 1) return 1;
return fib(N - 1) + fib(N - 2);
}*/

private List<Integer> memo;

public Solution() {
memo = new ArrayList();
memo.add(0);
memo.add(1);
}

public int fib(int N) {
// Dp with memo, O(n)
if (N < memo.size()) return memo.get(N);
for (int i = memo.size(); i <= N; i++) {
memo.add(memo.get(i - 1) + memo.get(i - 2));
}
return memo.get(N);
}
}
30 changes: 30 additions & 0 deletions python/509_Fibonacci_Number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution(object):

def __init__(self):
self.memo = []
self.memo.append(0)
self.memo.append(1)

def fib(self, N):
"""
DP with memo
:type N: int
:rtype: int
"""
if N < len(self.memo):
return self.memo[N]
for i in range(len(self.memo), N + 1):
self.memo.append(self.memo[i - 1] + self.memo[i - 2])
return self.memo[N]

# def fib(self, N):
# """
# Recursively, O(n)
# :type N: int
# :rtype: int
# """
# if N == 0:
# return 0
# if N == 1:
# return 1
# return self.fib(N - 1) + self.fib(N - 2)

0 comments on commit 5a693a6

Please sign in to comment.