算法学习day6----双指针-最长不重复子序列

Given an integer sequence of length n, please find the longest continuous interval without duplicate numbers and output its length. 

The first line contains an integer n. 


The second line contains n integers (all within the range of 0 to 105), representing an integer sequence. 

Output format
One line containing an integer, representing the length of the longest consecutive interval without duplicate numbers. 

Data range 1≤n≤105

input:

5
1 2 3 4 5

output:

3

算法学习day6----双指针-最长不重复子序列_第1张图片

def longest_unique_subsequence(nums):
    maxlong=0
    seen=set()
    left=0
    for right in range(len(nums)):
        while nums[right]  in seen:
            seen.remove(nums[left])
            left+=1
        seen.add(nums[right])
        maxlong=max(maxlong,right-left+1)
    return maxlong
    
n=int(input())
nums=list(map(int,input().split()))
print(longest_unique_subsequence(nums))
            

你可能感兴趣的:(学习)