Best Time To Buy And Sell Stock Solution for Accenture

Company: Accenture · Difficulty: Easy · Topics: Array, Dynamic Programming

Problem Statement

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.

Key Understanding:

Examples

Example 1:

Input: prices = [7,1,5,3,6,4]

Output: 5

Explanation:

Example 2:

Input: prices = [7,6,4,3,1]

Output: 0

Explanation:

Example 3:

Input: prices = [1,2,3,4,5]

Output: 4

Explanation:

Example 4:

Input: prices = [3,2,6,5,0,3]

Output: 4

Explanation:

Intuition

The key insight is that we need to find the maximum difference between any two prices, where the selling price comes after the buying price. We can track the minimum price seen so far and calculate potential profits.

Core Concept:

1. Buy the stock (if it's the lowest price seen so far)

2. Sell the stock (if it gives us maximum profit)

3. Do nothing

Why One Pass Works:

Mathematical Insight:

Approach

Approach 1: One Pass (Optimal)

Algorithm:

1. Initialize minPrice to the first price and maxProfit to 0

2. Iterate through prices starting from the second element

3. For each price:

4. Return maxProfit

Why This Works:

Complexity Analysis:

Approach 2: Two Pass (Finding Future Max)

Algorithm:

1. For each day, find the maximum price in the future

2. Calculate potential profit for each day

3. Return maximum profit

Why This Works:

Complexity Analysis:

Approach 3: Brute Force

Algorithm:

1. Check all possible buy-sell pairs

2. Calculate profit for each pair

3. Return maximum profit

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: prices = [7,1,5,3,6,4]

One Pass Approach:

Step 1: Initialize

Step 2: Process each price

Step 3: Return maxProfit = 5

Two Pass Approach:

Step 1: For each day, find future maximum

Step 2: Calculate profits

Step 3: Return max profit = 5

Key Insights

1. Single Pass Efficiency: We don't need to look back or forward, just track minimum

2. Greedy Nature: We always want to buy at the lowest price seen so far

3. No Need for History: We only need the minimum price, not all previous prices

4. Early Termination: If we find a profit, we can continue to look for better

5. Edge Case Handling: If no profit is possible, return 0

Edge Cases

1. Single Element: Array with one price, return 0 (can't buy and sell)

2. Decreasing Prices: All prices decrease, return 0

3. Increasing Prices: All prices increase, buy first, sell last

4. Same Prices: All prices are same, return 0

5. Large Arrays: Algorithm handles large arrays efficiently

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: One Pass (Optimal)

Time Complexity: O(n) - We traverse the array once

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 2: Two Pass

Time Complexity: O(n²) - We check future prices for each day

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 3: Brute Force

Time Complexity: O(n²) - We check all possible pairs

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

More Accenture coding questions