-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.cpp
158 lines (142 loc) · 3.07 KB
/
day5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
1)//buy and sell stock
class Solution {
public:
int maxProfit(vector<int>& prices) {
int mini=prices[0];
int maxprofit=0;
int n=prices.size();
for(int i=1;i<n;i++)
{
int cost=prices[i]-mini;
maxprofit=max(maxprofit,cost);
mini=min(mini,prices[i]);
}
return maxprofit;
}
};
2)//Kadanes Algo
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int csum=nums[0];
int osum=nums[0];
for(int i=1;i<nums.size();i++)
{
if(csum>=0)
csum+=nums[i];
else
csum=nums[i];
if(csum>osum)
osum=csum;
}
return osum;
}
};
3)//Next Permutation
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int n=nums.size();
int ind=-1;
for(int i=n-2;i>=0;i--)
{
if(nums[i+1]>nums[i])
{
ind=i;
break;
}
}
if(ind==-1)
{
reverse(nums.begin(),nums.end());
return;
}
for(int i=n-1;i>ind;i--)
{
if(nums[i]>nums[ind])
{
swap(nums[i],nums[ind]);
break;
}
}
reverse(nums.begin()+ind+1,nums.end());
}
};
4)//Sort 0s,1s and 2s
class Solution {
public:
void sortColors(vector<int>& nums) {
int n=nums.size();
int low=0,mid=0,high=n-1;
while(high>=mid)
{
if(nums[mid]==0)
{
swap(nums[low],nums[mid]);
low++;
mid++;
}
else if(nums[mid]==1)
{
mid++;
}
else
{
swap(nums[mid],nums[high]);
high--;
}
}
}
};
5)//Pascal Triangle
class Solution {
public:
vector<vector<int>> generate(int numRows)
{
vector<vector<int>>ans;
vector<int>temp ={1};
ans.emplace_back(temp);
for(int i =1;i<numRows;i++)
{
vector<int>v={1};
int res =1;
for(int j =0;j<i;j++)
{
res *=(i-j);
res/=(j+1);
v.push_back(res);
}
ans.emplace_back(v);
}
return ans;
}
};
6)Set Matrix zeros
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m=matrix.size();
int n=matrix[0].size();
int col0=1;
for(int i=0;i<m;i++)
{
if(matrix[i][0]==0)
col0=0;
for(int j=1;j<n;j++)
{
if(matrix[i][j]==0)
matrix[i][0]=matrix[0][j]=0;
}
}
for(int i=m-1;i>=0;i--)
{
for(int j=n-1;j>=1;j--)
{
if(matrix[i][0]==0 || matrix[0][j]==0)
matrix[i][j]=0;
}
if(col0==0)
matrix[i][0]=0;
}
}
};