Codility CountBoundedSlices Python


捣鼓了挺久总算整出一个可行解

点击查看代码
class Queue(object):
    def __init__(self):
        super(Queue, self).__init__()
        self.max_index = -1
        self.min_index = -1
        self.data_list = []

    def push(self, x):
        if len(self.data_list) == 0:
            self.max_index = 0
            self.min_index = 0
            self.data_list.append(x)
        else:
            # print("self.min_index, self.max_index: ", self.min_index, self.max_index)
            if x < self.data_list[self.min_index]:
                self.min_index = len(self.data_list)
                self.data_list.append(x)
            elif x > self.data_list[self.max_index]:
                self.max_index = len(self.data_list)
                self.data_list.append(x)
            else:
                self.data_list.append(x)

    def pop(self):
        if len(self.data_list) <= 1:
            self.data_list.pop(0)
            self.max_index = -1
            self.min_index = -1
        else:
            self.data_list.pop(0)
            if self.min_index == 0:
                self.min_index = 0
                for i in range(1, len(self.data_list)):
                    if self.data_list[i] <= self.data_list[self.min_index]:
                        self.min_index = i
            else:
                self.min_index -= 1
            if self.max_index == 0:
                self.max_index = 0
                for i in range(1, len(self.data_list)):
                    if self.data_list[i] >= self.data_list[self.max_index]:
                        self.max_index = i
            else:
                self.max_index -= 1

    def satasify(self, K):

        if not len(self.data_list) or self.data_list[self.max_index] - self.data_list[self.min_index] <= K:
            return True
        else:
            return False

    # def info(self):
    #     print("self.data_list:", self.data_list)
    #     print("self.min_index", self.min_index, "self.min:",
    #           self.data_list[self.min_index] if self.min_index >= 0 else -1)
    #     print("self.max_index", self.max_index, "self.max:",
    #           self.data_list[self.max_index] if self.max_index >= 0 else -1)


def solution2(K, A):
    queue = Queue()
    num_count = 0
    loop_index = 0
    if max(A) - min(A) <= K:
        num_count = len(A) * (len(A) + 1) >> 1
        num_count = min(num_count, 1000000000)
    else:
        while loop_index < len(A) or len(queue.data_list):
            if loop_index < len(A) and queue.satasify(K):
                queue.push(A[loop_index])
                loop_index += 1
            if not queue.satasify(K):
                num_count += len(queue.data_list) - 1
                queue.pop()
            if loop_index >= len(A) and queue.satasify(K):
                num_count += (len(queue.data_list) * (len(queue.data_list) + 1)) >> 1
                queue.data_list = []
            num_count = min(num_count, 1000000000)
            if num_count == 1000000000:
                break

    return num_count


def solution(K, A):
    num_count = 0
    for s in range(len(A)):
        num_count += 1
        min_val = A[s]
        max_val = A[s]
        for idx in range(s + 1, len(A)):

            if A[idx] < min_val:
                min_val = A[idx]
            elif A[idx] > max_val:
                max_val = A[idx]
            if max_val - min_val <= K:
                num_count += 1
            else:
                break

        # print("max_val:", max_val, "min_val:", min_val, "s:", s, "max_e:", max_e, "num_count:", num_count)
        num_count = min(num_count, 1000000000)
        if num_count == 1000000000:
            break
    return num_count


if __name__ == '__main__':
    # # A=9
    # K = 2
    # A = [3, 5, 7, 6, 3]
    # lea_count = solution(K, A)
    # print(lea_count)
    #
    # # A=1
    # K = 0
    # A = [1000000000]
    # lea_count = solution(K, A)
    # print(lea_count)
    #
    # # A=1
    # K = 1
    # A = [-1000000000]
    # lea_count = solution(K, A)
    # print(lea_count)
    #
    # # A=899
    # K = 3
    # A = [8, 1, 7, 2, 3, 3, 9, 1, 4, 2, 6] * 50
    # lea_count = solution(K, A)
    # print(lea_count)

    # A=9
    K = 2
    A = [3, 5, 7, 6, 3]
    lea_count = solution2(K, A)
    print(lea_count)

    # A=1
    K = 0
    A = [1000000000]
    lea_count = solution2(K, A)
    print(lea_count)

    # A=1
    K = 1
    A = [-1000000000]
    lea_count = solution2(K, A)
    print(lea_count)

    # A=17
    K = 3
    A = [8, 1, 7, 2, 3, 3, 9, 1, 4, 2, 6]
    lea_count = solution2(K, A)
    print(lea_count)

    # A=55
    K = 3
    A = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
    lea_count = solution2(K, A)
    print(lea_count)

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/280931.html

(0)
上一篇 2022年8月16日
下一篇 2022年8月16日

相关推荐

发表回复

登录后才能评论