20%off off — LaunchSpecial
Exercises/#2 Best Time to Buy and Sell Stock
#2

Best Time to Buy and Sell Stock

Easy📦 Arrays
50 XP

Problem

Given an array prices where prices[i] is the price on day i, find the maximum profit from one buy and one sell. Return 0 if no profit is possible.

Examples

Input: prices = [7,1,5,3,6,4]
Output: 5 (buy day 2 at 1, sell day 5 at 6)

Input: prices = [7,6,4,3,1]
Output: 0
greedyarraysliding-window
AI Assistant— powered by AI

Unlock hints progressively — start gentle, go deeper only if needed.

💡

Level 1: Gentle Nudge

A subtle direction

🗺️

Level 2: Approach

The algorithm to use

🔍

Level 3: Detailed

Step-by-step guidance

2 / 150

Your Solution

⚡ JavaScript

to save progress and use AI features