← 返回 uber 的题目列表Best Time to Buy and Sell Stock
类型:qbank
Given an array of daily prices, choose one day to buy and a later day to sell to maximize profit, returning 0 if no profitable transaction exists. Used as a phone-screen warm-up, with follow-ups exploring multiple solution approaches.
Best Time to Buy and Sell Stock
Given an array of daily prices, choose one day to buy and a later day to sell to maximize profit, returning 0 if no profitable transaction exists. Used as a phone-screen warm-up, with follow-ups exploring multiple solution approaches.
SWE
array
greedy
dp
easy
Frequency
Low
Last asked
2026-03-06
Stage
phone-screen
Best Time to Buy and Sell Stock
You are given an integer array prices where prices[i] is the price of NeetCoin on the ith day.
You may choose a single day to buy one NeetCoin and choose a different day in the future to sell it.
Return the maximum profit you can achieve. You may choose to not make any transactions, in which case the profit would be 0.
Examples
Example 1:
Input: prices = [10,1,5,6,7,1]
Output: 6
Explanation:
Buy prices[1] and sell prices[4], profit = 7 - 1 = 6.
Example 2:
Input: prices = [10,8,7,5,2]
Output: 0
Explanation:
No profitable transactions can be made, thus the max profit is 0.
Constraints
1 <= prices.length <= 100
0 <= prices[i] <= 100
Notes
Follow-up discussion was fairly rich, including solving this problem with different approaches.