Skip to content
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

Climbing Stairs #1776

Closed
wants to merge 8 commits into from
Closed
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
52 changes: 52 additions & 0 deletions Miscellaneous Algorithms/Climbing stairs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

# Climbing Stairs :

## Description
We will perform a programme in C language called climbing stairs

### Problem Defination

1. You are climbing a staircase.


2. It takes n steps to reach the top.


3. Each time you can either climb 1 or 2 steps

4. In how many distinct ways can you climb to the top?



### Examples :

Example 1:

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps



Example 2:

Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step





### Time Complexity

Time complexity: O(n)
n is number of steps to reach the top.

Space complexity: O(n)
For dp list
13 changes: 13 additions & 0 deletions Miscellaneous Algorithms/Climbing stairs/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

int climbStairs(int n) {
if(n<1){
return 0;
}
int dp[100];
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];
}
32 changes: 32 additions & 0 deletions Miscellaneous Algorithms/Maximum Products/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Maximum Products of two elements in an array



## How Does It Work?

Given the array of integers nums, you will choose two different indices i and j of that array. Return the maximum value of (nums[i]-1)*(nums[j]-1).


## Example:
### Example 1:

Input: nums = [3,4,5,2]
Output: 12
Explanation: If you choose the indices i=1 and j=2 (indexed from 0), you will get the maximum value, that is, (nums[1]-1)*(nums[2]-1) = (4-1)*(5-1) = 3*4 = 12.

### Example 2:
Input: nums = [1,5,4,5]
Output: 16
Explanation: Choosing the indices i=1 and j=3 (indexed from 0), you will get the maximum value of (5-1)*(5-1) = 16.



# Approach :
1. Sort the Array : Begin by sorting the array nums in ascending order. This will arrange the numbers such that the largest numbers are at the end of the array.
2. Identify the Two Largest Numbers : After sorting, the two largest numbers in the array will be at the last two indices. Specifically, the largest number will be at nums[nums.size() - 1] and the second largest will be at nums[nums.size() - 2].
3. Calculate the Product : Subtract 1 from each of these two largest numbers and then calculate their product.

# Complexity :
Time complexity : O(n*log(n))
The time complexity of this solution is O(n log n) due to the sorting operation, where n is the number of elements in the input vector.
Space complexity : O(1)
8 changes: 8 additions & 0 deletions Miscellaneous Algorithms/Maximum Products/programme.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
int cmp(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}

int maxProduct(int* nums, int numsSize) {
qsort(nums, numsSize, sizeof(int), cmp);
return (nums[numsSize - 2] - 1) * (nums[numsSize - 1] - 1);
}