[BUG] - <title>
vijayakanthang opened this issue · comments
LeetCode Username
Vijayakanthan
Problem Number, Title, and Link
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
Bug Category
Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases)
Bug Description
There is a missing test case in the problem. In this problem we have to return the maximum possible profit. but in one of my test case it didnot work as intented.
TestCase: [1,4,5,1,3,10,99]
Language Used for Code
Python/Python3
Code used for Submit/Run operation
class Solution:
def maxProfit(self, s: List[int]) -> int:
x=0
for i in range(len(s)-1):
if s[i]<s[i+1]:
x+=(s[i+1]-s[i])
return x
Expected behavior
.
Screenshots
Additional context
No response
Winston Tang commented: Dear Vijayakanthan,
Thank you for contacting LeetCode support! With respect to your issue, I believe there might be a misunderstanding of the problem statement or the functioning of your own code.
The problem "Best Time to Buy and Sell Stock II" aims to find the maximum possible profit, where you can complete as many transactions as you like. This implies that you can buy one and sell one share of the stock multiple times. According to your code, each time a subsequent day is found with a higher price, it accumulates the profit by selling on that day, which fits perfectly with the problem statement.
For the given test case [1,4,5,1,3,10,99], your code correctly returns the expected profit of 103, where:
- Buying at 1 and selling at 5 nets (5-1)=4
- Buying at 1 and selling at 3 nets (3-1)=2
- Buying at 3 and selling at 10 nets (10-3)=7
- Buying at 10 and selling at 99 nets (99-10)=89
Adding these together, the total is 4+2+7+89=102. Hence your code is working correctly.
Please revisit the problem statement and the intended functioning of your code. If you continue to have misunderstandings or difficulties, we recommend reviewing the problem's discussion forum for additional explanations and solutions submitted by other users.
Best regards,
LeetCode Support Team