Pintia Problem Solution - 7-19 Kindness in Ladder Competition

7-19 Kindness in Ladder Match

Original title:

Ladder match is a kind game. The kind-hearted question group hopes to control the difficulty of the questions within a range so that every participating student can have questions that can be solved, and the most powerful students have to work very hard to get high scores.

So the proposition team first divided programming ability into 106 levels (it’s crazy, it’s fake), and then investigated the programming ability of each participating student. Now please write a program to find out the minimum and maximum ability values ​​of all participating students, and give it to the proposition group as a reference for setting questions.

Input format:

The input gives a positive integer N (≤2×104) in the first line , which is the total number of participating students. The next line gives N positive integers not exceeding 106, which are the ability values ​​of participating students.

Output format:

The first line outputs the minimum ability value of all participating students and the number of students with this ability value. The second line outputs the maximum ability value of all participating students and the number of students with this ability value. Numbers in the same row are separated by 1 space, and there must be no extra spaces at the beginning or end of the line. .

Problem-solving ideas:

  1. Import readlinethe module and create an interface object: First readlineintroduce the module and use createInterfacemethods to create an interface object rl. This object sets the input stream to standard input.
  2. Read input and store: By listening to 'line'events, the input is stored in an array buf.
  3. Parse the input and process the sequence of numbers: assign bufthe second element in the array (that is, the position with index 1) to a variable arr, separate it into an array of strings based on spaces, and then use mapthe method to convert each string into a number.
  4. Find the minimum and maximum values: Use two variables minand max, initialized to arrthe first element of . Traverse the array arr, if the current element is greater than the current element max, it will be maxupdated to the current element; if the current element is less than the current element min, it will minbe updated to the current element.
  5. Count the occurrences of the minimum and maximum values: use two variables minCountand maxCountinitialize to 0. Traverse the array again arr, and if the current element is equal min, minCountadd 1; if the current element is equal max, maxCountadd 1.
  6. Output results: Use console.logto output the minimum value and the number of occurrences of the minimum value, as well as the maximum value and the number of occurrences of the maximum value respectively.

.

JavaScript (node) code:

const r = require("readline");
const rl = r.createInterface({
    
    
    input: process.stdin
});
let buf = [];
rl.on('line', (input) => buf.push(input));
rl.on('close', () => {
    
    
    const arr = buf[1].split(" ").map(Number);
    let min = arr[0];
    let max = arr[0];
    for (let i of arr) {
    
    
        if (i > max) {
    
    
            max = i;
        }
        if (i < min) {
    
    
            min = i;
        }
    }

    let minCount = 0;
    let maxCount = 0;
    for (let i of arr) {
    
    
        if (min == i) {
    
    
            minCount++;
        }
        if (max == i) {
    
    
            maxCount++;
        }
    }

    console.log(min + " " + minCount);
    console.log(max + " " + maxCount);
});

.

Complexity analysis:

Time complexity: O(n)
Space complexity: O(n)

Guess you like

Origin blog.csdn.net/Mredust/article/details/133519639