forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(pylint): unionfind/count_islands.py improves pylint score from 3.71 to 10.00 fixes KTH-Software-Engineering-DD2480#2 * refactor(pylint): algorithms/search/*.py improves pylint score from 5.83 to 10.00. fixes KTH-Software-Engineering-DD2480#7 * feat: improving pylint score * refactor dp.combination_sum: lint score from 5.42 to 9.13 * refactor(pylint): algorithms/graph/*.py Improves pylint score from 5.51 to 9.96. Score is lower than 10 due to duplication between maximum_flow_bfs.py and maximum_flow_dfs.py. However, due to the educational nature of this repo, keeping it as is will probably be benefitial as it reduces complexity while reading (having to jump between files). fixes KTH-Software-Engineering-DD2480#9 * refactor egg_drop, hosoya_triangle, min_cost_path and planting_trees * feat: improving pylint score of one_sparse_recovery * refactor: move tests from regex_matching to tests folder * refactor: lint score from 4.27 to 9.46 a reason that lint score isn't 10.00 is that the module is called dp, which in turn makes pylint raise invalid name for all files in dp. I leave that as it is, since I don't want to mess with the current folder structure. fixes keon#3 * Fix: Fixed lint error, lint value now aboe 8 * refactor: add docstring to misra, score 10 * fix (misra): add a newline * refactor: add docstring for one_sparse, score 9.33 * wip: refactor (pylint): algorithms/maths/ wip: keon#4 * Fix: pylint is above 8 for tree * refactor (pylint): algorithms/maths/ Finished improving pylint score for maths folder. fixes: keon#4 * Fixed: comments * fix: small intendation fix Co-authored-by: ekorre1001 <[email protected]> Co-authored-by: Philip Salqvist <[email protected]> Co-authored-by: psalqvist <[email protected]> Co-authored-by: Kubha99 <[email protected]> Co-authored-by: mantaur <[email protected]>
- Loading branch information
1 parent
15e5292
commit 3259a07
Showing
102 changed files
with
2,235 additions
and
1,735 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
""" | ||
You are climbing a stair case. | ||
It takes n steps to reach to the top. | ||
It takes `steps` number of steps to reach to the top. | ||
Each time you can either climb 1 or 2 steps. | ||
In how many distinct ways can you climb to the top? | ||
Note: Given n will be a positive integer. | ||
Note: Given argument `steps` will be a positive integer. | ||
""" | ||
|
||
|
||
# O(n) space | ||
|
||
def climb_stairs(n): | ||
def climb_stairs(steps): | ||
""" | ||
:type n: int | ||
:type steps: int | ||
:rtype: int | ||
""" | ||
arr = [1, 1] | ||
for _ in range(1, n): | ||
for _ in range(1, steps): | ||
arr.append(arr[-1] + arr[-2]) | ||
return arr[-1] | ||
|
||
|
||
# the above function can be optimized as: | ||
# O(1) space | ||
|
||
def climb_stairs_optimized(n): | ||
a = b = 1 | ||
for _ in range(n): | ||
a, b = b, a + b | ||
return a | ||
def climb_stairs_optimized(steps): | ||
""" | ||
:type steps: int | ||
:rtype: int | ||
""" | ||
a_steps = b_steps = 1 | ||
for _ in range(steps): | ||
a_steps, b_steps = b_steps, a_steps + b_steps | ||
return a_steps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
""" | ||
Problem | ||
Given a value n, if we want to make change for N cents, | ||
and we have infinite supply of each of | ||
coins = {S1, S2, .. , Sm} valued coins, how many ways | ||
can we make the change? | ||
The order of coins doesn't matter. | ||
For example, for n = 4 and coins = [1, 2, 3], there are | ||
four solutions: | ||
Given a value `value`, if we want to make change for `value` cents, and we have infinite | ||
supply of each of coins = {S1, S2, .. , Sm} valued `coins`, how many ways can we make the change? | ||
The order of `coins` doesn't matter. | ||
For example, for `value` = 4 and `coins` = [1, 2, 3], there are four solutions: | ||
[1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3]. | ||
So output should be 4. | ||
For n = 10 and coins = [2, 5, 3, 6], there are five solutions: | ||
For `value` = 10 and `coins` = [2, 5, 3, 6], there are five solutions: | ||
[2, 2, 2, 2, 2], [2, 2, 3, 3], [2, 2, 6], [2, 3, 5] and [5, 5]. | ||
So the output should be 5. | ||
Time complexity: O(n * m) where n is the value and m is the number of coins | ||
Time complexity: O(n * m) where n is the `value` and m is the number of `coins` | ||
Space complexity: O(n) | ||
""" | ||
|
||
def count(coins, value): | ||
""" Find number of combination of `coins` that adds upp to `value` | ||
def count(coins, n): | ||
Keyword arguments: | ||
coins -- int[] | ||
value -- int | ||
""" | ||
# initialize dp array and set base case as 1 | ||
dp = [1] + [0] * n | ||
dp_array = [1] + [0] * value | ||
|
||
# fill dp in a bottom up manner | ||
for coin in coins: | ||
for i in range(coin, n+1): | ||
dp[i] += dp[i-coin] | ||
for i in range(coin, value+1): | ||
dp_array[i] += dp_array[i-coin] | ||
|
||
return dp[n] | ||
return dp_array[value] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.