GeezerVison is showing github.com — Added one more fibonacci solution and moved 5 solutions in the right … · onowdev/coding-problems@ef4dc71
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboardExpand all lines: Other/longest_consecutive_sequence.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 |
| 18 | 18 | ========================================= | 19 | 19 | 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). | 21 | 22 | For each number tries to find the smaller and the bigger consequence numbers, and marks them as visited. | 22 | 23 | Time Complexity: O(N) | 23 | 24 | Space Complexity: O(N) | |
Collapse file Copy file name to clipboardExpand all lines: Other/nth_fibonacci_number.py +21-1Lines changed: 21 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 62 | 62 | Using a loop (without a recursion) compute the power of N of the matrix. | 63 | 63 | Time Complexity: O(LogN) | 64 | 64 | 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) | 65 | 69 | ''' | 66 | 70 |
| 67 | 71 |
| @@ -203,6 +207,20 @@ def nth_fibonacci_7(n): | 203 | 207 | return res[1][1] | 204 | 208 |
| 205 | 209 |
| | 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 | + | 206 | 224 | ########### | 207 | 225 | # Testing # | 208 | 226 | ########### | @@ -217,6 +235,7 @@ def nth_fibonacci_7(n): | 217 | 235 | print(nth_fibonacci_5(n)) | 218 | 236 | print(nth_fibonacci_6(n)) | 219 | 237 | print(nth_fibonacci_7(n)) | | 238 | +print(nth_fibonacci_8(n)) | 220 | 239 |
| 221 | 240 | # Test 2 | 222 | 241 | # Correct result => 10946 | @@ -227,4 +246,5 @@ def nth_fibonacci_7(n): | 227 | 246 | print(nth_fibonacci_4(n)) | 228 | 247 | print(nth_fibonacci_5(n)) | 229 | 248 | print(nth_fibonacci_6(n)) | 230 | | -print(nth_fibonacci_7(n)) | | 249 | +print(nth_fibonacci_7(n)) | | 250 | +print(nth_fibonacci_8(n)) |
Collapse file Copy file name to clipboard File renamed without changes. Collapse file Copy file name to clipboardExpand all lines: Other/sliding_window_maximum.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | ''' | 2 | | -Maximum of all subarrays of size k | | 2 | +Maximum of All Subarrays of Size K | 3 | 3 |
| 4 | 4 | Given an array and an integer k, find the maximum for each and every contiguous subarray of size k. | 5 | 5 | O(n) time and O(k) space! | | 12 | 12 |
| 13 | 13 | ========================================= | 14 | 14 | 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). | 16 | 16 | Time Complexity: O(N) | 17 | 17 | Space Complexity: O(K) | 18 | 18 | ''' | |
Collapse file Copy file name to clipboardExpand all lines: README.md +3-3Lines changed: 3 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,7 @@ All solutions are written in [Python](https://www.python.org/) (more precisely, | 14 | 14 | - [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). | 15 | 15 |
| 16 | 16 | 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. | 18 | 18 |
| 19 | 19 | *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.* | 20 | 20 |
| @@ -90,7 +90,7 @@ Each solution/problem in this repo belongs to one of these categories: | 90 | 90 | 4. [Dynamic Programming](https://github.com/MTrajK/coding-problems/tree/master/Dynamic%20Programming) - 2D and 1D Dynamic Programming, LCS, LIS, Knapsack, etc. | 91 | 91 | 5. [Strings](https://github.com/MTrajK/coding-problems/tree/master/Strings) - String Manipulations, Reversing, Encodings/Decodings, etc. | 92 | 92 | 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. | 94 | 94 |
| 95 | 95 |
| 96 | 96 | ## Learning Resources | @@ -142,7 +142,7 @@ Several books that have made an impression on me: | 142 | 142 | 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. | 143 | 143 | 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. | 144 | 144 | 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. | 146 | 146 | 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. | 147 | 147 |
| 148 | 148 |
| |
|