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. + 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 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& 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& 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 diff --git a/C++/Permutation_Sequence.cpp b/C++/Permutation_Sequence.cpp new file mode 100644 index 0000000..c613e9a --- /dev/null +++ b/C++/Permutation_Sequence.cpp @@ -0,0 +1,29 @@ +// https://leetcode.com/problems/permutation-sequence/ +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 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(); + */ 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'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 { + 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('') +} 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 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 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 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