Ahmad Munim's Solution to CS Games Team B Question #12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.