Skip to content

Commit ef4dc71

Browse files
author
Мето Трајковски
committed
Added one more fibonacci solution and moved 5 solutions in the right category
1 parent 246c657 commit ef4dc71

7 files changed

Lines changed: 28 additions & 7 deletions
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
1818
=========================================
1919
The simplest solution is to sort the array, that's O(N LogN) time complexity.
20-
This solution is faster and uses a dictionary (HashMap).
20+
21+
But this solution is faster, it uses a dictionary (HashMap).
2122
For each number tries to find the smaller and the bigger consequence numbers, and marks them as visited.
2223
Time Complexity: O(N)
2324
Space Complexity: O(N)

‎Other/nth_fibonacci_number.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
Using a loop (without a recursion) compute the power of N of the matrix.
6363
Time Complexity: O(LogN)
6464
Space Complexity: O(1)
65+
66+
Using the golden ratio (Binet's formula) = (1+sqrt(5))/2 ~ 1.6183...
67+
Time Complexity: O(1)
68+
Space Complexity: O(1)
6569
'''
6670

6771

@@ -203,6 +207,20 @@ def nth_fibonacci_7(n):
203207
return res[1][1]
204208

205209

210+
##############
211+
# Solution 8 #
212+
##############
213+
214+
import math
215+
216+
def nth_fibonacci_8(n):
217+
golden_ratio = (1 + math.sqrt(5)) / 2
218+
return int((1 + math.pow(golden_ratio, n)) / math.sqrt(5))
219+
# without math module, using ** operator
220+
# golden_ratio = (1 + 5 ** 0.5) / 2
221+
# return int((1 + golden_ratio ** n) / (5 ** 0.5))
222+
223+
206224
###########
207225
# Testing #
208226
###########
@@ -217,6 +235,7 @@ def nth_fibonacci_7(n):
217235
print(nth_fibonacci_5(n))
218236
print(nth_fibonacci_6(n))
219237
print(nth_fibonacci_7(n))
238+
print(nth_fibonacci_8(n))
220239

221240
# Test 2
222241
# Correct result => 10946
@@ -227,4 +246,5 @@ def nth_fibonacci_7(n):
227246
print(nth_fibonacci_4(n))
228247
print(nth_fibonacci_5(n))
229248
print(nth_fibonacci_6(n))
230-
print(nth_fibonacci_7(n))
249+
print(nth_fibonacci_7(n))
250+
print(nth_fibonacci_8(n))
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
Maximum of all subarrays of size k
2+
Maximum of All Subarrays of Size K
33
44
Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
55
O(n) time and O(k) space!
@@ -12,7 +12,7 @@
1212
1313
=========================================
1414
Sliding window solution using deque or linked lists
15-
(only needs to be able to remove from both sides and to add on both sides in constant time).
15+
(only need to be able to remove from both sides and to add on both sides in constant time).
1616
Time Complexity: O(N)
1717
Space Complexity: O(K)
1818
'''

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ All solutions are written in [Python](https://www.python.org/) (more precisely,
1414
- [heapq](https://docs.python.org/3/library/heapq.html) (used when there is a need for [Priority Queue](https://en.wikipedia.org/wiki/Priority_queue) data structure).
1515

1616
So, to execute these solutions there is no need from installing any external packages. \
17-
Coding style and name conventions are described in this file [PEP8](https://www.python.org/dev/peps/pep-0008).
17+
Coding style and name conventions are described in the official [PEP8](https://www.python.org/dev/peps/pep-0008) page.
1818

1919
*Note that I'm **not** the author of these problems, they are from sites like [LeetCode](https://leetcode.com/) (you can find more than 40 sites like this in the [Training Sites](#Training-Sites) section). **Only** the solutions and explanations are mine.*
2020

@@ -90,7 +90,7 @@ Each solution/problem in this repo belongs to one of these categories:
9090
4. [Dynamic Programming](https://github.com/MTrajK/coding-problems/tree/master/Dynamic%20Programming) - 2D and 1D Dynamic Programming, LCS, LIS, Knapsack, etc.
9191
5. [Strings](https://github.com/MTrajK/coding-problems/tree/master/Strings) - String Manipulations, Reversing, Encodings/Decodings, etc.
9292
6. [Math](https://github.com/MTrajK/coding-problems/tree/master/Math) - GCD, LCM, Factorization, Geometry, Math Formulas, etc.
93-
7. [Other](https://github.com/MTrajK/coding-problems/tree/master/Other) - Backtracking, BFS, DFS, Queues, Stacks, Matrices, etc.
93+
7. [Other](https://github.com/MTrajK/coding-problems/tree/master/Other) - Backtracking, BFS, DFS, HashSets/Sets, HashMaps/Dictionaries, Stacks, Queues, Deques, Priority Queues (Heaps), Matrices, etc.
9494

9595

9696
## Learning Resources
@@ -142,7 +142,7 @@ Several books that have made an impression on me:
142142
2. [Introduction to Algorithms by CLRS](https://www.goodreads.com/book/show/6752187-introduction-to-algorithms) - This book is called "bible textbook of algorithms" by many programmers.
143143
3. [Algorithms by Robert Sedgewick & Kevin Wayne](https://www.goodreads.com/book/show/10803540-algorithms) - These authors are instructors of the previously mentioned coursera courses: [Algorithms Part 1](https://www.coursera.org/learn/algorithms-part1) and [Algorithms Part 2](https://www.coursera.org/learn/algorithms-part2). Also this book has excellent and free [site](http://algs4.cs.princeton.edu) with exercises, presentations, and examples.
144144
4. [The Algorithm Design Manual by Steven Skiena](https://www.goodreads.com/book/show/425208.The_Algorithm_Design_Manual) - The book describes many advanced topics and algorithms and it focuses on real life practical examples. This book has one of the best [site](http://www.algorist.com) with resources ([solutions](http://www.algorist.com/algowiki/index.php/The_Algorithms_Design_Manual_(Second_Edition)), [algorithms and data structures](http://www.algorist.com/algorist.html), [python implementations](http://www.algorist.com/languages/Python.html)).
145-
5. [Algorithms by S. Dasgupta, C. Papadimitriou, and U. Vazirani](https://www.goodreads.com/book/show/138563.Algorithms) - This book is an official book for algorithm and data structure classes in several famous universities.
145+
5. [Algorithms by S. Dasgupta, C. Papadimitriou, and U. Vazirani](https://www.goodreads.com/book/show/138563.Algorithms) - This book is an official book for algorithms and data structures classes in several famous universities.
146146
6. [Competitive Programming 3 by Steven Halim & Felix Halim](https://www.goodreads.com/book/show/22820968-competitive-programming-3) - A great book that prepares you for competitive programming (not for complete beginners). You can learn many things and tricks about competitive programming.
147147

148148

0 commit comments

Comments
 (0)