Distant Barcodes

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]

思路:跟 Reorganize String 一模一样;

数学的思想就是,统计频率以后,先用频率最大的填0,2,4 然后继续用c填 6,8,10 ...直到超出length范围,然后从奇数位子开始填1,3,5 这样就是一个不想临的string;我个人比较喜欢后面一种用priorityqueue来解答的,可以拓展到 Rearrange String k Distance Apart 这题,这两题其实非常类似;

class Solution {
    public int[] rearrangeBarcodes(int[] barcodes) {
        if(barcodes == null || barcodes.length == 0) {
            return new int[0];
        }
        int n = barcodes.length;
        int[] res = new int[n];
        HashMap<Integer, Integer> countMap = new HashMap<>();
        int maxi = 0; int maxcount = 0;
        for(int i = 0; i < barcodes.length; i++) {
            int val = barcodes[i];
            countMap.put(val, countMap.getOrDefault(val, 0) + 1);
            if(countMap.get(val) > maxcount) {
                maxcount = countMap.get(val);
                maxi = val;
            }
        }
        
        int index = 0;
        while(index < n && countMap.get(maxi) > 0) {
            res[index] = maxi;
            index += 2;
            countMap.put(maxi, countMap.get(maxi) - 1); 
        }
        
        for(Integer i: countMap.keySet()) {
            while(countMap.get(i) > 0) {
                if(index >= n) {
                    index = 1;
                }
                res[index] = i;
                index += 2;
                countMap.put(i, countMap.get(i) - 1);
            }
        }
        return res;
    }
}

用priorityQueue来做,比较规范,最后T: nlog(26) = > O(N);

class Solution {
    private class Node {
        public int val;
        public int fre;
        public Node(int val, int fre) {
            this.val = val;
            this.fre = fre;
        }
    }
    
    private class NodeComparator implements Comparator<Node> {
        @Override
        public int compare(Node a, Node b) {
            return b.fre - a.fre;
        }
    }
    public int[] rearrangeBarcodes(int[] barcodes) {
        if(barcodes == null || barcodes.length == 0) {
            return new int[0];
        }
        int n = barcodes.length;
        HashMap<Integer, Integer> countMap = new HashMap<>();
        for(int i = 0; i < n; i++) {
            int val = barcodes[i];
            countMap.put(val, countMap.getOrDefault(val, 0) + 1);
        }
        
        PriorityQueue<Node> pq = new PriorityQueue<Node>(new NodeComparator());
        for(Integer val: countMap.keySet()) {
            pq.offer(new Node(val, countMap.get(val)));
        }
        
        int[] res = new int[n];
        int index = 0;
        Node pre = null;
        while(!pq.isEmpty()) {
            Node node = pq.poll();
            res[index++] = node.val;
            node.fre--;
            if(pre != null && pre.fre > 0) {
                pq.offer(pre);
            }
            pre = node;
        }
        return res;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
When Apple released the first beta of i OS 7 at WWDC 2013 , I scoured through the API changes looking for anything out of the ordinary. That’s when I noticed the unex- pected addition of new API s pertaining to barcodes. At that time, barcodes were little more than visual noise to me, a necessity of mod- ern commerce but of no value to me as a consumer or app developer. Why would Apple devote precious resources to implementing functionality for that? Several third-party libraries for barcode scanning were available at that time. Some were commercial offerings too expensive for casual use. Others were open source projects requiring a great deal of work to understand or implement in your own apps. By adding support for barcodes within the i OS SDK , Apple made the technology acces- sible to all developers equally. Apple was sending a message: barcodes are important to us. This paradigm shift inspired me to learn all I could about barcode technologies. I began to research the barcode types supported by iOS and their capabilities and limi- tations, and all the new related i OS API s. A mere month after WWDC 2013 , I was contacted by Manning. They’d found me via my blog (cocoanetics.com) and inquired if I would be interested in writing an i OS book for them. They could not have contacted me at a more perfect moment! I was willing, able, and inspired to write, for more than a year, the book you’re now holding. June 2014 marked the 40-year anniversary of the first barcode being scanned at a point of sale. In other words, barcodes are a nearly ubiquitous, mature technology. The UPC you’ll find on all products sold in your supermarket was just the beginning. PREFACE xii Just look at any Apple product box. You’ll find several barcodes on the stickers offer- ing additional information such as the device’s serial number. Since October 2013, all iPhones can be used to scan barcodes. Together with always-on mobile internet and built-in device sensors, this enables a new breed of product-centric apps that weren’t feasible before. After reading this book, you’ll be able to build the exciting new apps that are bringing together the digital and physical worlds.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值