Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ahmad Munim's Solution to CS Games Team B Question #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ahmadmunim
Copy link

CS Games 2023 Team B Application

PLEASE FILL IN THE FOLLOWING!

Full Name

Ahmad Munim

UWindsor Email

[email protected]

Application Form

Briefly explain how your solution works and how to run it

I have two for-loops in my function. The first loop iterates over the entire array to find the minimum value which represents the price of the stock to buy which is stored in the variable 'min'. Now, the second for loop execute if and only if 'min' is not the last element of the array. This is because there is no maximum value succeeding 'min' if 'min' is the last element of the array. Hence, 0 is returned if that's the case indicating that there's no case for maximum profit.

If the minimum value is not the last element, then the second for loop iterates from the index of the 'min' to the end of the array where the loop finds the maximum value. This is because the goal of this algorithm is to find the maximum profit. So it only makes sense to find the maximum value that succeeds the minimum value. Another justification is that the array's indexes represent the day number. You can only sell a stock if you've bought a stock in the past. The maximum value is stored in the variable 'max' and the difference between 'max' and 'min' is returned which is equivalent to the maximum profit.

The worst-case runtime of this algorithm is O(2n) where the minimum value is the first element of the array. This implies that the second for loop will also iterate over the entire array since the minimum value is the first element. Note that I mentioned that the second for-loop iterates from the index of the minimum value to the end of the array.

The best-case runtime of this algorithm is O(n) where the minimum value is the last element of the given array. This implies that the second for-loop will not execute. Note that I mentioned that the second for-loop executes if and only if 'min' is not the last element. So the first for-loop will execute which iterates over the entire array to find the minimum value.

To run this program, you simply need python installed on your computer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant