Skip to content

Commit

Permalink
solved @xiaoshuai96
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoshuai96 committed May 10, 2020
1 parent 493e20a commit 7163aaf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
68 changes: 68 additions & 0 deletions 1054-rearrangeBarcodes/Article/1054-rearrangeBarcodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
## LeetCode第1054号问题距离相等的条形码

> 本文首发于公众号图解面试算法」, [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步个人博客www.zhangxiaoshuai.fun

**本题选自leetcode第1054号问题medium级别目前通过率33.3%**

**题目描述:**

在一个仓库里有一排条形码其中第 i 个条形码为 barcodes[i]。
请你重新排列这些条形码使其中两个相邻的条形码不能相等
你可以返回任何满足该要求的答案此题保证存在答案
示例 1
输入:[1,1,1,2,2,2]
输出:[2,1,2,1,2,1]

示例 2
输入:[1,1,1,1,2,2,3,3]
输出:[1,3,1,3,2,1,2,1]

提示
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000
### 题目分析
1.首先我们需要将每个条形码和出现的次数作一记录为了存取方便这里使用数组题目中已经给出了数组的最大和最小长度进行操作;
2.找出其中出现最多次数的条形码拿到该barcode和count;
3.先将出现次数最多的条形码存入目标数组中偶数位或者奇数位),并对记录数组作一更新
4.随后将剩余的barcode填充进目标数组中

### GIF动画展示

![](../Animation/1054-rearrangeBarcodes.gif)

### 代码

```java
public static int[] rearrangeBarcodes(int[] barcodes){
int[] address = new int[10001];
for (int barcode : barcodes)
address[barcode]++;
// 找到出现次数最多的barcode
int maxCode = 0, maxCount = 0;
for (int i = 0; i < address.length; i++) {
if (maxCount < address[i]) {
maxCode = i;
maxCount = address[i];
}
}
int index = 0;
// 先填充最大的那一位barcode
for (; address[maxCode] > 0; index += 2) {
barcodes[index] = maxCode;
address[maxCode]--;
}
// 继续填充剩余的条形码
for (int i = 1; i < address.length; i++) {
while (address[i] > 0) {
//偶数位填充完毕
if (index >= barcodes.length) index = 1;
barcodes[index] = i;
address[i]--;
index += 2;
}
}
return barcodes;
}
```

0 comments on commit 7163aaf

Please sign in to comment.