Skip to content

Commit c0b75a3

Browse files
Merge pull request #113 from bhadurajbala22/Leetcode_solutions
Create Two Sum
2 parents 04087f9 + 38781ea commit c0b75a3

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Best time to buy and Sell Stock

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
5+
int n=prices.size();
6+
int max_profit=0;
7+
int profit=0;
8+
int min=INT_MAX;
9+
10+
11+
for(int i=0;i<n;i++)
12+
{
13+
//for finding the minimum value
14+
15+
if(min>prices[i])
16+
{
17+
min=prices[i];
18+
19+
}
20+
//check profit
21+
profit=prices[i]-min;
22+
23+
//check if profit is greater then maximum profit or not
24+
if(max_profit<profit)
25+
{
26+
max_profit=profit;
27+
}
28+
}
29+
return max_profit;
30+
}
31+
};

Two Sum

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution
2+
{
3+
public:
4+
vector<int> twoSum(vector<int>& nums, int target)
5+
{
6+
vector<int> ans;
7+
int n=nums.size();
8+
9+
for(int i=0;i<n-1;i++)
10+
{
11+
12+
for(int j=i+1;j<n;j++)
13+
{
14+
if(target==nums[i]+nums[j])
15+
16+
{
17+
18+
ans.push_back(i);
19+
ans.push_back(j);
20+
21+
}
22+
}
23+
24+
}
25+
26+
return ans;
27+
28+
}
29+
30+
31+
};

0 commit comments

Comments
 (0)