b**********1 发帖数: 215 | 1 最近收到一道 java coding question:
Given a list of country names and an int value n, write a method that
determines if there is any
combination of three countries that when you sum up their occurrence it is
equal to n
Examples:
list = Italy, France, USA, UK, Germany, Italy, Mexico, USA, Canada, Italy
n = 7
result = return false
list = Greece, Spain, China, Norway, Russia, Greece, Portugal, China, Greece
, Portugal, China, Turkey, Greece, China
n =10
result = return true
思路是:吧这些 list 输入到hashmap 里,得出他们的出现次数。但是 如何得到 最多
的前3个出现的次数,难道对 hashmap 根据value 进行排序,然后计算 总和 嘛? 如
果您有更好的 办法,请指教, 谢谢。 | j******o 发帖数: 4219 | | k****r 发帖数: 807 | 3 是问最多频率的三个之和,还是任意三个?
最多频之和,用map+minheap,heap实时更新,存最多频那仨;
任意三个,。。。只能每个都试一试了吧,或者sort之后,3sum。 |
|