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

solution #1

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

solution #1

wants to merge 2 commits into from

Conversation

Nafiz1
Copy link

@Nafiz1 Nafiz1 commented Nov 21, 2022

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.

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