zoukankan      html  css  js  c++  java
  • 30-Day Leetcoding Challenge Day12

    本题的难点在于每次要找到数组中前两个最大的值。提供三种解法:

    1.每次对数组进行排序找到前两个最大的值

    2.利用堆结构找到最大的两个值 PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder())

    3.桶排序:虽然降低时间复杂度。但只适用于最大值 较小 时才可行。

    JAVA

    class Solution {
        public int lastStoneWeight(int[] stones) {
            List<Integer> _stones = new LinkedList<>();
            for(int stone : stones){
                _stones.add(stone);
            }
            Collections.sort(_stones);
            while(_stones.size() > 1){
                int x = _stones.remove(_stones.size()-2);
                int y = _stones.remove(_stones.size()-1);
                if(x != y){
                    _stones.add(y-x);
                    Collections.sort(_stones);
                }
            }
            return !_stones.isEmpty() ? _stones.get(0) : 0;
        }
    }
    class Solution {
        public int lastStoneWeight(int[] stones) {
            PriorityQueue<Integer> heap = new PriorityQueue<>(Comparator.reverseOrder());
            for(int stone: stones){
                heap.add(stone);
            }
            while(heap.size() > 1){
                int y = heap.remove();
                int x = heap.remove();
                if(x != y){
                    heap.add(y-x);
                }
            }
            return !heap.isEmpty() ? heap.poll() : 0;
        }
    }
    class Solution {
        
        public int lastStoneWeight(int[] stones) {
            
            // Set up the bucket array.
            int maxWeight = stones[0];
            for (int stone: stones) {
                maxWeight = Math.max(maxWeight, stone);
            }
            int[] buckets = new int[maxWeight + 1];
    
            // Bucket sort.
            for (int weight : stones) {
                buckets[weight]++;
            }
    
            // Scan through the buckets.
            int biggestWeight = 0;
            int currentWeight = maxWeight;
            while (currentWeight > 0) {
                if (buckets[currentWeight] == 0) {
                    currentWeight--;
                } else if (biggestWeight == 0) {
                    buckets[currentWeight] %= 2;
                    if (buckets[currentWeight] == 1) {
                        biggestWeight = currentWeight;
                    }
                    currentWeight--;
                } else {
                    buckets[currentWeight]--;
                    if (biggestWeight - currentWeight <= currentWeight) {
                        buckets[biggestWeight - currentWeight]++;
                        biggestWeight = 0;
                    } else {
                        biggestWeight -= currentWeight;
                    }
                }
            }
            return biggestWeight;
        }
    }

    Python3

    class Solution:
        def lastStoneWeight(self, stones: List[int]) -> int:
            stones.sort(reverse = True)
            while len(stones) > 1:
                y = stones[0]
                x = stones[1]
                del stones[:2]
                if x != y:
                    stones.append(y-x)
                    stones.sort(reverse = True)
            return stones[0]
    class Solution:
        def lastStoneWeight(self, stones: List[int]) -> int:
            for i in range(len(stones)):
                stones[i] *= -1
            heapq.heapify(stones)
            while len(stones) > 1:
                y = heapq.heappop(stones)
                x = heapq.heappop(stones)
                if x != y:
                    heapq.heappush(stones,y-x)
            return -heapq.heappop(stones) if stones else 0
    class Solution:
        def lastStoneWeight(self, stones: List[int]) -> int:
            
            # Set up the bucket array.
            max_weight = max(stones)
            buckets = [0] * (max_weight + 1)
    
            # Bucket sort.
            for weight in stones:
                buckets[weight] += 1
    
            # Scan through the weights.
            biggest_weight = 0 
            current_weight = max_weight
            while current_weight > 0:
                if buckets[current_weight] == 0:
                    current_weight -= 1
                elif biggest_weight == 0:
                    buckets[current_weight] %= 2
                    if buckets[current_weight] == 1:
                        biggest_weight = current_weight
                    current_weight -= 1
                else:
                    buckets[current_weight] -= 1
                    if biggest_weight - current_weight <= current_weight:
                        buckets[biggest_weight - current_weight] += 1
                        biggest_weight = 0
                    else:
                        biggest_weight -= current_weight
            return biggest_weight
  • 相关阅读:
    java如何得到GET和POST请求URL和参数列表
    Java中,当表单含有文件上传时,提交数据的如何读取
    图片文件,图片文件流和BASE64加密字符串之间的转换,以及图片的BASE64加密字符串再jsp上如何显示
    Multipart/form-data POST文件上传详解(转)
    如何控制微信分享网页时,展示的标题,描述和图片
    微信的分享功能(针对web手机站页面进行的分享功能)
    关于linux下内存使用的一些疑惑[转载]
    【转】《高级前端3.6》JavaScript多线程——Concurrent.Thread.js, WebWork
    【转】javascript中的LHS与RHS
    [转] linux系统中如何进入退出vim编辑器,方法及区别
  • 原文地址:https://www.cnblogs.com/yawenw/p/12709264.html
Copyright © 2011-2022 走看看