You are given an array of integers stones
where stones[i]
is the weight of the ith
stone.
We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x
and y
with x <= y
. The result of this smash is:
x == y
, both stones are destroyed, andx != y
, the stone of weight x
is destroyed, and the stone of weight y
has new weight y - x
.At the end of the game, there is at most one stone left.
Return the weight of the last remaining stone. If there are no stones left, return 0
.
Example 1:
Input: stones = [2,7,4,1,8,1] Output: 1 Explanation: We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then, we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then, we combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
Example 2:
Input: stones = [1] Output: 1
Constraints:
1 <= stones.length <= 30
1 <= stones[i] <= 1000
program main
implicit none
integer, parameter :: n_examples = 2
integer, parameter :: stones(n_examples) = reshape([2, 7, 4, 1, 8, 1], shape=[n_examples])
integer :: i
do i = 1, n_examples
print '(A, I0)', 'Example ', i
print '(A, I0)', 'Stones: ', stones(i)
print '(A, I0)', 'Result: ', solve(stones(i))
print *, ''
end do
contains
function solve(stones) result(result)
implicit none
integer, intent(in) :: stones(:)
integer :: result, x, y
if (size(stones) == 1) then
result = stones(1)
else
x = maxval(stones)
y = minval(stones)
if (x == y) then
result = 0
else
if (x < y) then
result = y - x
else
result = x
end if
end if
end if
end function solve
end program main
temp.f95:11:43: 11 | print '(A, I0)', 'Result: ', solve(stones(i)) | 1 Error: Rank mismatch in argument ‘stones’ at (1) (rank-1 and scalar)
module smashing_stones
implicit none
contains
function smashing_stones(stones) result(last_stone)
integer, intent(in) :: stones(:)
integer :: last_stone, i, j, x, y
! Sort the stones in descending order
call sort(stones)
! Initialize the last stone
last_stone = stones(1)
! Loop through the stones and smash the heaviest two
do i = 2, size(stones)
x = stones(i - 1)
y = stones(i)
! If x == y, both stones are destroyed
if (x == y) then
last_stone = 0
exit
! If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x
else
last_stone = y - x
end if
end do
end function smashing_stones
end module smashing_stones
program test_smashing_stones
use smashing_stones
implicit none
integer :: stones(6)
! Test case 1
stones = [2, 7, 4, 1, 8, 1]
write (*,*) 'Test case 1:', smashing_stones(stones)
! Test case 2
stones = [1]
write (*,*) 'Test case 2:', smashing_stones(stones)
! Test case 3
stones = [2, 1, 1, 1, 1, 1]
write (*,*) 'Test case 3:', smashing_stones(stones)
! Test case 4
stones = [2, 7, 4, 1, 8, 1, 2]
write (*,*) 'Test case 4:', smashing_stones(stones)
! Test case 5
stones = [2, 1, 1, 1, 1, 1, 2]
write (*,*) 'Test case 5:', smashing_stones(stones)
end program test_smashing_stones
temp.f95:5:24: 5 | function smashing_stones(stones) result(last_stone) | 1 Error: MODULE attribute of ‘smashing_stones’ conflicts with PROCEDURE attribute at (1) temp.f95:6:32: 6 | integer, intent(in) :: stones(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:7:33: 7 | integer :: last_stone, i, j, x, y | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:17: 10 | call sort(stones) | 1 Error: Unexpected CALL statement in CONTAINS section at (1) temp.f95:13:22: 13 | last_stone = stones(1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:22: 16 | do i = 2, size(stones) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:21: 17 | x = stones(i - 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:17: 18 | y = stones(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:21:20: 21 | if (x == y) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:22:22: 22 | last_stone = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:12: 23 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:25:8: 25 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:26:26: 26 | last_stone = y - x | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:27:7: 27 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:3: 28 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:30:3: 30 | end function smashing_stones | 1 Error: Expecting END MODULE statement at (1) temp.f95:35:5: 35 | use smashing_stones | 1 Fatal Error: Cannot open module file ‘smashing_stones.mod’ for reading at (1): No such file or directory compilation terminated.
def longestOnes(nums, k):
left, right, max_ones, zero_count = 0, 0, 0, 0
while right < len(nums):
if nums[right] == 0:
zero_count += 1
while zero_count > k:
if nums[left] == 0:
zero_count -= 1
left += 1
max_ones = max(max_ones, right - left + 1)
right += 1
return max_ones
The algorithm for this problem is a sliding window solution. We iterate through the array, and for each iteration, we check if the current element is a 0. If it is, we increment the zeroCount. If the zeroCount exceeds k, we move the left pointer of the sliding window forwards, checking if the element we are leaving is a 0, and decrementing the zeroCount accordingly. We then calculate and store the maximum consecutive 1's within the sliding window for each iteration, incrementing the right pointer of the sliding window at every step. Finally, we return the maximum consecutive 1's after iterating through the whole array. The algorithm focuses on processing the array in a single pass and adjusting window boundaries accordingly. Each language implementation follows this algorithm and uses similar variable names for convenience.
int longestOnes(vector<int>& nums, int k) {
int left = 0, right = 0, maxOnes = 0, zeroCount = 0;
while (right < nums.size()) {
if (nums[right] == 0) zeroCount++;
while (zeroCount > k) {
if (nums[left] == 0) zeroCount--;
left++;
}
maxOnes = max(maxOnes, right - left + 1);
right++;
}
return maxOnes;
}
The algorithm for this problem is a sliding window solution. We iterate through the array, and for each iteration, we check if the current element is a 0. If it is, we increment the zeroCount. If the zeroCount exceeds k, we move the left pointer of the sliding window forwards, checking if the element we are leaving is a 0, and decrementing the zeroCount accordingly. We then calculate and store the maximum consecutive 1's within the sliding window for each iteration, incrementing the right pointer of the sliding window at every step. Finally, we return the maximum consecutive 1's after iterating through the whole array. The algorithm focuses on processing the array in a single pass and adjusting window boundaries accordingly. Each language implementation follows this algorithm and uses similar variable names for convenience.