Huawei OD computer-based test algorithm questions: bonus points

Question part

topic Bonus points
difficulty Disaster
Question description The boss of the company has made a big business and wants to allocate some bonuses to each employee. He wants to use a game to determine how much money each person will get. According to the order of employees' job numbers, a number is randomly selected for each person. Arrange according to the order of the employee number. If the first number is greater than your own number, then the employee in front can get a bonus of the difference between the distance * number. If you don't encounter a number larger than your own, assign yourself a random number of bonuses. For example, the random numbers in order of job numbers are: 2,10,3. Then the number 10 of the second employee is greater than the number 2 of the first employee, so the first employee can get 1 * (10 - 2) = 8. There is no employee with a higher number behind the second employee, so he gets the bonus of the random number he was assigned, which is 10. The third employee is the last employee, and there is no employee with a larger number behind him, so the bonus he gets is 3.
Please help the boss calculate how much bonus each employee will receive in the end.
Enter description The first line n represents the number of employees (including the last boss).
The second is a random number assigned to each employee.
For example:
3
2 10 3
Output description The final bonus amount allocated to each employee.
For example:
8 10 3
Additional information The random numbers do not repeat, the number of employees (including bosses) ranges from 1 to 10,000, and the random numbers range from 1 to 100,000.
------------------------------------------------------
Example
Example 1
enter 3
2 10 3
output 8 10 3
illustrate none


Interpretation and analysis

Question interpretation :

When distributing bonuses, employees are first sorted by employee number. The number of bonuses for each employee is related to the number drawn by the employee with a larger job number, and has nothing to do with the number drawn by the employee with a smaller job number.

This question can be translated as: There is an integer array, set to arr, whose length is n, and the array elements have been initialized to the specified value. For the i-th (0  ≤ i < n ) element in the array, if:
1. There is an element larger than arr[i] among the (i + 1) to n-th elements. If there are multiple such elements, take The element with the smallest subscript. Assuming that the subscript of the element with the smallest subscript is j, then the value of arr[i] is modified to ( arr[j] - arr[i] ) * ( j - i).
2.  There is
no element larger than arr[i] among the (i + 1) to nth elements , then the value of arr[i] remains unchanged. After traversing the array arr, output the contents of the array arr.

Analysis and ideas :

Although the difficulty of this question is "hard", the problem-solving ideas and algorithms are very simple.

We can directly start from the 0th element and traverse backwards, and refresh the value of each element one by one according to the two situations mentioned in the question interpretation . Because the final value of each element is only related to the element that follows it, you must start traversing from the 0th element, not from the last element (n - 1).

During the traversal process, in the worst case, n! elements need to be traversed, and the time complexity of this algorithm is O(n!); the entire traversal process only uses primitive data type variables, and the space has nothing to do with the length of the array. The space of this algorithm The complexity is O(1).


Code

Java code

import java.util.Scanner;

/**
 * 分奖金
 * @since 2023.09.11
 * @version 0.1
 * @author Frank
 *
 */
public class BonusDistribution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			String input = sc.nextLine();
			int count = Integer.parseInt( input );
			input = sc.nextLine();
			String[] numbers = input.split( " " );
			// 此处 count == numbers.count,可以完全不用考虑 count.
			processBonusDistribution( numbers );
		}

	}
	
	private static void processBonusDistribution( String numbers[] )
	{
		int[] arr = arrString2Int( numbers );
		for( int i = 0; i < arr.length; i ++ )
		{
			for( int j = i + 1; j < arr.length; j ++ )
			{
				if( arr[j] > arr[i] )
				{
					arr[i] = ( arr[j] - arr[i] ) * ( j - i );
					break;
				}
			}
		}
		
		StringBuffer sb = new StringBuffer();
		for( int i = 0; i < arr.length; i ++ )
		{
			if( i != arr.length - 1 )
			{
				sb.append( arr[i] + " " );
			}else
			{
				sb.append( arr[i] );
			}
		}
		System.out.println( sb.toString() );
	}
	
	private static int[] arrString2Int( String numbers[] )
	{
		int ret[] = new int[numbers.length];
		for( int i = 0; i < numbers.length; i ++ )
		{
			ret[i] = Integer.parseInt( numbers[i] );
		}
		return ret;
	}

}

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()) {
        // count 可以忽略
        var count = parseInt(line);
        line = await readline();
        var numberArr = line.split(" ");
        processBonusDistribution(numberArr);
    }
}();

function processBonusDistribution(numberArr) {
        var arr = arrString2Int( numberArr );
        for( var i = 0; i < arr.length; i ++ )
        {
            for( var j = i + 1; j < arr.length; j ++ )
            {
                if( arr[j] > arr[i] )
                {
                    arr[i] = ( arr[j] - arr[i] ) * ( j - i );
                    break;
                }
            }
        }
        console.log( arr.join(" "));
}

function arrString2Int( numberArr )
{
    var ret = [];
    for( var i = 0; i < numberArr.length; i ++)
    {
        ret.push( parseInt( numberArr[i] ) );
    }
    return ret;
}

(over)

Guess you like

Origin blog.csdn.net/ZiJinShi/article/details/132767869