Python bigO question

Asked 2 years ago, Updated 2 years ago, 40 views

def solution(number, k):
    num_s = str(number)
    answer = ''
    if k == 0:
        return number
    list1 = []
    list_a = []
    list_temp = []
    for i in range(len(str(number))):
        list1.append(int(num_s[i]))
    num1 = len(list1) - k

    while True:
        max_num = 0
        if len(list_a) == num1:
            answer = ''.join(map(str,list_a))
            return answer
        if k == 0:
            list_a = list_a + list1
            answer = ''.join(map(str,list_a))
            return answer

        for i in range(k+1):
            if list1[i] == 9:
                max_num = 9
            elif list1[i] >= max_num:
                max_num = list1[i]
        list_a.append(max_num)
        temp = list1.index(max_num)
        k = k - temp
        list1 = list1[temp+1:]

I'm solving the programmers problem, but one test keeps showing a timeout. Where do you think it takes the most time here?

python algorithm

2022-09-20 18:56

1 Answers

If you use while True, the code repeats indefinitely.

Therefore, you have to break the loop with the same command as break in the middle, but there are no commands to stop repeating except for two if statements.

I don't know what number and k values are, but I think there will be a problem in that part.


2022-09-20 18:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.