头图

cause

Recently I interviewed web full-stack development engineer position. I came into contact with an algorithm question. I felt that it was a test of analysis ability and an algorithm that had a certain effect in practice, so I shared it with you.

topic

According to operational requirements, you need to add a digital input box verification function to our value release system. We usually limit the size of some digital inputs.
For example, the required input value must be between 300 - 347 (including 300 and 347 ). You are wise to find out that sometimes you can know whether the user's input is illegal without waiting for the user to finish typing.
For example, in the above situation, when the user enters 37 , his input is definitely illegal.

Now you need to use a program to do this.
enter:
The first line is the lower bound of the input box
The second line is the upper bound of the input box
The third line is some integers separated by commas, these integers are used as numbers entered by the user
Output:
There is only one line, which is some strings separated by commas.
Each string corresponds to a number entered by the user. If the user input is INVALID , output 060edbf9c2d769, otherwise output VALID

Input constraints
The upper and lower bounds are between [1, 2000000000] , and the lower bound is less than or equal to the upper bound, the number of digits in the third row is between [1, 50] , and each digit is also between [1, 2000000000]

Example 1:
enter
300
347
37
Output
INVALID

Example 2:
enter
310
320
3,31,317,3174,310,320
Output
VALID,VALID,VALID,INVALID,VALID,VALID
Explanation: The first 3 are the order input when inputting 317 , so they are all legal. The last 2 time upper and lower bounds are also legal

Topic analysis

  1. If the input is greater than the upper limit, return directly to INVALID
  2. Enter between the lower limit and the upper limit, and directly return to VALID
  3. Only when the input is less than the lower limit, the verification will occur, so the code focuses on this step

    • The difference between the upper limit and the lower limit is more than 1 digit. For example, the lower limit 31 is a two-digit number, and the upper limit 7001 is a four-digit number, because the input is less than the lower limit 31, and because the upper and lower limits can contain all three digits, three digits All meet the conditions, so directly return VALID
    • When the upper and lower limits have the same number of digits, you only need to shorten the upper and lower limits to the same length as the input number (removed from the right). At this time, the input must be between the shortened upper and lower limits to return to VALID , otherwise you are in No matter how you add the number after input, it cannot be guaranteed to be between the upper and lower limits.
    • When the upper limit and lower limit digits differ by exactly 1 digit

      • 50 lower limit is less than the first digit of the upper limit 618 , and because the input is less than 50 , so you add one digit after the input that is greater than 50 less than 618 , and directly returns to VALID
      • The first digit of the lower limit is greater than or equal to the first digit of the upper limit, and the upper and lower limit numbers are also shortened to the same length as the input number (removed from the right). At this time, you must ensure that the input is greater than or equal to the shortened lower limit, or less than or equal to the shortened upper limit (in this case, shorten After the lower limit> the shortened upper limit), you can return to VALID , otherwise it is illegal to add any number again after input
  4. In other cases, it will always return INVALID

Code

const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
let min = 0;
let minLen = 0;
let max = 0;
let maxLen = 0;
let strArr = null;
let outputArr = [];
let index = 0;
rl.on('line', function(line) {
    if (index === 0) {
      min = parseInt(line);
      if (min < 1 || min > 12000000000) {
        console.log('输入有误!');
        rl.close();
      }
      minLen = line.length;
    }
    if (index === 1) {
      max = parseInt(line);
      if (max < 1 || max > 12000000000) {
        console.log('输入有误!');
        rl.close();
      }
      if (min > max){
        console.log('上界不能小于下界!');
        rl.close();
      }
      maxLen = line.length;
    }

    if (index === 2) {
      strArr = line.split(',');
      const len = strArr.length;
      if (len < 1 || len > 50) {
        console.log('输入有误!');
        rl.close();
      }

      for (const str of strArr) {
        const num = parseInt(str);
        if (num < 1 || num > 12000000000) {
          console.log('输入有误!');
          rl.close();
        }
        // 大于上限直接返回INVALID
        if (num > max) {
          outputArr.push('INVALID');
          continue;
        }
        // 大于等于下限且小于等于上限直接返回VALID
        if (num <= max && num >= min) {
          outputArr.push('VALID');
          continue;
        }
        // 小于下限的情况

        // 上下限位数相差1以上,直接返回VALID
        if (maxLen - minLen > 1) {
          outputArr.push('VALID');
          continue;
        }
        // 上下限位数相等的情况,即maxLen = minLen;
        const len = str.length;
        const offsetMin = String(min).slice(0, len);
        const offsetMax = String(max).slice(0, len);
        if (maxLen === minLen && str >= offsetMin && str <= offsetMax) {
          outputArr.push('VALID');
          continue;
        }
        // 上下限位数相差1位的情况
        if (maxLen - minLen === 1) {
          // 下限首位小于上限首位
          if (String(min)[0] < String(max)[0]) {
            outputArr.push('VALID');
            continue;
          }
          // 下限首位大于等于上限首位
          if (str >= offsetMin || str <= offsetMax ){
            outputArr.push('VALID');
            continue;
          }
        }
        
        // 其他情况
        outputArr.push('INVALID');
      }
      console.log(outputArr.join(','));
      rl.close();
    }
    index++;
}).on('close',function(){
    // It's important to exit, otherwise the execution will time out
    process.exit(0);
});

to sum up

This code ran through all test cases on the test terminal and did not exceed the response time limit. You can make a reference, and friends with other better ideas are welcome to leave a message and share.


小磊
352 声望884 粉丝

以一颗更加开放,更加多元,更加包容的心走进别人的世界