From f8b4aab113d1dbc6b592384745fab112dc28a81d Mon Sep 17 00:00:00 2001 From: Deeksha Agrawal Date: Fri, 1 Oct 2021 16:15:44 +0530 Subject: [PATCH 01/17] Added Permutation_Sequence.cpp --- C++/Permutation_Sequence.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 C++/Permutation_Sequence.cpp diff --git a/C++/Permutation_Sequence.cpp b/C++/Permutation_Sequence.cpp new file mode 100644 index 0000000..076eeff --- /dev/null +++ b/C++/Permutation_Sequence.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + string getPermutation(int n, int k) { + vector chck(n+1,false); + vector fac={1,1,2,6,24,120,720,5040,40320,362880}; + string ans; + int len=0; + while(len!=n) + { + int x=(k-1)/fac[n-len-1]; + k-=fac[n-len-1]*x; + int count=0; + for(int i=1;i<=n;i++) + { + if(chck[i]==false) + count++; + if(count==(x+1)) + { + chck[i]=true; + ans+=i+48; + len++; + break; + } + } + } + return ans; + } +}; \ No newline at end of file From 69c8c76e4dc390b1a92f98a2e92892535c53d8a3 Mon Sep 17 00:00:00 2001 From: Deeksha Agrawal Date: Fri, 1 Oct 2021 16:17:03 +0530 Subject: [PATCH 02/17] Added link for the question --- C++/Permutation_Sequence.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/C++/Permutation_Sequence.cpp b/C++/Permutation_Sequence.cpp index 076eeff..c613e9a 100644 --- a/C++/Permutation_Sequence.cpp +++ b/C++/Permutation_Sequence.cpp @@ -1,3 +1,4 @@ +// https://leetcode.com/problems/permutation-sequence/ class Solution { public: string getPermutation(int n, int k) { From 02184267d3ccbf04a37b791d013552a56b4f333c Mon Sep 17 00:00:00 2001 From: Deeksha Agrawal Date: Fri, 1 Oct 2021 16:41:02 +0530 Subject: [PATCH 03/17] Valid_Number.cpp --- C++/Valid_Number.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 C++/Valid_Number.cpp diff --git a/C++/Valid_Number.cpp b/C++/Valid_Number.cpp new file mode 100644 index 0000000..cf08450 --- /dev/null +++ b/C++/Valid_Number.cpp @@ -0,0 +1,63 @@ +//https://leetcode.com/problems/valid-number +class Solution { +public: + bool isNumber(string s) { + int n=s.length(); + if(n==0) + return false; + if(n==1&&(s[0]<'0'||s[0]>'9')) + return false; + int i=0; + while(s[i]==' '&&i=0) + n--; + if(s[i]=='+'||s[i]=='-') + i++; + if(i==n) + return false; + bool chcke=false; + bool chckdec=false; + if(s[i]=='e') + return false; + if(s[i]=='.'&&i+1='0'&&s[i+1]<='9') + { + i++; + chckdec=true; + } + for(i;i='0'&&s[i]<='9') + { + if(i+1 Date: Fri, 1 Oct 2021 18:10:53 +0530 Subject: [PATCH 04/17] Added code to - single element in sorted array problem in python --- Python/singleElementInSortedArray.py | 69 ++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Python/singleElementInSortedArray.py diff --git a/Python/singleElementInSortedArray.py b/Python/singleElementInSortedArray.py new file mode 100644 index 0000000..e588c0a --- /dev/null +++ b/Python/singleElementInSortedArray.py @@ -0,0 +1,69 @@ +# https://leetcode.com/problems/single-element-in-a-sorted-array/ + +# Algorithm - Binary Search + +# apply binary search +# if mid is a non extreme element (mid!=0 and mid!=len(arr)) => arr[mid]!=arr[mid-1] and arr[mid]!=arr[mid+1] =>return arr[mid] +# if mid==0, if arr[mid] != arr[mid +1] => return arr[mid] +# if mid==n-1, if arr[mid] != arr[mid-1] => return arr[mid] +# if mid is even, move to the side of the element same as mid +# if mid is odd, move to side of the element different from mid + +class Solution: + def singleNonDuplicate(self, nums) -> int: + + # n represents the size of the array + n = len(nums) + + # if the size of the array is 1, the only element available is our answer + if n == 1: + return nums[0] + + # initially start is the first index of the array + start = 0 + + # initially end is the last index of the array + end = n - 1 + + while start <= end: + mid = start + (end-start)//2 + + # if mid is the start index and if element at mid is different from element at mid+1 + # then element at mid is our answer + if mid == 0 and nums[mid]!=nums[mid+1]: + return nums[mid] + + # if mid is the last index of the array and if element at mid is different from element at mid-1 + # then element at mid is our answer + if mid == n - 1 and nums[mid] != nums[mid-1]: + return nums[mid] + + # if mid is an index somewhere in the middle of the array + # if element at mid is different from elements at mid-1 and mid+1 + # then mid is our answer + if nums[mid] != nums[mid-1] and nums[mid] != nums[mid+1]: + return nums[mid] + + # now we have cases to move the start and end pointers + + # if mid is even - even number of elements to the left of mid + if mid%2 == 0: + # if element at mid is equal to element at mid+1 + # ans will lie to the right of mid + # start = mid + 1 + # else end = mid - 1 + if nums[mid] == nums[mid+1]: + start = mid + 1 + else: + end = mid - 1 + + # if mid is odd - odd number of elements to the left of mid + else: + # if element at mid is not equal to element at mid-1 + # then our ans lies to the right side of mid + # so start = mid + 1 + # else end = mid - 1 + if nums[mid] != nums[mid + 1]: + start = mid + 1 + else: + end = mid - 1 \ No newline at end of file From e03effbd934f87e141a72f9eabbe016c169b2c51 Mon Sep 17 00:00:00 2001 From: Subramani Srinivasan Date: Fri, 1 Oct 2021 19:58:18 +0530 Subject: [PATCH 05/17] Added Problem 180 | Word Break 2 | Python --- Python/140_Word break_II.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Python/140_Word break_II.py diff --git a/Python/140_Word break_II.py b/Python/140_Word break_II.py new file mode 100644 index 0000000..39c8b1e --- /dev/null +++ b/Python/140_Word break_II.py @@ -0,0 +1,36 @@ +""" +Link : https://leetcode.com/problems/word-break-ii/ + +140. Word Break II (Hard) + +Given a string s and a dictionary of strings wordDict, +add spaces in s to construct a sentence where each word is a valid dictionary word. +Return all such possible sentences in any order. + +Note that the same word in the dictionary may be reused multiple times in the segmentation. +""" + +class Solution: + + def __init__(self): + self.dp = {} + + def wordBreak(self, s: str, d: List[str]) -> bool: + + res = [] + if s in d: + res.append(s) + + for i in range(1,len(s)): + if s[:i] in d: + t1 = s[:i] + t2 = s[i:] + if t2 in self.dp: + tt = self.dp[t2] + else: + tt = self.wordBreak(t2, d) + self.dp[t2] = tt + for k in tt: + res.append(t1 + " " + k) + + return res \ No newline at end of file From 6e28a2f5c44059341c919c7bd45884a76067f735 Mon Sep 17 00:00:00 2001 From: Koddi-Evangelista Date: Sat, 2 Oct 2021 16:08:44 +0800 Subject: [PATCH 06/17] feat: add multiply strings algorithm using javascript --- Javascript/Multiply_Strings.js | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Javascript/Multiply_Strings.js diff --git a/Javascript/Multiply_Strings.js b/Javascript/Multiply_Strings.js new file mode 100644 index 0000000..5986162 --- /dev/null +++ b/Javascript/Multiply_Strings.js @@ -0,0 +1,39 @@ +// problem link: https://leetcode.com/problems/divide-two-integers/ + +// Runtime: 92 ms, faster than 66.27% of JavaScript online submissions for Multiply Strings. + +const multiply = (num1, num2) => { + let n1 = num1 + let n2 = num2 + + if (parseInt(n1) === 0 || parseInt(n2) === 0) { + return '0' + } + + n1 = n1.split('').reverse() + n2 = n2.split('').reverse() + const result = [] + + for (let i = 0; n1[i] >= 0; i++) { + for (let j = 0; n2[j] >= 0; j++) { + if (!result[i + j]) { + result[i + j] = 0 + } + + result[i + j] += n1[i] * n2[j] + } + } + + for (let i = 0; result[i] >= 0; i++) { + if (result[i] >= 10) { + if (!result[i + 1]) { + result[i + 1] = 0 + } + + result[i + 1] += parseInt(result[i] / 10) + result[i] %= 10 + } + } + + return result.reverse().join('') +} From 54465308c3a36c89d5637ab5627aab978405cebe Mon Sep 17 00:00:00 2001 From: Deeksha Agrawal Date: Sat, 2 Oct 2021 14:06:10 +0530 Subject: [PATCH 07/17] Deleted Extra file --- C++/Valid_Number.cpp | 63 -------------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 C++/Valid_Number.cpp diff --git a/C++/Valid_Number.cpp b/C++/Valid_Number.cpp deleted file mode 100644 index cf08450..0000000 --- a/C++/Valid_Number.cpp +++ /dev/null @@ -1,63 +0,0 @@ -//https://leetcode.com/problems/valid-number -class Solution { -public: - bool isNumber(string s) { - int n=s.length(); - if(n==0) - return false; - if(n==1&&(s[0]<'0'||s[0]>'9')) - return false; - int i=0; - while(s[i]==' '&&i=0) - n--; - if(s[i]=='+'||s[i]=='-') - i++; - if(i==n) - return false; - bool chcke=false; - bool chckdec=false; - if(s[i]=='e') - return false; - if(s[i]=='.'&&i+1='0'&&s[i+1]<='9') - { - i++; - chckdec=true; - } - for(i;i='0'&&s[i]<='9') - { - if(i+1 Date: Sat, 2 Oct 2021 14:21:48 +0530 Subject: [PATCH 08/17] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4dfd472..2893a88 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,4 +28,4 @@ jobs: # GITHUB_TOKEN secret repo-token: ${{ secrets.GITHUB_TOKEN }} # Message to comment - message: Please link your PR with the issue assigned to it, otherwise it will not be reviewed + message: Please link your PR with the issue, otherwise it will not be reviewed. If this PR was created for an Issue not assigned to you, it might be marked invalid From 1d33d9831721396922cf442f11558d3d8fb8e24a Mon Sep 17 00:00:00 2001 From: Raman <61423761+noisefilter19@users.noreply.github.com> Date: Sat, 2 Oct 2021 14:28:47 +0530 Subject: [PATCH 09/17] Create IssueComment.yml --- .github/workflows/IssueComment.yml | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/IssueComment.yml diff --git a/.github/workflows/IssueComment.yml b/.github/workflows/IssueComment.yml new file mode 100644 index 0000000..7d1cef2 --- /dev/null +++ b/.github/workflows/IssueComment.yml @@ -0,0 +1,34 @@ +# This is a basic workflow to help you get started with Actions + +name: CI + +# Controls when the workflow will run +on: + # Triggers the workflow on push or pull request events but only for the main branch + push: + branches: [ main ] + pull_request: + branches: [ main ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + - name: Issue comment + # You may pin to the exact commit or the version. + # uses: kyoncy/issue-comment-actions@1b1e351bee044874171a5dc99598257425f6272c + uses: kyoncy/issue-comment-actions@v1 + with: + # GitHub token for use by this action. + token: ${{ secrets.GITHUB_TOKEN }} + # Message + message: Thankyou, for opening the issue. Please wait for the issue to be assigned to you and fo not open anymore issues until this is resolved. + From 7c21c984a292acabe3de8d59bdbd0fdb4ce67303 Mon Sep 17 00:00:00 2001 From: Deeksha Agrawal Date: Sat, 2 Oct 2021 15:02:07 +0530 Subject: [PATCH 10/17] Added Valid_Number.cpp --- C++/Valid_Number.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 C++/Valid_Number.cpp diff --git a/C++/Valid_Number.cpp b/C++/Valid_Number.cpp new file mode 100644 index 0000000..cf08450 --- /dev/null +++ b/C++/Valid_Number.cpp @@ -0,0 +1,63 @@ +//https://leetcode.com/problems/valid-number +class Solution { +public: + bool isNumber(string s) { + int n=s.length(); + if(n==0) + return false; + if(n==1&&(s[0]<'0'||s[0]>'9')) + return false; + int i=0; + while(s[i]==' '&&i=0) + n--; + if(s[i]=='+'||s[i]=='-') + i++; + if(i==n) + return false; + bool chcke=false; + bool chckdec=false; + if(s[i]=='e') + return false; + if(s[i]=='.'&&i+1='0'&&s[i+1]<='9') + { + i++; + chckdec=true; + } + for(i;i='0'&&s[i]<='9') + { + if(i+1 Date: Sat, 2 Oct 2021 15:10:46 +0530 Subject: [PATCH 11/17] Added Text_Justification.cpp --- C++/Text_Justification.cpp | 109 +++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 C++/Text_Justification.cpp diff --git a/C++/Text_Justification.cpp b/C++/Text_Justification.cpp new file mode 100644 index 0000000..043c3a5 --- /dev/null +++ b/C++/Text_Justification.cpp @@ -0,0 +1,109 @@ +//https://leetcode.com/problems/text-justification/ +#define ff first +#define ss second +class Solution { +public: + vector fullJustify(vector& words, int maxWidth) { + int n=words.size(); + if(n==1) + { + vector ans(1); + ans[0]+=words[0]; + while(ans[0].length()!=maxWidth) + ans[0]+=" "; + return ans; + } + vector> len(n); + len[0].first=words[0].length(); + len[0].second=len[0].first; + int i; + for(i=1;i ans(1); + ans[0]+=words[0]; + for(int i=1;i> help(1); + int check=0; + help[0].ff=0; + int j=0; + for(i=0;i ans(j,""); + int sum=len[help[0].ss].ss; + int space=help[0].ss; + i=0; + while(i!=help[0].ss) + { + ans[0]+=words[i]; + i++; + int xx=maxWidth-sum; + int j; + for(j=0;j Date: Sat, 2 Oct 2021 20:22:37 +0530 Subject: [PATCH 12/17] Added code for First and Last position of element in a sorted array problem --- ...rstAndLastIndexOfAnElementInSortedArray.py | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Python/firstAndLastIndexOfAnElementInSortedArray.py diff --git a/Python/firstAndLastIndexOfAnElementInSortedArray.py b/Python/firstAndLastIndexOfAnElementInSortedArray.py new file mode 100644 index 0000000..7a55f48 --- /dev/null +++ b/Python/firstAndLastIndexOfAnElementInSortedArray.py @@ -0,0 +1,80 @@ +# https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ + +def searchRange(arr, target): + + n = len(arr) + + # find leftIndex + leftIndex = -1 + + # start is the first index of the array + start = 0 + + # end is the last index of the array + end = n-1 + + while(start <= end): + # mid = (start+end)//2 + # (start + end) might exceed the range of integers + + # better way to do this + mid = start + (end - start)//2 + + # if target element is equal to the middle element + if arr[mid] == target: + + # potential answer is found + leftIndex = mid + + # continue searching in left of mid + end = mid - 1 + + # target element is less than middle element + # search in the left + elif target < arr[mid]: + end = mid - 1 + + # target element is greater than middle element + # search in the right + elif target > arr[mid]: + start = mid + 1 + + + # find rightIndex + rightIndex = -1 + + # start is the first index of the array + start = 0 + + # end is the last index of the array + end = n-1 + + while(start <= end): + # mid = (start+end)//2 + # (start + end) might exceed the range of integers + + # better way to do this + mid = start + (end - start)//2 + + # if target element is equal to the middle element + if arr[mid] == target: + # potential answer is found + rightIndex = mid + + # continue searching in right of mid + start = mid + 1 + + # target element is less than middle element + # search in the left + elif target < arr[mid]: + end = mid - 1 + + # target element is greater than middle element + # search in the right + elif target > arr[mid]: + start = mid + 1 + + return [leftIndex,rightIndex] + + + \ No newline at end of file From b6a03d8713191b72ce11a1f841faf44590bd1658 Mon Sep 17 00:00:00 2001 From: ShubhaIn <61375064+ShubhaIn@users.noreply.github.com> Date: Tue, 5 Oct 2021 23:19:32 +0530 Subject: [PATCH 13/17] Create 3Sum.cpp --- C++/3Sum.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 C++/3Sum.cpp diff --git a/C++/3Sum.cpp b/C++/3Sum.cpp new file mode 100644 index 0000000..3265699 --- /dev/null +++ b/C++/3Sum.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + vector> threeSum(vector& nums) { + vector> res; + sort(nums.begin(), nums.end()); + for(int i=0;itarget){ + back--; + } + else if(sum Date: Tue, 5 Oct 2021 23:26:18 +0530 Subject: [PATCH 14/17] Create LongestIncreasingSubsequence.cpp --- C++/LongestIncreasingSubsequence.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 C++/LongestIncreasingSubsequence.cpp diff --git a/C++/LongestIncreasingSubsequence.cpp b/C++/LongestIncreasingSubsequence.cpp new file mode 100644 index 0000000..39efc6c --- /dev/null +++ b/C++/LongestIncreasingSubsequence.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int lengthOfLIS(vector& nums) { + vector> dp(nums.size()+1,vector(nums.size()+1,0)); + vector a; + vector s=nums; + sort(s.begin(),s.end()); + a.push_back(s[0]); + for(int i=1;i Date: Tue, 5 Oct 2021 23:51:33 +0530 Subject: [PATCH 15/17] Create ShuffleAnArray.cpp --- C++/ShuffleAnArray.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 C++/ShuffleAnArray.cpp diff --git a/C++/ShuffleAnArray.cpp b/C++/ShuffleAnArray.cpp new file mode 100644 index 0000000..44e67a6 --- /dev/null +++ b/C++/ShuffleAnArray.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector nums; + Solution(vector& nums) { + this->nums=nums; + } + + /** Resets the array to its original configuration and return it. */ + vector reset() { + return nums; + } + + /** Returns a random shuffling of the array. */ + vector shuffle() { + vector nw(nums); + for(int i=0;i param_1 = obj->reset(); + * vector param_2 = obj->shuffle(); + */ From 006b03e3388eb444b75545d9eb71ea48b5e68e50 Mon Sep 17 00:00:00 2001 From: Alekhya77 <54280241+Alekhya77@users.noreply.github.com> Date: Tue, 5 Oct 2021 23:54:50 +0530 Subject: [PATCH 16/17] Create reverse_words_in_a_string.py --- reverse_words_in_a_string.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 reverse_words_in_a_string.py diff --git a/reverse_words_in_a_string.py b/reverse_words_in_a_string.py new file mode 100644 index 0000000..e1c52e3 --- /dev/null +++ b/reverse_words_in_a_string.py @@ -0,0 +1,18 @@ +#problem-link: https://leetcode.com/problems/reverse-words-in-a-string/ + +''' +Problem Description: + +Given an input string s, reverse the order of the words. +A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. +Return a string of the words in reverse order concatenated by a single space. + +''' + +class Solution: + def reverseWords(self, s: str) -> str: + + sList = list(map(str, s.split())) + revList = sList[::-1] + rev = " ".join(s for s in revList) + return rev From 50307bec3c1f229d6f4f25e18a63ed99060fccc5 Mon Sep 17 00:00:00 2001 From: Omkar Joshi Date: Wed, 6 Oct 2021 11:05:27 +0530 Subject: [PATCH 17/17] Added Median of two sorted arrays --- C++/Median_of_two_sorted_arrays.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 C++/Median_of_two_sorted_arrays.cpp diff --git a/C++/Median_of_two_sorted_arrays.cpp b/C++/Median_of_two_sorted_arrays.cpp new file mode 100644 index 0000000..b16d7a6 --- /dev/null +++ b/C++/Median_of_two_sorted_arrays.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + double findMedianSortedArrays(vector& a, vector& b) { + int m=a.size(); + int n=b.size(); + if(m>n) + return findMedianSortedArrays(b,a); + int l=0,r=m; + while(l<=r){ + int partx=l+(r-l)/2; + int party=(m+n+1)/2-partx; + int maxlx=(partx==0)?INT_MIN:a[partx-1]; + int minrx=(partx==m)?INT_MAX:a[partx]; + int maxly=(party==0)?INT_MIN:b[party-1]; + int minry=(party==n)?INT_MAX:b[party]; + if(maxlx<=minry&&maxly<=minrx){ + if((m+n)%2==0) + return (double)(max(maxlx,maxly)+min(minrx,minry))/2; + else + return (double)(max(maxlx,maxly)); + }else if(maxlx>minry) + r=partx-1; + else + l=partx+1; + } + return -1.0; + } +}; \ No newline at end of file