From f6552a5ac71c30c9a8de0a7ab51e38c3c8956c08 Mon Sep 17 00:00:00 2001 From: diagon Date: Thu, 20 Oct 2022 13:56:23 +0530 Subject: [PATCH] Added Leetcode questions --- Leetcode_BestTimeToBuyAndSellStockIV.cpp | 54 ++++++++++++++++++++++ Leetcode_GenerateParanthesis.cpp | 52 +++++++++++++++++++++ Leetcode_JumpGame.cpp | 45 ++++++++++++++++++ Leetcode_UniquePath.cpp | 59 ++++++++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 Leetcode_BestTimeToBuyAndSellStockIV.cpp create mode 100644 Leetcode_GenerateParanthesis.cpp create mode 100644 Leetcode_JumpGame.cpp create mode 100644 Leetcode_UniquePath.cpp diff --git a/Leetcode_BestTimeToBuyAndSellStockIV.cpp b/Leetcode_BestTimeToBuyAndSellStockIV.cpp new file mode 100644 index 0000000..6ed0257 --- /dev/null +++ b/Leetcode_BestTimeToBuyAndSellStockIV.cpp @@ -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& 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 v1(k, INT_MIN); + vector 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(); + + } +}; \ No newline at end of file diff --git a/Leetcode_GenerateParanthesis.cpp b/Leetcode_GenerateParanthesis.cpp new file mode 100644 index 0000000..3dcac7f --- /dev/null +++ b/Leetcode_GenerateParanthesis.cpp @@ -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 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 && co){ + 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 generateParenthesis(int n) { + rec(0,0,n,""); + + return v; + } +}; \ No newline at end of file diff --git a/Leetcode_JumpGame.cpp b/Leetcode_JumpGame.cpp new file mode 100644 index 0000000..feafa99 --- /dev/null +++ b/Leetcode_JumpGame.cpp @@ -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& nums) { + int m=0; + for(int i=0;i=nums.size()-1) return true; + if(m>=i){ + m=max(m,i+nums[i]); + } + else if(m 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> v(m,vector(n)); + for(int i=0;i