代码(Python3) class Solution: def lastStoneWeight(self, stones: List[int]) -> int: # 由于 heapq 默认是最小堆,所以这里取相反数进行转换 heap: List[int] = [-num for num in stones] # 通过数组直接建立堆 heapq.heapify(heap) # 至少还有两个数字时,继续循环处理 while len(heap) > 1: # ...
#include<queue> #include<vector> using namespace std; class Solution { public: int lastStoneWeight(vector<int>& stones) { std::priority_queue<int> table(stones.begin(), stones.end()); while(table.size() > 1){ // Break when only 1 left; or no left in the queue int first_value ...
最后选出 1 和 1,得到 0,最终数组转换为 [1],这就是最后剩下那块石头的重量。 classSolution:deflastStoneWeight(self,stones:list[int])->int:whileTrue:iflen(stones)<=1:breakstones.sort()m1=stones.pop()m2=stones.pop()temp=m1-m2iftemp>=0:stones.append(temp)iflen(stones)==1:r=stones[0...
1classSolution {2public:3intlastStoneWeight(vector<int>&stones) {45while(stones.size()>1)6{7sort(stones.begin(),stones.end());8if(stones[stones.size()-1]==stones[stones.size()-2])9{10stones.pop_back();stones.pop_back();11}12else13{14inttt = stones[stones.size()-1]-stones[s...
classSolution{public:intlastStoneWeight(vector<int>& stones){ priority_queue<int> q;for(intstone : stones) q.push(stone);while(q.size() >1) {intfirst = q.top(); q.pop();intsecond = q.top(); q.pop();if(first > second) q.push(first - second); ...
class Solution{public:intlastStoneWeightII(vector<int>&stones){intsum=accumulate(stones.begin(),stones.end(),0);vector<int>dp((sum>>1)+1);for(constauto&stone:stones)for(intj=(sum>>1);j>stone-1;j--)dp[j]=max(dp[j],dp[j-stone]+stone);returnsum-(dp[(sum>>1)]<<1);}}; ...
1049. Last Stone Weight II刷题笔记 class Solution: def lastStoneWeightII(self, stones: List[int]) -> int: sumweight = sum(stones) target = sumweight//2 dp = [0]*(target+1) for i in range(len(stones)): for j in range(target,stones[i]-1,-1):...
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone. 1. 2. 3. 4. 5. 6. 7. Note: 1 <= stones.length <= 30 1 <= stones[i] <= 1000 classSolution {publicintlastStoneWeight(int[] A) { ...
Project Solution Capability graphic design, 3D model design, total solution for projects, Cross Categories Consolidation Application School Design Style Traditional Material Porcelain Tiles Function Acid-Resistant, Antibacterial, Heat Insulation, Non-Slip, Firebrick, Wear-Resistant Surface Treatment Glazed Tiles...
The best of National Geographic delivered to your inbox Sign up for more inspiring photos, stories, and special offers from National Geographic.Sign Up Legal Terms of Use Privacy Policy Interest-Based Ads Our Sites Nat Geo Home Attend a Live Event Book a Trip Buy Maps Inspire ...