Skip to content

0070 | Easy accepted by leetcode climbing stairs #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 12 additions & 79 deletions Java/0070.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,12 @@
class Solution {
public int climbStairs(int n) {
if (n==1) return 1;

int first = 1;
int second = 2;

for (int i=3; i<=n; i++){
int third = first + second;
first = second;
second = third;
}

return second;
}
}


/*

Time - O(n)
Space - O(1)


[_, _, _, _, _, _]

n = 6

n != 1, continue next lines

dp[] - size 6
dp = [0, 0, 0, 0, 0, 0]
dp = [1, 0, 0, 0, 0, 0]
dp = [1, 2, 0, 0, 0, 0]

i = 2 to 5 (n-1)

i=2
dp[2] = dp[1] + dp[0] = 2 + 1 = 3
dp = [1, 2, 3, 0, 0, 0]

i=3
dp[3] = dp[2] + dp[1] = 3 + 2 = 5
dp = [1, 2, 3, 5, 0, 0]

i=4
dp[4] = dp[3] + dp[2] = 5 + 3 = 8
dp = [1, 2, 3, 5, 8, 0]

i=5
dp[5] = dp[4] + dp[3] = 8 + 5 = 13
dp = [1, 2, 3, 5, 8, 13]

return dp[n-1]
// return dp[5]
// return 13


// Dynamic Programming Solution

class Solution {
public int climbStairs(int n) {
if (n==1) return 1;

int[] dp = new int[n];
dp[0] = 1;
dp[1] = 2;

for (int i=2;i <n; i++){
dp[i] = dp[i-1] + dp[i-2];
}

return dp[n-1];
}
}



*/
class Solution {
public int climbStairs(int n) {
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for(int i = 2; i<=n; i++) {
dp[i] = dp[i - 1] + dp[i - 1];
}

return dp[n];
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Or from the Sheet 1 of this [Google Sheet](https://bit.ly/2EUhwnw)
| 0055 | [Jump Game](https://leetcode.com/problems/jump-game/) | Iterate from last index and check if we can reach there from current index or not | [Python](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Python/0055.py) | O(n) | O(1) | Medium | Array Greedy | [📺](https://www.youtube.com/watch?v=ymET7SJsDQc) |
| 0062| [Unique Paths (Total no of unique paths in m x n matrix)](https://leetcode.com/problems/unique-paths/) | As the first element in each row will always be 1, so maintaining one row is enough to reach bottom-right corner of the grid | [Java]() | O(n) | O(1) | Medium |Array Dynamic Programming | |
| 0070 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | steps[n]=steps[n-1]+steps[n-2]. | [C++](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/C++/0070.cpp) [Python](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Python/0070.py) | O(n) | O(1) | Easy | Dynamic Programming | [📺](https://www.youtube.com/watch?v=QiD2Hbwx2z0) |
| 0070 | [Climbing Stairs (Number of ways to reach top of steps by taking 1 or 2 steps) ](https://leetcode.com/problems/climbing-stairs/) | Number of ways to reach top of steps by taking 1 or 2 steps | [Python](https://github.com/oudarjya718/LeetCode-Solutions/blob/master/Java/0070.java) | O(1) | O(1) | Medium | Array Design | [📺](https://www.youtube.com/watch?v=QiD2Hbwx2z0) |

| 0072 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | minDis(i,j)=min(minDis(i-1,j),minDis(i,j-1),minDis(i-1,j-1))+1; | [Java](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Java/0072.py) | O(m*n) | O(m*n) | Hard | String Dynamic programming |
| 0073 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | Use the first cell of every row and column as a flag. This flag would determine whether a row or column has been set to zero. | [C++](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/C++/0073.cpp) [Python](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Python/0073.py) | O(m*n) | O(1) | Medium | Array | [📺](https://www.youtube.com/watch?v=W1I7slnETp4) |
| 0075 | [Sort an array of 0’s 1’s 2’s without using extra space or sorting algo](https://leetcode.com/problems/sort-colors/) | the logic is to count total number of 0's, 1's and 2's int the list and starting from beginning first append total 0's then total 1's and then 2's| [Python](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Python/0075.py) | O(n) | O(1) | Medium | Array | [📺](https://www.youtube.com/watch?v=BL9H4zIFHHw&list=PLsowTcGqVtPgo0VSIUIbcOgNQJzblGnst&index=11) |
Expand All @@ -42,6 +44,7 @@ Or from the Sheet 1 of this [Google Sheet](https://bit.ly/2EUhwnw)
| 1352 | [Product of the Last K Numbers](https://leetcode.com/problems/product-of-the-last-k-numbers/) | Add new element to list by multiplying it with previous number and return arr[n-1]/arr[n-k-1] | [Python](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Python/1352.py) | O(1) | O(1) | Medium | Array Design | [📺](https://www.youtube.com/watch?v=8CuVduv0Kyg) |



Format
| 0000 | [Ques name]() | Algo | [Java]() | O() | O() | Easy | Category | [📺]() |
|------|----------------------------------|------------|---------------------|-----|------|------------|----------|---------------|
Expand Down