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

4. Median of Two Sorted Arrays | Hard | C++ #1037

Open
wants to merge 32 commits into
base: test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f8b4aab
Added Permutation_Sequence.cpp
agrawal-deeksha Oct 1, 2021
69c8c76
Added link for the question
agrawal-deeksha Oct 1, 2021
0218426
Valid_Number.cpp
agrawal-deeksha Oct 1, 2021
2db8f76
Added code to - single element in sorted array problem in python
suburban-daredevil Oct 1, 2021
e03effb
Added Problem 180 | Word Break 2 | Python
Oct 1, 2021
6e28a2f
feat: add multiply strings algorithm using javascript
Koddi-Evangelista Oct 2, 2021
1ec84eb
Merge branch 'noisefilter19:main' into main
agrawal-deeksha Oct 2, 2021
5446530
Deleted Extra file
agrawal-deeksha Oct 2, 2021
7aa7b6a
Merge branch 'main' of https://github.com/agrawal-deeksha/LeetCode_Al…
agrawal-deeksha Oct 2, 2021
bcadc69
Merge branch 'noisefilter19:main' into main
agrawal-deeksha Oct 2, 2021
58184d9
Merge pull request #945 from agrawal-deeksha/main
noisefilter19 Oct 2, 2021
7fffad4
Update main.yml
noisefilter19 Oct 2, 2021
1d33d98
Create IssueComment.yml
noisefilter19 Oct 2, 2021
cd3a4f4
Merge pull request #981 from Koddi-Evangelista/main
noisefilter19 Oct 2, 2021
7c21c98
Added Valid_Number.cpp
agrawal-deeksha Oct 2, 2021
c75145a
Added Text_Justification.cpp
agrawal-deeksha Oct 2, 2021
85679d1
Merge branch 'noisefilter19:main' into main
suburban-daredevil Oct 2, 2021
fd00e89
Added code for First and Last position of element in a sorted array p…
suburban-daredevil Oct 2, 2021
f16c32a
Merge pull request #957 from suburban-daredevil/main
noisefilter19 Oct 5, 2021
5848d48
Merge pull request #967 from SubramaniSrinivasan/Issue-895/Word_Break…
noisefilter19 Oct 5, 2021
fa50a85
Merge pull request #989 from agrawal-deeksha/935
noisefilter19 Oct 5, 2021
b6a03d8
Create 3Sum.cpp
ShubhaIn Oct 5, 2021
3ee37ce
Merge pull request #1024 from ShubhaIn/15_3SUM
noisefilter19 Oct 5, 2021
043ad90
Create LongestIncreasingSubsequence.cpp
ShubhaIn Oct 5, 2021
04d10b2
Merge pull request #1026 from ShubhaIn/main
noisefilter19 Oct 5, 2021
d3d580c
Merge pull request #994 from agrawal-deeksha/936
noisefilter19 Oct 5, 2021
97366aa
Create ShuffleAnArray.cpp
ShubhaIn Oct 5, 2021
006b03e
Create reverse_words_in_a_string.py
Alekhya77 Oct 5, 2021
44712c3
Merge pull request #1028 from ShubhaIn/main
noisefilter19 Oct 5, 2021
c3d80b4
Merge branch 'noisefilter19:main' into Alekhya77-patch-1
Alekhya77 Oct 5, 2021
54af8cd
Merge pull request #1031 from Alekhya77/Alekhya77-patch-1
noisefilter19 Oct 5, 2021
50307be
Added Median of two sorted arrays
joshi-omkar Oct 6, 2021
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
34 changes: 34 additions & 0 deletions .github/workflows/IssueComment.yml
Original file line number Diff line number Diff line change
@@ -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.

