题目如下:

In a warehouse, there is a row of barcodes, where the i-th barcode is barcodes[i].

Rearrange the barcodes so that no two adjacent barcodes are equal.  You may return any answer, and it is guaranteed an answer exists.

Example 1:

Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]

Example 2:

Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]

Note:

  1. 1 <= barcodes.length <= 10000
  2. 1 <= barcodes[i] <= 10000

解题思路:首先把input按元素出现的次数从多到少排序,如果出现的次数一样,可以按值从小到大排好序。接下来遍历input,依次pop出input的第一个元素,并且把这个元素放入Output的奇数位上;完成之后再依次pop出input的第一个元素并放入Output的偶数位。

代码如下:

class Solution(object):
def rearrangeBarcodes(self, barcodes):
"""
:type barcodes: List[int]
:rtype: List[int]
"""
dic = {}
for i in barcodes:
if i not in dic:
dic[i] = [i,1]
else:
dic[i][1] += 1 def cmpf(l1,l2):
if l1[1] != l2[1]:
return l2[1] - l1[1]
return l1[0] - l2[0] tmp_list = sorted(dic.itervalues(),cmp=cmpf) blist = []
for (k,v) in tmp_list:
blist += [k] * v res = [0] * len(barcodes)
for i in range(0,len(res),2):
res[i] = blist.pop(0)
for i in range(1,len(res), 2):
res[i] = blist.pop(0)
return res

【leetcode】1054. Distant Barcodes的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. join当前线程等待指定的线程结束后才能继续运行

    模拟一个QQ游戏大厅斗地主 /** sleep(休眠.睡眠) join当前线程等待指定的线程结束后才能继续运行 */ class Player extends Thread{ private Stri ...

  2. 安装U盘启动ferdora-22-fce笔记

    如何格式化为fat? windows图形界面格式化, 选项中没有fat, 只有fat32和exfat两种upan格式 Fat就是 传统的FAT16 要格式化为fat, 需要使用cmd的format命令 ...

  3. C++类前置声明

    cpp前置声明: 前置声明只能作为指针或引用,不能定义类的对象,也不能调用对象中的方法. 详见:https://www.cnblogs.com/dobben/p/7440745.html

  4. Delphi XE2 之 FireMonkey 入门(7) - TText 与 TFont

    TText 也是从 TShape(TControl -> TShape)继承; 而与之类似的 TLabel 的继承序列是 TControl -> TStyledControl -> ...

  5. js 正则整理

    //严格验证身份证格式方法function idCardNo(value){ //验证身份证号方法 var area = { 11: "北京", 12: "天津" ...

  6. Reverse Linked List(反转单向链表)

    来源:https://leetcode.com/problems/reverse-linked-list Reverse a singly linked list. 递归方法:递归调用直到最后一个节点 ...

  7. Maven 标准项目结构

    项目结构 src main java         源文件 resources    资源文件 filters   资源过滤文件 config   配置文件 scripts   脚本文件 webap ...

  8. C语言 malloc()、memcpy()、free()等

    1.malloc()函数:  void *malloc(unsigned int num_bytes); 头文件:#include <malloc.h> 或 #include <al ...

  9. 百度文档,用Python一键免费下载

    百度文库下载需要券,或者vip才能下载 Vip价格高,偶尔下载一次不划算. 不下载复制?不好意思复制也需要vip否则只能一次复制两行. 如何才能以最低成本获取到百度文库里的文档内容呢? 当然是用Pyt ...

  10. HDU-1878 欧拉回路(并查集,欧拉回路性质)

    欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...