【leetcode】1054. Distant Barcodes
题目如下:
In a warehouse, there is a row of barcodes, where the
i-th barcode isbarcodes[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 <= barcodes.length <= 100001 <= 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的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- centos7部署前后端分离项目的过程
概述 本文主要讲解在安装了centos7的Linux主机中部署前后端分离项目的过程. 前端项目名为:vue_project:后端项目名为:django_project. 将这两个项目放在/opt/wh ...
- bp文件错误消除
代码文件报错, error: unused parameter 'data' [-Werror,-Wunused-parameter]‘ 按提示在cflags中加入: "-Wunused-p ...
- Openssl ASN.1 说明一 分享
[引言]ASN.1全称为Abstract Syntax NotationOne,是一种描述数字对象的方法和标准.openssl的编码方法就是基于该标准的,目前,很多其他软件的编码方法也是基于该标准.对 ...
- 练习4-python+selenium+pandas
最近对于python的第三方库pandas比较有兴趣,在学习的过程中也简单的结合selenium做了一个简单的小工具 最新公司用一个外部系统来记录,追踪BUG,可是这个系统并不是专业的BUG管理系统, ...
- lvs三种负载均衡模式
lvs模式 lvs nat模式 地址转换 nat模式 地址转发 ,数据全部集中在lvs处理,lvs压力大 lvs ip-tun模式 ip隧道 与nat模式差不多,差别有了隧道封装,轮询调度给后端服务器 ...
- sql中unique和distinct
在SQL语法里面,有unique和distinct两个关键字, unique是distinct的同义词,功能完全相同.distinct是标准语法,其他数据库 sql server,db2,oracle ...
- python列表-定义
一.定义: 1.“列表”是一个值,它包含多个字构成的序列. 2.术语“列表值”指的是列表本身(它作为一个值,可以保存在变量中,或传递给函数,像所有其他值一样),而不是指列表值之内的那些值.列表值看起来 ...
- POJ-2456.Aggressivecows.(二分求解最大化最小值)
本题大意:在坐标轴上有n个点,现在打算在这n个点上建立c个牛棚,由于牛对厂主的分配方式表示很不满意,它很暴躁,所以它会攻击离它很近的牛来获得快感,这件事让厂主大大知道了,他怎么可能容忍?所以他决定有策 ...
- 旧接口注册LED字符驱动设备(动态映射)
#include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...
- 浏览器输入url按回车背后经历了哪些?
在PC浏览器的地址栏输入一串URL,然后按Enter键这个页面渲染出来,这个过程中都发生了什么事? 1.首先,在浏览器地址栏中输入url,先解析url,检测url地址是否合法2.浏览器先查看浏览器缓存 ...