Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions Flipkart/Q2-Best Time To Sell Stock
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,20 @@ Explanation: In this case, no transaction is done, i.e. max profit = 0.
Constraints:
1 <= prices.length <= 105
0 <= prices[i] <= 105

Solution:
int maxProfit(vector<int>& prices) {
int minprice=INT_MAX;
int maxprofit=INT_MIN;
for(int i=0;i<prices.size();i++){
if(prices[i]<minprice){
minprice=prices[i];
}
int todaysprofit=prices[i]-minprice;
if(todaysprofit>maxprofit){
maxprofit=todaysprofit;
}
}
return maxprofit;
}

18 changes: 18 additions & 0 deletions Visa/Q11-Two Sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,21 @@ Constraints:
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.

Solution:
vector<int> twoSum(vector<int>& nums, int target)
{
//created a map to store numbers with their indices
unordered_map<int, int> mp;

for(int i = 0; i < nums.size(); i++){
if(mp.find(target - nums[i]) == mp.end())
mp[nums[i]] = i;
else
return {mp[target - nums[i]], i};
}
//returning {-1,-1} if not found
return {-1, -1};
}

Time complexity: O(n)