Huawei OD computer-based test algorithm questions: Transportation time

Question part

topic Transportation time
difficulty Disaster
Question description M (1<=M<=20) vehicles need to reach the end point on a one-way road that cannot overtake. The distance from the starting point to the end point is N (1<=N<=400). After a fast car catches up with the car in front, it can only continue driving at the speed of the car in front. Find the time it takes for the last vehicle to reach its destination.
Note: Each vehicle departs at a fixed interval of one hour. For example, the first vehicle departs at 0 o'clock, the second vehicle departs at 1 o'clock, and so on.
Enter description The two numbers in the first line: MN represent the number of vehicles and the distance to the end point respectively, separated by spaces.
Next there are M lines, each line contains a number S, representing the speed of each vehicle. 0<S<30.
Output description The time it took for the last car to reach its destination.
Additional information none
------------------------------------------------------
Example
Example 1
enter 2 11
3
2
output 5.5
illustrate 2 cars, distance 11, the car starting at 0 o'clock is faster, the car starting at 1 o'clock, the cost to reach the destination is 5.5.


Interpretation and analysis

Question interpretation :

One vehicle is sent out every hour. If the vehicle behind is faster and has caught up with the vehicle in front, the speed will remain the same as the vehicle in front. Find the time it takes for the last car to arrive from departure to destination.

Analysis and ideas :

Although the difficulty of this question is "difficult", the idea is very simple.
Starting from 0 o'clock, the time required for each vehicle is calculated in turn. Since the vehicle behind is faster, if the vehicle behind catches up with the vehicle in front, the arrival time will be the time of the vehicle in front minus 1. Start with the 1st car and count to the last car.

The time complexity is O(n) and the space complexity is O(n).


Code

Java code

import java.util.Scanner;

/**
 * 运输时间
 * @since 2023.09.21
 * @version 0.1
 * @author Frank
 *
 */
public class TransportTime {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String line = sc.nextLine();
			String[] cntDistance = line.split( " " );
			int count = Integer.parseInt( cntDistance[0] );
			int distance = Integer.parseInt( cntDistance[1] );
			
			String[] inputs = new String[count];
			for( int i = 0; i < count; i ++ )
			{
				inputs[i] = sc.nextLine();
			}			
			processTransportTime( count, distance, inputs );
		}
	}
	
	private static void processTransportTime( int count, int distance, String inputs[] )
	{
		float minTime = 0;
		for( int i = 0; i < count; i ++ )
		{
			int speed = Integer.parseInt( inputs[i] );
			float curTime = (float) distance / speed;
			if( i == 0 )
			{
				minTime = curTime;
				continue;
			}
			if( curTime < minTime - 1 )
			{
				minTime = minTime - 1;
			}else
			{
				minTime = curTime;
			}
		}
		System.out.println( minTime );
	}

}

JavaScript code

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function() {
    while (line = await readline()) {
        var cntDistance = line.split(" ");
        var cnt = cntDistance[0];
        var distance = cntDistance[1];

        var speeds = new Array();
        for (var i = 0; i < cnt; i++) {
            line = await readline();
            speeds[i] = parseInt( line );
        }
        processTransportTime(distance, speeds);
    }
}();

function processTransportTime( distance,speeds) {
    var minTime = 0;
    for (var i = 0; i < speeds.length; i++) {
        var speed = parseInt(speeds[i]);
        var curTime = distance / speed;
        if (i == 0) {
            minTime = curTime;
            continue;
        }
        if (curTime < minTime - 1) {
            minTime = minTime - 1;
        } else {
            minTime = curTime;
        }
    }
    console.log(minTime);
}

(over)

Guess you like

Origin blog.csdn.net/ZiJinShi/article/details/133087272
Recommended