2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions C++/3Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
for(int i=0;i<nums.size();i++){
int target=-nums[i];
int front=i+1;
int back=nums.size()-1;
while(front<back){
int sum=nums[front]+nums[back];
if(sum>target){
back--;
}
else if(sum<target){
front++;
}else{
int f=nums[front];
int b=nums[back];
res.push_back({nums[i],f,b});
while(front<back&&nums[front]==f)
front++;
while(front<back&&nums[back]==b)
back--;
}
}
while(i+1<nums.size() && nums[i+1]==nums[i])
i++;
}
return res;
}
};
24 changes: 24 additions & 0 deletions C++/LongestIncreasingSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
vector<vector<int>> dp(nums.size()+1,vector<int>(nums.size()+1,0));
vector<int> a;
vector<int> s=nums;
sort(s.begin(),s.end());
a.push_back(s[0]);
for(int i=1;i<s.size();i++){
if(s[i]!=s[i-1])
a.push_back(s[i]);
}
for(int i=0;i<nums.size();i++){
for(int j=0;j<a.size();j++){
if(nums[i]==a[j]){
dp[i+1][j+1]=1+dp[i][j];
}else{
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
}
}
}
return dp[nums.size()][a.size()];
}
};
28 changes: 28 additions & 0 deletions C++/Median_of_two_sorted_arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
double findMedianSortedArrays(vector<int>& a, vector<int>& 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;
}
};
29 changes: 29 additions & 0 deletions C++/Permutation_Sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// https://leetcode.com/problems/permutation-sequence/
class Solution {
public:
string getPermutation(int n, int k) {
vector<bool> chck(n+1,false);
vector<long long> 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;
}
};
29 changes: 29 additions & 0 deletions C++/ShuffleAnArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
vector<int> nums;
Solution(vector<int>& nums) {
this->nums=nums;
}

/** Resets the array to its original configuration and return it. */
vector<int> reset() {
return nums;
}

/** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> nw(nums);
for(int i=0;i<nums.size();i++){
int pos=rand()%(nums.size()-i);
swap(nw[pos+i],nw[i]);
}
return nw;
}
};

/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(nums);
* vector<int> param_1 = obj->reset();
* vector<int> param_2 = obj->shuffle();
*/
109 changes: 109 additions & 0 deletions C++/Text_Justification.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//https://leetcode.com/problems/text-justification/
#define ff first
#define ss second
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int n=words.size();
if(n==1)
{
vector<string> ans(1);
ans[0]+=words[0];
while(ans[0].length()!=maxWidth)
ans[0]+=" ";
return ans;
}
vector<pair<int,int>> len(n);
len[0].first=words[0].length();
len[0].second=len[0].first;
int i;
for(i=1;i<n;i++)
{
len[i].first=words[i].length();
len[i].second=len[i-1].second+len[i].first;
}
if(len[i-1].ss+i-1<=maxWidth)
{
vector<string> ans(1);
ans[0]+=words[0];
for(int i=1;i<n;i++)
ans[0]+=" "+words[i];
while(ans[0].length()!=maxWidth)
ans[0]+=" ";
return ans;
}
vector<pair<int,int>> help(1);
int check=0;
help[0].ff=0;
int j=0;
for(i=0;i<words.size();i++)
{
if(check+len[i].first<=maxWidth)
{
check+=len[i].first;
check++;
}
else
{
help[j].ss=i-1;
j++;
help.push_back(make_pair(i,0));
check=len[i].first;
check++;
}
}
help[j].ss=i-1;
j++;
vector<string> 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<ceil((double)xx/(double)space);j++)
{
ans[0]+=" ";
}
space--;
sum+=(j);
}
ans[0]+=words[i];
while(ans[0].length()!=maxWidth)
ans[0]+=" ";
int ii;
for(ii=1;ii<j-1;ii++)
{
i=help[ii].ff;
sum=len[help[ii].ss].ss-len[help[ii].ff-1].ss;
space=help[ii].ss-help[ii].ff;
while(i!=help[ii].ss)
{
ans[ii]+=words[i];
i++;
int xx=maxWidth-sum;
int j;
for(j=0;j<ceil((double)xx/(double)space);j++)
{
ans[ii]+=" ";
}
space--;
sum+=(j);
}
ans[ii]+=words[i];
while(ans[ii].length()!=maxWidth)
ans[ii]+=" ";
}
ans[ii]+=words[help[ii].ff];
for(int i=help[ii].ff+1;i<n;i++)
{
ans[ii]+=" "+words[i];
}
while(ans[ii].length()!=maxWidth)
ans[ii]+=" ";
return ans;
}
};
63 changes: 63 additions & 0 deletions C++/Valid_Number.cpp
Original file line number Diff line number Diff line change
@@ -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<n)
i++;
if(i==n)
return false;
while(s[n-1]==' '&&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<n&&s[i+1]>='0'&&s[i+1]<='9')
{
i++;
chckdec=true;
}
for(i;i<n;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
if(i+1<n&&s[i+1]=='.')
{
if(chckdec==false)
chckdec=true;
else
return false;
i++;
}
}
else if(s[i]=='e')
{
if(i+1==n)
return false;
if(chcke==true)
return false;
chcke=true;
if(s[i+1]=='+'||s[i+1]=='-')
{
i++;
if(i+1==n)
return false;
}
chckdec=true;
}
else
return false;
}
return true;
}
};
Loading