Skip to content

Commit e5c4294

Browse files
authored
Add files via upload
1 parent 8d04ac2 commit e5c4294

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2272
-0
lines changed

LC_1.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int,int>mp;
5+
vector<int>v;
6+
int i,len=nums.size();
7+
for(i=0;i<len;i++)
8+
{
9+
if(mp.find(target-nums[i])!=mp.end())
10+
{
11+
v.push_back(mp[target-nums[i]]);
12+
v.push_back(i);
13+
return v;
14+
}
15+
mp[nums[i]]=i;
16+
}
17+
return v;
18+
}
19+
};

LC_1018.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
vector<bool> prefixesDivBy5(vector<int>& A) {
4+
vector<bool>res;
5+
if(A.size()==0)
6+
return res;
7+
long long int i,sz=A.size(),val=A[0],con=0;
8+
if(val%5==0)
9+
res.push_back(true);
10+
else
11+
res.push_back(false);
12+
for(i=1;i<sz;i++)
13+
{
14+
val = (2*val + A[i])%5;
15+
if(val==0)
16+
res.push_back(true);
17+
else
18+
res.push_back(false);
19+
}
20+
return res;
21+
}
22+
};

LC_1021.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
string removeOuterParentheses(string S) {
4+
int i,len=S.length(),left=0,flag=0;
5+
string res="",ans="";
6+
for(i=0;i<len;i++)
7+
{
8+
if((S[i]=='(')&&(flag==0))
9+
{
10+
left++;
11+
flag=1;
12+
}
13+
else if(S[i]=='(')
14+
{
15+
res=res+S[i];
16+
left++;
17+
}
18+
else if(S[i]==')')
19+
{
20+
left--;
21+
if(left>0)
22+
res=res+')';
23+
else if(left==0)
24+
{
25+
flag=0;
26+
if(res!="")
27+
ans=ans+res;
28+
res="";
29+
}
30+
}
31+
}
32+
return ans;
33+
}
34+
};

LC_1029.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int twoCitySchedCost(vector<vector<int>>& costs) {
4+
int i,len=costs.size(),sum=0;
5+
vector<int>saving(len);
6+
for(i=0;i<len;i++)
7+
{
8+
sum=sum+costs[i][0];
9+
saving[i]=costs[i][1]-costs[i][0];
10+
}
11+
sort(saving.begin(),saving.end());
12+
for(i=0;i<saving.size()/2;i++)
13+
sum=sum+saving[i];
14+
return sum;
15+
}
16+
};

LC_1037.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
bool isBoomerang(vector<vector<int>>& points) {
4+
int x1=points[0][0],y1=points[0][1],x2=points[1][0],y2=points[1][1],x3=points[2][0],y3=points[2][1];
5+
double area = (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))*0.5;
6+
7+
return area!=0;
8+
}
9+
};

LC_1042.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
vector<int> gardenNoAdj(int N, vector<vector<int>>& paths) {
4+
vector<vector<int>>graph(N+1);
5+
vector<int>options = {1,2,3,4};
6+
int i,sz=paths.size(),src,parent,neighbor,col,j,k;
7+
bool found;
8+
for(i=0;i<sz;i++)
9+
{
10+
graph[paths[i][0]-1].push_back(paths[i][1]-1);
11+
graph[paths[i][1]-1].push_back(paths[i][0]-1);
12+
}
13+
14+
vector<int>res(N,-1);
15+
for(i=0;i<N;i++)
16+
{
17+
for(k=0;k<4;k++)
18+
{
19+
col = options[k];
20+
found=false;
21+
for(j=0;j<graph[i].size();j++)
22+
{
23+
if((res[graph[i][j]]!=-1) && (res[graph[i][j]]==col))
24+
found=true;
25+
}
26+
if(!found)
27+
{
28+
res[i]=col;
29+
break;
30+
}
31+
}
32+
}
33+
return res;
34+
}
35+
};

LC_1089.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
void duplicateZeros(vector<int>& arr) {
4+
int i,j,sz=arr.size();
5+
for(i=0;i<sz;)
6+
{
7+
if(arr[i]==0)
8+
{
9+
for(j=sz-2;j>=i;j--)
10+
arr[j+1]=arr[j];
11+
i=i+2;
12+
}
13+
else
14+
i++;
15+
}
16+
}
17+
};

LC_1100.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int numKLenSubstrNoRepeats(string S, int K) {
4+
int i,len=S.length(),j,con=0;
5+
if(len<K)
6+
return con;
7+
unordered_map<char,int>mp;
8+
for(i=0;i<K;i++)
9+
mp[S[i]]++;
10+
if(mp.size()==K)
11+
con++;
12+
for(i=K;i<len;i++)
13+
{
14+
mp[S[i-K]]--;
15+
if(mp[S[i-K]]==0)
16+
mp.erase(S[i-K]);
17+
mp[S[i]]++;
18+
if(mp.size()==K)
19+
con++;
20+
}
21+
return con;
22+
}
23+
};

LC_1109.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
4+
int i,len=bookings.size(),x,y;
5+
vector<int>ans(n,0);
6+
for(i=0;i<len;i++)
7+
{
8+
x=bookings[i][0]-1;
9+
y=bookings[i][1]-1;
10+
ans[x] += bookings[i][2];
11+
if(y+1<n)
12+
ans[y+1] -= bookings[i][2];
13+
}
14+
for(i=1;i<n;i++)
15+
ans[i] += ans[i-1];
16+
return ans;
17+
}
18+
};

LC_121.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int i,len=prices.size(),buy,sell=INT_MIN,currentprofit,maxprofit=INT_MIN;
5+
if(len<=1)
6+
return 0;
7+
buy=prices[0];
8+
for(i=1;i<len;i++)
9+
{
10+
sell=prices[i];
11+
currentprofit=sell-buy;
12+
maxprofit=max(maxprofit,currentprofit);
13+
buy=min(buy,prices[i]);
14+
}
15+
16+
return maxprofit<0? 0:maxprofit;
17+
}
18+
};

0 commit comments

Comments
 (0)