-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAY 3 SOLUTION LEET CODE
56 lines (49 loc) · 1.05 KB
/
DAY 3 SOLUTION LEET CODE
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
//Problem 1st - Intersection of two array ||
// date 60/july/2022
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
int i=0,j=0;
vector<int> ans;
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
while(i<nums1.size() && j <nums2.size())
{
if(nums1[i]==nums2[j])
{
ans.push_back(nums1[i]);
i++;
j++;
}
else if(nums1[i]>nums2[j])
{
j++;
}
else
i++;
}
return ans;
}
};
//Problem 2-Best time buy and sell stock
class Solution {
public:
int maxProfit(vector<int>& prices) {
int lsf=INT_MAX;
int ans=0;
int p=0;
for(int i=0; i<prices.size(); i++)
{
if(prices[i] <lsf)
{
lsf=prices[i];
}
p=prices[i]-lsf;
if(ans <p)
{
ans=p;
}
}
return ans;
}
};