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

Leetcode questions added #127

Merged
merged 1 commit into from
Nov 2, 2022
Merged
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
54 changes: 54 additions & 0 deletions Leetcode_BestTimeToBuyAndSellStockIV.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*

Problem No:-188

You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.

Find the maximum profit you can achieve. You may complete at most k transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).



Example 1:

Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:

Input: k = 2, prices = [3,2,6,5,0,3]
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.


Constraints:

1 <= k <= 100
1 <= prices.length <= 1000
0 <= prices[i] <= 1000

*/


class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if (k == 0) return 0;
if (k >= prices.size()/2) {
int s = 0;
for (int i = 1; i < prices.size(); ++i)
s += max(prices[i] - prices[i-1], 0);
return s;
}
vector<int> v1(k, INT_MIN);
vector<int> v2(k, 0);
for (int price : prices)
for (int i = 0; i < k; ++i) {
v1[i] = max(v1[i], (i > 0 ? v2[i-1] : 0) - price);
v2[i] = max(v2[i], v1[i] + price);
}
return v2.back();

}
};
52 changes: 52 additions & 0 deletions Leetcode_GenerateParanthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Problem No:- 22
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Example 1:

Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:

Input: n = 1
Output: ["()"]

Constraints:
1 <= n <= 8

*/


class Solution {
public:

vector<string> v;


void rec(int o,int c,int n,string s){
if(o==n && c==n){
v.push_back(s);
return;
}
else if(o==n && c<n){
rec(o,c+1,n,s+')');
}
else if(c>o){
return;
}
else if(o==c){
rec(o+1,c,n,s+'(');
}
else if(o>c){
rec(o+1,c,n,s+'(');
rec(o,c+1,n,s+')');
}
else
return;
}
vector<string> generateParenthesis(int n) {
rec(0,0,n,"");

return v;
}
};
45 changes: 45 additions & 0 deletions Leetcode_JumpGame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Problem No:- 55

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.



Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.


Constraints:

1 <= nums.length <= 104
0 <= nums[i] <= 105


*/


class Solution {
public:
bool canJump(vector<int>& nums) {
int m=0;
for(int i=0;i<nums.size();i++){

if(m>=nums.size()-1) return true;
if(m>=i){
m=max(m,i+nums[i]);
}
else if(m<i) break;
}
return false;
}
};
59 changes: 59 additions & 0 deletions Leetcode_UniquePath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Problem No:-62

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

The test cases are generated so that the answer will be less than or equal to 2 * 109.



Example 1:


Input: m = 3, n = 7
Output: 28
Example 2:

Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down


Constraints:

1 <= m, n <= 100

*/


class Solution {
public:
int uniquePaths(int m, int n) {
if(m<=2){
if(m==1) return 1;
else return n;
}
if(n<=2){
if(n==1) return 1;
else return m;
}
vector<vector<int>> v(m,vector<int>(n));
for(int i=0;i<m;i++){
v[i][0]=1;
}
for(int i=0;i<n;i++){
v[0][i]=1;
}

for(int i=1;i<m;i++)
for(int j=1;j<n;j++)
v[i][j]=v[i-1][j]+v[i][j-1];

return v[m-1][n-1];
}
};