Lecture
Finding Maximum Profit - Solution Explanation
Explore 3 ways to find the maximum profit.
Method 1
def solution(prices): max_profit = 0 for i in range(len(prices) - 1): for j in range(i + 1, len(prices)): # Buy at time i, sell at time j profit = prices[j] - prices[i] # Calculate profit max_profit = max(max_profit, profit) # Update maximum profit return max_profit
Usage Example
Input/Output Example
result = solution([7, 1, 5, 3, 6, 4]) print(result) # Output: 5
Previous lessonCoding Quiz - Maximize Stock Trading ProfitNext lessonCoding Quiz - Detecting Anagrams
Lessons in this chapter · Python Coding Quiz Practice
- 1. Coding Quiz - Palindrome Check
- 2. Checking for Palindrome Strings - Problem Solving
- 3. Coding Quiz - Find Unique Words
- 4. Finding Unique Words - Problem Solving
- 5. Coding Quiz - Caesar Cipher
- 6. Caesar Cipher - Problem Solving
- 7. Coding Quiz - Find Prime Numbers
- 8. Finding Primes - Problem Solving
- 9. Coding Quiz - Finding Indices for a Target Sum
- 10. Creating Target Value with Two-Sum - Problem Solution
- 11. Coding Quiz - Maximize Stock Trading Profit
- 12. Finding Maximum Profit - Solution Explanation
- 13. Coding Quiz - Detecting Anagrams
- 14. Anagram Checker - Problem Solution
- 15. Coding Quiz - Remove the Smallest Number
- 16. Removing the Smallest Number - Problem Solution
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help