Coders Crushby Napplied AI
Back to DSA Problems
EasySliding Window

Best Time to Buy and Sell Stock

Find max profit from one transaction

Solution Approach

Make locally optimal choices at each step. Choose the best option available at current state without reconsidering past choices. Works when problem has optimal substructure.

Implementation
def canJump(nums):
    max_reach = 0
    for i in range(len(nums)):
        if i > max_reach:
            return False
        max_reach = max(max_reach, i + nums[i])
        if max_reach >= len(nums) - 1:
            return True
    return False
Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)
Key Learning Points
Local optimal choicesNo backtracking neededTrack maximum reachable index
Related Problems to Practice
Jump GameJump Game IIBest Time to Buy Stock
Complexity
Time:O(n)
Space:O(1)
Asked at
GoogleAmazonMicrosoft
Coders Crushby Napplied AI

The ultimate interview preparation platform. Master System Design, DSA, and tackle community challenges to crush your FAANG interviews.

Looking for jobs? Visit Napplied AI Jobs Search Agent

System Design

  • All Problems
  • Easy
  • Hard

DSA

  • All Problems
  • Dynamic Programming
  • Graphs

More

  • Problems Arena
  • Growth Paths
  • My Crush

Coders Crush by Napplied AI - Tech Interview & Coding Should Be Effortless