Tandem Bicycle Coding Problem
For this weeks assignment I decided to do one of the longer coding challenges. This one is called tandem bicycle. A tandem bike is a bike that has 2 or more seats on it with every rider pedaling. If there are 2 people riding one bike than the speed of the bike will be determined by the faster peddler and the slower peddler will be ignored. The way this challenge works is you are given an 2 arrays with integer values that represent 2 different groups of riders. One of the groups are red riders and the other group is blue riders. Every index inside of the arrays represent one rider and the value inside of that index determines the speed of the rider. The higher the speed the faster the rider can peddle. You are also given another variable called target which is a boolean and will either be False or True. If the target is True then we are trying to find a way to pair every blue rider with every red rider to find the fastest combination of riders. If target is False we are trying to find the combination of red and blue riders that will have the slowest bike speed. Every blue rider must be paired with every red rider and both arrays will always be the same length. Once we calculate either the fastest or slowest speed we must return that speed to the user.
The way I went around the solution for this problem is to break this down into 2 cases. Either we are calculating the fastest or slowest speed. For both cases we need to initialize 3 variables. The speed which will calculate the total speed in both cases. The max speed which will calculate the max speed of both arrays and the min speed which will calculate the min speed of both arrays. If fastest is True then we go into our if case. The way we calculate the fastest speed is to get the fastest rider from both cases and pair them up with the slowest rider from the other array. This is because since we want the fastest speed the only value that will be calculated into our total speed will be the faster rider and we wouldn't want to pair two riders together that both have high speeds because that would not produce the fastest bike possible. Then I set max = to the max value of both arrays. Once we have the max value of both arrays we go into our first if condition. Now if the max value comes from the redshirt array then we will call the min function on the blue shirts array to get the minimum value from that array that way we can pair the max from red with the min from blue. We then proceed to add the current max value to our total speed. Then for both arrays we will remove the max from red and the min from blue so we dont calculate those riders again. If the max rider came from blue shirts we would do the same except it would be the opposite when it comes to setting the values and removing. We will do this until the length of our red riders array is 0 or less since we know we no longer have any riders in the array. This works since we know both arrays will always have the same length. At the end of our if fastest condition we will return the fastest speed to our user.
Now if we entered the else part of our initial condition we know we are trying to find the combination of riders that will give us the slowest speed. To calculate the slowest speed possible we want to pair up the fastest riders from both arrays together. That is because if you pair the fastest rider from blue with the fastest rider from red it will be like wasting one riders speed because only the faster one will be added to the speed. This way some of the slower speeds in our arrays will be added to the total speed. So we initialize 2 variables. Max blue speed and max red speed. We use the max function on both to get the fastest from red and fastest from blue. Now we enter another condition to check if max red is faster than max blue. If it is we add max red to our total speed. We then go ahead and remove both max red and max speed from their respective arrays. We continue to do this until there are no elements in our red riders array. After that we return the speed to the user which will be the slowest speed possible.
I loved the analogy with tandem bicycles you used. It helped me understand why you would avoid having the fastest speeds on one "bicycle" and how you extract the max speed and min speed depending one which was the fastest and you just repeat. I could definitely see its use in optimization related problems where you want the maximum or minimum speed of an object.
ReplyDeleteGreat work this week, Sergio.
ReplyDelete