CareerCup Finds all the elements that appear more than n/3 times in the list

265 篇文章 1 订阅
86 篇文章 0 订阅

Design an algorithm that, given a list of n elements in an array, finds all the elements that appear more than n/3 times in the list. The algorithm should run in linear time ( n >=0 ) 

 

 

You are expected to use comparisons and achieve linear time. No hashing/excessive space/ and don't use standard linear time deterministic selection algo

 

 

---------------------------------------------------------------------------------------------------------------------------

Solution:

Use a Dictionary with no more than 3 keys!!!

Well, here comes the English: 
The idea of the problem is from the famous game: Tetris 
Lets see how it works. Consider m = 5; 
Given an array : 4 3 3 2 1 2 3 4 4 7, we treat each number as a piece in Tetris, which falls down from the ceil. Our task is to try to keep the same number stacked on the same column. Consider the moment that 7 is going to fall down. The snapshot of our game now is like : 




4 3 2 
4 3 2 1 _ 
Note that, the size of a row is designed as M, which is 5 here. 
Just like Tetris, this game has a similar rule that: 
if a row is full of numbers then it should be eliminated. 
So when 7 goes down, it becomes: 

4 3 2 
4 3 2 1 7 //This row is full and to be eliminated 
Then the bottom row is gone. 
It becomes 

4 3 2 
As the numbers falls down, eventually, the game will end in a status that no row is full. 
So we have at most M - 1 numbers left at the final stage. 
But it is not over. We can easily prove that, if there is a solution, it must be in the number left at the final stage; but we can not guarantee all numbers left are the correct solution. 
So we need to scan the array again, and count the numbers left to find the correct solutions and report their frequencies.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值