/** * @param {number[]}stones * @return {number} */ var lastStoneWeight = function (stones) { stones.sort((a, b) => b - a); while (stones.length > 1) { const num = stones.splice(0, 2).reduce((a, b) => a - b); for (let i = 0; i < stones.length; i++) { if (num >= stones[i]) { stones.splice(i, 0, num); break; } if (i === stones.length - 1) { stones.push(num); break; } } if (stones.length === 0) stones.push(num); } return stones[0]; };