Find the longest sequence length whose sum is divisible by 3

前端 未结 4 1275
北荒
北荒 2020-12-15 21:03

I have an exercise that needs to be done with O(n) time complexity, however, I can only solve it with an O(n^2) solution.

You have an array and you need to count the

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 21:15

    As a non-CS person, this is interesting. First approach of mine was simply to calc the running sum mod 3. You'll get a sequence of {0,1,2}. Now look for the first and the last 0, the first and the last 1 and the first and the last 2, and compare their respective distances...

提交回复
热议问题