Codility CountBoundedSlices Python
2022/8/17 1:54:51
本文主要是介绍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)
这篇关于Codility CountBoundedSlices Python的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享
- 2024-12-19使用python 把docx转为pdf文件有哪些方法?-icode9专业技术文章分享
- 2024-12-19python怎么更换换pip的源镜像?-icode9专业技术文章分享
- 2024-12-19Python资料:新手入门的全面指南
- 2024-12-19Python股票自动化交易实战入门教程
- 2024-12-19Python股票自动化交易入门教程
- 2024-12-18Python量化入门教程:轻松掌握量化交易基础知识