Contribute Coding Problems
Submit quality problems and earn at least $20 per accepted submission.
def f(x):
return x*2 # idk
# fix laterdef test():
assert is_palindrome("A man, a plan, a canal: Panama") == True
assert is_palindrome("race a car") == FalseBackground
In trading, the bid is the highest price someone is willing to buy at. Suppose you observe a stream of bid prices over time.
Requirements
Given a list of bid prices, determine the maximum profit you could achieve by buying at one time and selling at a later time.
If no profit is possible, return 0.
Examples
max_profit([10, 7, 5, 8, 11, 9]) # 6
max_profit([10, 9, 8, 7]) # 0Track the minimum price seen so far and compute the profit at each step. Update the maximum profit accordingly.
def max_profit(prices: list[int]) -> int:
min_price = float('inf')
max_profit = 0
for price in prices:
min_price = min(min_price, price)
max_profit = max(max_profit, price - min_price)
return max_profitdef max_profit(prices: list[int]) -> int:
# Your code here
pass# The user's starter code is appended to this test-driver.
# Test cases are passed in as strings.
def main(testCase: str):
if testCase == "1,2,3":
expected = 2
result = max_profit(
. list(map(int, testCase.split(","))))
print(result == expected, end="")
else:
print("Unknown test!", end="")
if __name__ == '__main__':
if len(sys.argv) > 1:
testCase = (sys.argv[1])
main(testCase)# Additional example usages
result = max_profit([0, 0, 0, 3]) # 3Input
Output
Input
Output
What makes a great submission?
Write problems that teach a concept, not just test memorization. The best submissions have descriptions that mirror real interview phrasing, edge cases in the test suite, and explanations that walk through the reasoning step by step.