微软面试题6.18

Given an array with around 10w elements, each element is within [0,10w], how to get max subsequense length with same value if you have a opportunity to swap two elements?

Sampes:
1,1,0,2,3,1,0,1,1,3,4 => output: 4
0,2,3,1,0,1,1,3,4 => output: 3
1,1,0,0,2,3,3,1 => output: 3

用以下数据结构存储:

Key num, value = [(start,end)...(start,end)]

Case 1:

0:[(2,2),(6,6)]

1:  [(0,1),(5,5), (7,8)]

2:  [(3,3)]

3: [ (4,4), (9,9)]

4: [(10,10)]

 

 

 

 

 

你可能感兴趣的:(算法)