首页 > 编程语言 >Codility CountBoundedSlices Python

Codility CountBoundedSlices Python

时间:2022-08-16 19:58:16浏览次数:59  
标签:index min Python max self Codility list CountBoundedSlices data

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

点击查看代码
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)

标签:index,min,Python,max,self,Codility,list,CountBoundedSlices,data
From: https://www.cnblogs.com/Coulson/p/16592765.html

相关文章

  • 学习:python 文件的读写
    文件作用文件把一些数据存放起来,可以让程序下一次执行的时候直接使用,而不必重新制作一份,省时省力。文件读写也称为IO流,分为读文件和写文件两个方法读文件Inputstream......
  • 学习python-Day40
    今日学习内容约束条件主键单纯从约束角度上主键等价于非空唯一notnull、uniquecreatetablet1( idintprimarykey, namevarchar(32));mysql>desct......
  • python 中 如何提取或者删除列表的最后几个元素(适用于元组、字符串序列)
     001、>>>test=["a","b","c","d","e","f","g","h","i","j"]##测试列表>>>test['a','b','c'......
  • python不输出warning信息和多线程
    python 不输出warning信息 加入-Wignore参数Python -Wignore XXX.pyPython多线程1同一函数执行多线程,使用apply_async函数参考https://www.cnblogs.com/ai......
  • python-map()函数基本用法
    最近经常遇到一个问题:输入端在同一行输入两个整型数字,并用空格间隔,问如何方便快捷的将这两个变量分别赋予给x1,x2?新手小白,由于不知道map()函数的用法,便想要用仅有的知识去解......
  • python 爬虫
    1.手写第一个python爬虫#爬虫:用程序来获取网站上的资源#常用encoding='utf-8'encoding='gbk'#1.导入urllib.requesturlopen第三方库fromurllib.requ......
  • python数据结构学习整理-集合
    """集合的定义-无序的唯一对象集合-用大括号{}包围,对象相互之间使用逗号分隔-集合是动态的,可以随时添加或删除元素-集合是异构的,可以包含不同类型的数据"""集合的使......
  • Python3_2022
    1、Python中__init__的通俗解释是什么?  2、Python数据结构之序列(Sequence)、集合(Set)、映射(Mapping)不可变序列(ImmutableSequence):字符串(class str)、元组(cl......
  • Python os.system()
    os.system()是\(os\)模块最基础的方法,其他方法一般在该基础上封装完成。os.system()原理\(system()\)可将字符串转化成命令行在服务器上运行;其原理是每条\(syste......
  • Python - PyMuPDF模块的简单使用
    1.简介能够用来对PDF文档进行操作的Python包有好几个,如用于提取内容的PdfPlumber、PDFMiner,可以用来对PDF文件进行修改操作的PyPDF2等等,如果只是需要简单地对PDF文件实现......