Skip to content

Commit

Permalink
dp-basic-series (#574)
Browse files Browse the repository at this point in the history
## Pull Request for PyVerse 💡

### Requesting to submit a pull request to the PyVerse repository.

---

#### Issue Title
**Please enter the title of the issue related to your pull request.**  
Dynamic Programming Series (Dynamic Programming Basic Algos)

- [x] I have provided the issue title.

---

#### Info about the Related Issue
**What's the goal of the project?**  
A dynamic programming learning library

- [x] I have described the aim of the project.

---

#### Name
**Please mention your name.**  
Anneshu Nag

- [x] I have provided my name.

---

#### GitHub ID
**Please mention your GitHub ID.**  
NK-Works

- [x] I have provided my GitHub ID.

---

#### Email ID
**Please mention your email ID for further communication.**  
nkworks777@gmail.com

- [x] I have provided my email ID.

---

#### Identify Yourself
**Mention in which program you are contributing (e.g., WoB, GSSOC, SSOC,
SWOC).**
GSSOC, Hacktoberfest

- [x] I have mentioned my participant role.

---

#### Closes
**Enter the issue number that will be closed through this PR.**  
Closes: #538

- [x] I have provided the issue number.

---

#### Describe the Add-ons or Changes You've Made
**Give a clear description of what you have added or modified.**  
 I have added the basic easy dp algos

- [x] I have described my changes.

---

#### Type of Change
**Select the type of change:**  
- [x] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [x] Code style update (formatting, local variables)
- [x] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [x] This change requires a documentation update

---

#### How Has This Been Tested?
I tested them by running them

- [x] I have described my testing process.

---

#### Checklist
**Please confirm the following:**  
- [x] My code follows the guidelines of this project.
- [x] I have performed a self-review of my own code.
- [x] I have commented my code, particularly wherever it was hard to
understand.
- [x] I have made corresponding changes to the documentation.
- [x] My changes generate no new warnings.
- [x] I have added things that prove my fix is effective or that my
feature works.
- [x] Any dependent changes have been merged and published in downstream
modules.
  • Loading branch information
UTSAVS26 authored Oct 16, 2024
2 parents 00c4043 + f558c47 commit 2860b60
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def climbStairs(n):
# Handle base cases
if n == 1:
return 1
if n == 2:
return 2

# Create a table to store the number of ways to reach each step
dp = [0] * (n + 1)
dp[1] = 1 # 1 way to reach the first step
dp[2] = 2 # 2 ways to reach the second step

# Fill the table in a bottom-up manner
for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]

return dp[n]

# Test the function
n = 5
print(f"Number of ways to climb {n} stairs: {climbStairs(n)}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def fibonacci(n):
# Handle base cases
if n == 0:
return 0
if n == 1:
return 1

# Create a table to store Fibonacci numbers
fib = [0] * (n + 1)
fib[1] = 1

# Fill the table in a bottom-up manner
for i in range(2, n + 1):
fib[i] = fib[i - 1] + fib[i - 2]

return fib[n]

# Test the function
n = 10
print(f"Fibonacci number at position {n} is: {fibonacci(n)}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def rob(houses):
# If there are no houses, return 0
if not houses:
return 0
# If there is only one house, return its value
if len(houses) == 1:
return houses[0]

# Create a table to store the maximum money that can be robbed up to each house
dp = [0] * len(houses)
dp[0] = houses[0] # Max money for the first house
dp[1] = max(houses[0], houses[1]) # Max money for the first two houses

# Fill the table in a bottom-up manner
for i in range(2, len(houses)):
# For each house, decide whether to rob it (dp[i-2] + houses[i]) or not (dp[i-1])
dp[i] = max(dp[i - 1], dp[i - 2] + houses[i])

return dp[-1]

# Test the function
houses = [2, 7, 9, 3, 1]
print(f"Maximum money that can be robbed: {rob(houses)}")

0 comments on commit 2860b60

Please sign in to comment.