2023 Huawei OD computer test (python) Volume B - Find the optimal time period for interface success rate

1. Title

Topic description:

The interface success rate exchanged between services is a key quality characteristic of service invocation. The interface failure rate within a certain period of time is represented by an array. Each element in the array is the failure rate value per unit time. The value in the array is 0~ An integer of 100, given a value (minAverageLost) that represents the average failure rate tolerance within a certain time period, that is, the average failure rate is less than or equal to minAverageLost, find the longest time period in the array, and directly return NULL if not found.

2. Input and output

Input description:
The input has two lines of content, the first line is {minAverageLost}, the second line is {array}, the array elements are separated by spaces (" "), minAverageLost and the array The element value range is an integer from 0 to 100, and the number of array elements will not exceed 100.
Output description:
Find the longest period in which the average value is less than or equal to minAverageLost, and output the array subscript pair in the format {beginIndex}-{endIndx} (below The subscripts start from 0). If there are multiple maximum time periods at the same time, multiple subscript pairs are output and are spliced ​​with spaces (" ") between the subscript pairs. Multiple subscript pairs are sorted by subscript from small to large.

3. Example

Example 1 

Input and output examples are for debugging only, background judgment data generally does not contain examples
Input:
1
0 1 2 3 4
Output:
0-2
Explanation:
Input explanation : minAverageLost=1, array [0, 1, 2, 3, 4]
The average of the first 3 elements is 1, so the first to third array subscripts of the array, that is 0-2

4. Requirements

Time limit: 1 second for C/C++, 2 seconds for other languages
Space limit: 262144K for C/C++, 524288K for other languages
64bit IO Format: %lld

5. Problem-solving ideas

Problem-solving ideas:

  1. Convert the input array into a list of integers separated by spaces;
  2. Define variablesstart and end represent the starting subscript and end subscript of the longest period of time respectively. The initial value is 0; 3 is determined. The defined variablemax_length represents the length of the longest period, and the initial value is 0;
  3. Define variablessum_lost represents the sum of the failure rates in the current time period, and the initial value is 0;
  4. Define variablecount represents the number of elements in the current time period, and the initial value is 0;
  5. Iterate over the array, for each element:
    • Join general elementsum_lost;
    • GeneralcountAdd1;
    • If the average failure rate in the current time period is less than or equal to minAverageLost, and the length of the time period is greater than max_length, update max_length , start and end;
    • If the average failure rate in the current period is greater thanminAverageLost, reset sum_lost and count;
  6. Ifmax_length is 0, it means that no segment satisfying the condition time is found, and the string "NULL" is returned;
  7. Otherwise, return the starting index and ending index of the longest period in the format of "{beginIndex}-{endIndex}".

6. Reference code 

# -*- coding: utf-8 -*-
'''
@File    :   2023-B-查找接口成功率最优时间段.py
@Time    :   2023/12/12 23:26:53
@Author  :   mgc 
@Version :   1.0
@Desc    :   None
'''

def find_longest_time_period(minAverageLost, array):
    minAverageLost = int(minAverageLost)
    array = [int(num) for num in array.split()]

    start = 0  # 最长时间段的起始下标
    end = 0  # 最长时间段的结束下标
    max_length = 0  # 最长时间段的长度
    sum_lost = 0  # 当前时间段的失败率总和
    count = 0  # 当前时间段元的素个数

    for i in range(len(array)):
        sum_lost += array[i]
        count += 1

        if sum_lost / count <= minAverageLost and count > max_length:
            max_length = count
            end = i
            start = end - max_length + 1

        if sum_lost / count > minAverageLost:
            sum_lost = 0
            count = 0

    if max_length == 0:
        return "NULL"
    else:
        return f"{start}-{end}"


minAverageLost = input()
array = input()
result = find_longest_time_period(minAverageLost, array)
print(result)

Guess you like

Origin blog.csdn.net/u014481728/article/details/134961493
Recommended