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
Nafiz Hasan
UWindsor Email
[email protected]
Application Form
Briefly explain how your solution works and how to run it
My solution...
works by doing one pass of the array where we take note of the lowest price so far and compare it to every price after that until we find the largest difference between our lowest and the highest. To calculate this difference we will subtract the current price by our current lowest. We store our current max value in maxProfit and keep going through the array comparing our current maxProfit to the current difference, trying to find a larger maxProfit, until we reach the end of the array. If we do find a higher profit, that will be our new maxProfit. Because we can not go back in the array to use previous values, we only need one pass. The maxProfit has a default of 0.
To parse the input, we take in the line and split it based on spaces, and add the elements to a String array. We then go through that array and convert each element into an int, then add it to our stocks arraylist, that we use to calculate the max profit.
The running time of this program will be O(n). This is because in the max profit function we go through a for loop that is length n once, with all other operations being O(1). When parsing the input we use split which is O(n), and a for loop to add to our arraylist which is also O(n). together we have O(3n) which simplifies to O(n).
To run it, enter numbers separated by spaces and a new line as per the input instructions. The output will be printed to the console.