Date: 2024-07-16
Three largest solution
- In this solution the following happens
- We define a parent function that holds the
threeLargest
array. we initially define them asNone
since we will be iterating over the input array. - Next, we create a simple for loop that loops through the input array and calls a helper function called
udpateLargest
.updateLargest
takes in the threeLargest array a number in the input array. As the name states this function updates the current largest number by checking every value in the threeLargest array. For instance lets say we run into the number 20, and we have the following values in threeLargest[2, 6, 9]
. The first if statement will check if the position of 9 is none or if num (20) is greater than 9. Since this is true we shift and update.- To accomplish this we use another helper function called
shiftAndUpdate
. This function takes in an array, num, and index (idx). We pass in the threeLargest variable, num, and the index respectively in this case 2 for the first if statement. The way it shifts and update when the if statement is triggered is that using the idx we can see which value is smaller then num. Using this we replace the value at idx with the new value num. Since we are iterating from 0 we also do the followingarray[i] = array[i+1]
. Essentially what this does is update the least greatest value to the value next to it, until it reaches the desired index to where it is replaced by num.
- Once, all these operations are done we end up with an array that hold the three largest number from least to greatest.
- We define a parent function that holds the