Sunday, June 23, 2024
HomeData StructuresBest Time to Buy and Sell Stock - Day 7 | 100...

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

-

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called – Best Time to Buy and Sell Stock. If you want to become part of the series and want to continue on the same, refer to the following page:

100 Days of Code

Let us explore the problem statement first.

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Example 1:Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Read more about the problem here:

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

Solution:

Now let us look into the solution.

class Solution {
public int maxProfit(int[] prices) {
int profit =0;
int min = Integer.MAX_VALUE;
for(int i = 0; i< prices.length;i++) {
if(prices[i] < min) {
min = prices[i];
}
profit = Math.max(profit, prices[i] – min);
}
return profit;
}
}

Here we are initializing prices[i] to a maximum value. Next, we will be iterating through the prices array and see if we can find any other minimum value. Also within the iteration, we will calculate the maximum profit till now.

Next, we will be returning Profit after the iteration is complete.

Time and Space Complexity:

Here the time complexity is O(N) as we iterate through all the elements from an Array.

Space complexity is O(1) as we are not using any additional Data Structures.

Vamsi Tallapudi
Vamsi Tallapudi
Architect Technology at Cognizant | Full Stack Engineer | Technical Blogger | AI Enthusiast

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

3 Sum | Leetcode #15 | 100 Days of Code Day 10

Today we are going to solve 3 Sum - our first Medium Level Array problem from Coderefer DSA Sheet. If you are new to this...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Two Sum – Day 6 | 100 Days of Code

Welcome to Day 6 of 100 Days of Code where we solve the most frequently asked Easy level Array problem - Two Sum. Let us...

Embarking on a 100-day adventure: Day 5 – No.Of Occurrences In An Array

Welcome to Day 5 of our coding journey! Today, we dive deep into the world of programming to tackle a common, yet curious problem: finding...

Follow us

1,358FansLike
10FollowersFollow
397SubscribersSubscribe

Most Popular