本题的难点在于每次要找到数组中前两个最大的值。提供三种解法:
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