【最小循环子数组】Python实现

liftword1周前 (05-28)技术文章14
def find_minimal_pattern(nums):
    n = len(nums)
    for i in range(1, n + 1):
        if n % i != 0:
            continue
        pattern = nums[:i]
        if pattern * (n // i) == nums:
            return pattern
    return nums  # 自身是最小子数组

# 读取输入
n = int(input())
nums = list(map(int, input().split()))

# 查找并输出最小子数组
result = find_minimal_pattern(nums)
print(' '.join(map(str, result)))


相关文章

Python算法:4.寻找两个正序数组的中位数

题目:寻找两个正序数组的中位数给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 算法的时间复杂度应该为 O(log(m+n...

在Python中怎么用二分法插入一个新的数到数组列表

接昨天的把一个数插入到有序的数组、列表中,我们今天使用二分法来,就好比一个队分成2个队,因为有序的么,先比较中间的,得到结果,只要比较一个方向的一半就可以了,这样一来是不是减少一半的时间,节约是运行时...

Python实现【分割数组的最大差值】

n = int(input()) nums = list(map(int, input().split())) total_sum = sum(nums) max_diff = 0 left_sum...

Python实现【找出两个整数数组中同时出现的整数】

from collections import defaultdict import sys def solve(): # 读取输入 arr1 = list(map(int, sys...

Python实现分治算法?

分治算法(Divide and Conquer Algorithm)是一种设计算法的策略,它将一个问题分成多个相似的子问题,递归地解决这些子问题,然后将结果合并以得到原问题的解。典型的分治算法包括归并...

【找出两个整数数组中同时出现的整数】Python实现

from collections import Counter def find_common_elements(arr1, arr2): # 统计数组中每个数字的出现次数 coun...