最长的可整合子数组_python3
最长的可整合子数组的长度
http://www.nowcoder.com/questionTerminal/677a21987e5d46f1a62cded9509a94f2
感觉题目有点歧义,子数组可以不连续,所以有重复数据也可以去重排序后计算
1、731 ms 4088K
def check(l): return len(l) == (l[-1] - l[0] + 1) while True: try: n = int(input()) l = list(map(int, input().split())) l = sorted(list(set(l))) maxL = 1 n = len(l) for i in range(n-1): for j in range(i+1, n): if check(l[i:j+1]): maxL = max(maxL, j-i+1) else: break print(maxL) except EOFError: break
2、214 ms 3940K
while True: try: n = int(input()) l = list(map(int, input().split())) l.sort() maxL = 1 if n == 2 and l[1]-l[0] == 1: maxL = 2 else: for i in range(n-2): mi = l[i] ma = l[i+1] if mi == ma or ma - mi > 1: continue for j in range(i+2, n): if ma + 1 == l[j]: ma = l[j] maxL = max(maxL, j-i+1) else: break print(maxL) except EOFError: break
3、31 ms 4216K
while True: try: n = int(input()) l = list(map(int, input().split())) l = sorted(list(set(l))) tmp = maxL = 1 n = len(l) for i in range(1, n): if l[i] - l[i-1] == 1: tmp += 1 else: maxL = max(maxL, tmp) tmp = 1 print(max(maxL, tmp)) except EOFError: break