From ba96a939460727d8c0494bd0e35dbdf70c392348 Mon Sep 17 00:00:00 2001 From: Aditya Anand Date: Mon, 3 Oct 2022 00:34:20 +0530 Subject: [PATCH] added code for combination_sum_II in python --- Combination Sum II/combination_sumII.cpp | 32 ++++++++ Combination Sum II/combination_sum_II.java | 20 +++++ Combination Sum II/combination_sum_II.py | 0 LRUCache.cpp | 77 +++++++++++++++++++ .../longest_valid_parentheses.java | 20 +++++ Longest Valid Parentheses/parentheses.cpp | 34 ++++++++ Next Permutation/new_permutation.java | 30 ++++++++ Next Permutation/nextPermutation.cpp | 19 +++++ Permutations/Permutations.java | 26 +++++++ Permutations/permutations.cpp | 14 ++++ README.md | 8 ++ .../Search_in_Rotated_Sorted_Array | 16 ++++ Search in Rotated Sorted Array/search.cpp | 20 +++++ .../search_in_rotated_sorted_array.java | 60 +++++++++++++++ combination-sum-ii | 60 +++++++++++++++ firstMissingPositiveNo.cpp | 12 +++ trappingRainWater.cpp | 18 +++++ 17 files changed, 466 insertions(+) create mode 100644 Combination Sum II/combination_sumII.cpp create mode 100644 Combination Sum II/combination_sum_II.java create mode 100644 Combination Sum II/combination_sum_II.py create mode 100644 LRUCache.cpp create mode 100644 Longest Valid Parentheses/longest_valid_parentheses.java create mode 100644 Longest Valid Parentheses/parentheses.cpp create mode 100644 Next Permutation/new_permutation.java create mode 100644 Next Permutation/nextPermutation.cpp create mode 100644 Permutations/Permutations.java create mode 100644 Permutations/permutations.cpp create mode 100644 README.md create mode 100644 Search in Rotated Sorted Array/Search_in_Rotated_Sorted_Array create mode 100644 Search in Rotated Sorted Array/search.cpp create mode 100644 Search in Rotated Sorted Array/search_in_rotated_sorted_array.java create mode 100644 combination-sum-ii create mode 100644 firstMissingPositiveNo.cpp create mode 100644 trappingRainWater.cpp diff --git a/Combination Sum II/combination_sumII.cpp b/Combination Sum II/combination_sumII.cpp new file mode 100644 index 0000000..cb4f157 --- /dev/null +++ b/Combination Sum II/combination_sumII.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + vector> result; + + void comsum(vector &curr, int target, int sum, vector &candidates, int curInd, int n){ + if(target == sum){ + result.push_back(curr); + return; + } + else if(sum>target){ + return; + } + + for(int i = curInd; i < n; i++){ + if(i != curInd && candidates[i]==candidates[i-1]) //to avoid picking up the same combnations i.e. we don't pick same element for certain kth position of a combination + continue; + sum += candidates[i]; + curr.push_back(candidates[i]); + comsum(curr, target, sum, candidates, i+1, n); + sum -= candidates[i]; + curr.pop_back(); + } + + } + vector> combinationSum2(vector& candidates, int target) { + vector curr; + int n = candidates.size(); + sort(candidates.begin(), candidates.end()); + comsum(curr, target, 0, candidates, 0, n); + return result; + } +}; diff --git a/Combination Sum II/combination_sum_II.java b/Combination Sum II/combination_sum_II.java new file mode 100644 index 0000000..7d32e3c --- /dev/null +++ b/Combination Sum II/combination_sum_II.java @@ -0,0 +1,20 @@ +public List> combinationSum2(int[] cand, int target) { + Arrays.sort(cand); + List> res = new ArrayList>(); + List path = new ArrayList(); + dfs_com(cand, 0, target, path, res); + return res; +} +void dfs_com(int[] cand, int cur, int target, List path, List> res) { + if (target == 0) { + res.add(new ArrayList(path)); + return ; + } + if (target < 0) return; + for (int i = cur; i < cand.length; i++){ + if (i > cur && cand[i] == cand[i-1]) continue; + path.add(path.size(), cand[i]); + dfs_com(cand, i+1, target - cand[i], path, res); + path.remove(path.size()-1); + } +} \ No newline at end of file diff --git a/Combination Sum II/combination_sum_II.py b/Combination Sum II/combination_sum_II.py new file mode 100644 index 0000000..e69de29 diff --git a/LRUCache.cpp b/LRUCache.cpp new file mode 100644 index 0000000..6c7939d --- /dev/null +++ b/LRUCache.cpp @@ -0,0 +1,77 @@ +class LRUCache { +public: + //doubly linked list + class Node{ + public: + int key,val; + Node* next; + Node* prev; + + Node(int key,int val){ + this->key=key; + this->val=val; + } + }; + + Node* head=new Node(-1,-1); + Node* tail=new Node(-1,-1); + + int csize; + unordered_mapmp; + + void addNode(Node* node){ + Node* temp=head->next; + node->next=temp; + head->next=node; + node->prev=head; + temp->prev=node; + } + + void deleteNode(Node* node){ + Node* temp=node->prev; + Node* temp1=node->next; + temp->next=temp1; + temp1->prev=temp; + } + + + LRUCache(int capacity) { + csize=capacity; + head->next=tail; + tail->prev=head; + } + + int get(int key) { + if(mp.find(key)!=mp.end()){ + Node* temp=mp[key]; + int ans=temp->val; + mp.erase(key); + deleteNode(temp); + addNode(temp); + mp[key]=head->next; + return ans; + } + return -1; + } + + void put(int key, int value) { + if(mp.find(key)!=mp.end()){ + Node* temp=mp[key]; + mp.erase(key); + deleteNode(temp); + } + if(mp.size()==csize){ + mp.erase(tail->prev->key); + deleteNode(tail->prev); + } + addNode(new Node(key,value)); + mp[key]=head->next; + } +}; + +/** + * Your LRUCache object will be instantiated and called as such: + * LRUCache* obj = new LRUCache(capacity); + * int param_1 = obj->get(key); + * obj->put(key,value); + */ diff --git a/Longest Valid Parentheses/longest_valid_parentheses.java b/Longest Valid Parentheses/longest_valid_parentheses.java new file mode 100644 index 0000000..a14f244 --- /dev/null +++ b/Longest Valid Parentheses/longest_valid_parentheses.java @@ -0,0 +1,20 @@ +class Solution { + public int longestValidParentheses(String s) { + Stack index=new Stack(); + index.push(-1); + int max=0; + for(int i=0; i= 0; j--) { + if(s[j] == ')') { + k++; + } + else if(s[j] == '(') { + k--; + if(k == 0) + ans = max(ans, i - j + 1); + } + if(k < 0) { + k = 0; + i = j - 1; + } + } + return ans; + } +}; diff --git a/Next Permutation/new_permutation.java b/Next Permutation/new_permutation.java new file mode 100644 index 0000000..be92e64 --- /dev/null +++ b/Next Permutation/new_permutation.java @@ -0,0 +1,30 @@ +class Solution { + public void nextPermutation(int[] nums) { + + int infPoint=-1; + for(int i=nums.length-1;i>0;i--){ + if (nums[i]>nums[i-1]){ + infPoint=i-1; + break; + } + } + + if (infPoint != -1){ + for(int j=nums.length-1;j>0;j--){ + if (nums[infPoint]& nums) { + int n=nums.size(); + int i,j; + for(i=n-2;i>=0;i--){ + if(nums[i]i;j--){ + if(nums[j]>nums[i]) break; + } + swap(nums[j],nums[i]); + reverse(nums.begin()+i+1,nums.end()); + } +}; diff --git a/Permutations/Permutations.java b/Permutations/Permutations.java new file mode 100644 index 0000000..0ce760a --- /dev/null +++ b/Permutations/Permutations.java @@ -0,0 +1,26 @@ +public class Solution { + + public List> permute(int[] num) { + List> result = new ArrayList>(); + + result.add(new ArrayList()); + List> current = new ArrayList>(); + for (int i = 0; i < num.length; ++i) { + + current.clear(); + for (List l : result) { + + for (int j = 0; j < l.size()+1; ++j) { + + l.add(j, num[i]); + ArrayList temp = new ArrayList(l); + current.add(temp); + l.remove(j); + } + } + result = new ArrayList>(current); + } + return result; + } + +} \ No newline at end of file diff --git a/Permutations/permutations.cpp b/Permutations/permutations.cpp new file mode 100644 index 0000000..6c106e7 --- /dev/null +++ b/Permutations/permutations.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + vector> permute(vector& nums) { + vector> ans; + vector temp=nums; + ans.push_back(nums); + sort(nums.begin(),nums.end()); + do{ + if(nums!=temp) + ans.push_back(nums); + }while(next_permutation(nums.begin(),nums.end())); + return ans; + } +}; diff --git a/README.md b/README.md new file mode 100644 index 0000000..e099669 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Leetcode-solutions + +Hactoberfest contribution - 2022 + +1. Add leetcode solutions of given problem in any language. +2. The file name should be same as question name. +3. Don't add irrelevant code, it will mark as invalid. +4. Don't raise invalid PR's diff --git a/Search in Rotated Sorted Array/Search_in_Rotated_Sorted_Array b/Search in Rotated Sorted Array/Search_in_Rotated_Sorted_Array new file mode 100644 index 0000000..b8e6656 --- /dev/null +++ b/Search in Rotated Sorted Array/Search_in_Rotated_Sorted_Array @@ -0,0 +1,16 @@ +#Easy Solution +with memoey usage :43.5 MB + +class Solution { + public int search(int[] nums, int target) { + + for(int i=0;i& nums, int target) { + int n=nums.size(); + int start=0,end=n-1; + while(start<=end){ + int mid=(start+end)/2; + if(nums[mid]==target)return mid; + else if(nums[mid]>=nums[start]){ + if(nums[start]<=target && target<=nums[mid])end=mid-1; + else start=mid+1; + } + else { + if(target>=nums[mid]&&target<=nums[end])start=mid+1; + else end=mid-1; + } + } + return -1; + } +}; diff --git a/Search in Rotated Sorted Array/search_in_rotated_sorted_array.java b/Search in Rotated Sorted Array/search_in_rotated_sorted_array.java new file mode 100644 index 0000000..dc078af --- /dev/null +++ b/Search in Rotated Sorted Array/search_in_rotated_sorted_array.java @@ -0,0 +1,60 @@ +class Solution { + public int search(int[] nums, int target) { + int pivot=pivot(nums); + return BinarySearch(nums,pivot,target); + + } + static int pivot(int[] arr){ + int start=0; + int end=arr.length-1; + while(start<=end){ + int mid=start+(end-start)/2; + //4 cases + if(midarr[mid+1]){ + return mid; + } + if(mid>start && arr[mid] &arr, int n, vector &subSet, vector> &powerSet, int sum, int target) { + + // If sum is equal to target, we have reached a Valid Combination + + if(sum == target) + { + powerSet.push_back(subSet) ; + return; + } + + // If at any moment, sum becomes greater than target, we don't need to proceed further + + if(sum > target) return ; + + // If we reach the end of arr[], we cannot go any further so we return back + + if(i == n) return; + + // Include the i-th Element into our Subset & Sum + + + subSet.push_back(arr[i]) ; + sum += arr[i] ; + + // Ask recursion to do rest of the task + help(i + 1, arr, n, subSet, powerSet, sum, target) ; + + // Backtrack and undo the change we have done + + subSet.pop_back(); + sum -= arr[i]; + + // Use the While Loop to skip all the duplicate occurrences of i-th Element + while(i + 1 < arr.size() && arr[i] == arr[i + 1]) i++ ; + + // Don't pick the i-th Element and ask recursion to do rest of the task + + help(i + 1, arr, n, subSet, powerSet, sum, target) ; + + +} + + +public: + vector> combinationSum2(vector& candidates, int target) { + + vector subSet ; + vector> powerSet ; + int sum = 0; + int n = candidates.size() ; + sort(candidates.begin(), candidates.end()) ; + + help(0, candidates, n, subSet, powerSet, sum, target) ; + + return powerSet ; +} +}; diff --git a/firstMissingPositiveNo.cpp b/firstMissingPositiveNo.cpp new file mode 100644 index 0000000..21318f7 --- /dev/null +++ b/firstMissingPositiveNo.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int firstMissingPositive(vector& nums) { + int n=nums.size(); + vector arr(n+1,0); + for(int i=0;i0 && nums[i]<=n)arr[nums[i]]=1; + for(int i=1;i<=n;i++){ + if(arr[i]==0)return i; + } + return n+1; + } +}; diff --git a/trappingRainWater.cpp b/trappingRainWater.cpp new file mode 100644 index 0000000..aaa370b --- /dev/null +++ b/trappingRainWater.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int trap(vector& height) { + int n=height.size(); + int left=0,right=n-1,sum=0,mleft=0,mright=0; + for(int i=0;i=mleft?mleft=height[left]:sum+=(mleft-height[left]); + left++; + } + else{ + height[right]>=mright?mright=height[right]:sum+=(mright-height[right]); + right--; + } + } + return sum; + } +};