# re: 最长连续序列问题[未登录] 回复 更多评论
2013-04-15 15:00 by
I am afraid that the containsKey function is not O(1) operation.
# re: 最长连续序列问题[未登录] 回复 更多评论
2013-04-15 15:10 by
additionally, this structure was merely O(n)
for(int i=0;i<len;++i){
while(end.containsKey(s.start-1)){ //merge ends
# re: 最长连续序列问题[未登录] 回复 更多评论
2013-04-15 15:30 by
write in scala: (is it O(n) complexity?)
def maxSeq(lst: List[Int]): Int = {
val l = lst.sortWith(_ < _)
def findMax(ls: List[Int], maxLen: Int, curr: Int): Int = {
ls match {
case x :: y :: lst if x + 1 == y => findMax(y :: lst, if (maxLen > curr + 1) maxLen else curr + 1, curr + 1)
case x :: xs => findMax(xs, maxLen, 0)
case Nil => maxLen
}
}
findMax(l, 0, 0) + 1
}
def main(args: Array[String]): Unit = {
val lst = List(300, 1, 3, 2, 4, 5, 7, 9, 10, 22, 18, 6)
println(maxSeq(lst))
}
# re: 最长连续序列问题 回复 更多评论
2013-04-15 17:21 by
@Harry
Containkey of hashmap should be O(1) normally. In worst is O(n) when there are lots of hash conflicts.
# re: 最长连续序列问题 回复 更多评论
2013-04-15 17:25 by
@Harry
你这个算法肯定不是,光排序就是O(nlgn)的复杂度了
# re: 最长连续序列问题[未登录] 回复 更多评论
2013-04-16 13:27 by
you're right. the sort function already is O(nlgn)... but I wonder that there is no such a solution to the problem with O(n) complexity.
# re: 最长连续序列问题[未登录] 回复 更多评论
2013-04-16 13:29 by
any way, it is an interesting problem.