You are given an integer array nums
and an integer k
.
In one operation, you can choose any index i
where 0 <= i < nums.length
and change nums[i]
to nums[i] + x
where x
is an integer from the range [-k, k]
. You can apply this operation at most once for each index i
.
The score of nums
is the difference between the maximum and minimum elements in nums
.
Return the minimum score of nums
after applying the mentioned operation at most once for each index in it.
Example 1:
Input: nums = [1], k = 0 Output: 0 Explanation: The score is max(nums) - min(nums) = 1 - 1 = 0.
Example 2:
Input: nums = [0,10], k = 2 Output: 6 Explanation: Change nums to be [2, 8]. The score is max(nums) - min(nums) = 8 - 2 = 6.
Example 3:
Input: nums = [1,3,6], k = 3 Output: 0 Explanation: Change nums to be [4, 4, 4]. The score is max(nums) - min(nums) = 4 - 4 = 0.
Constraints:
1 <= nums.length <= 104
0 <= nums[i] <= 104
0 <= k <= 104
program main
implicit none
integer, parameter :: n = 1
integer, parameter :: k = 0
integer, dimension(n) :: nums
integer :: i, j, x
integer :: score
! Example 1
nums = [1]
write (*, '(A)') 'Example 1:'
write (*, '(A)') 'Input: nums = [1], k = 0'
write (*, '(A)') 'Output:', solve(nums, k)
write (*, '(A)') 'Expected: 0'
write (*, *)
! Example 2
nums = [0, 10]
write (*, '(A)') 'Example 2:'
write (*, '(A)') 'Input: nums = [0, 10], k = 2'
write (*, '(A)') 'Output:', solve(nums, k)
write (*, '(A)') 'Expected: 6'
write (*, *)
! Example 3
nums = [1, 3, 6]
write (*, '(A)') 'Example 3:'
write (*, '(A)') 'Input: nums = [1, 3, 6], k = 3'
write (*, '(A)') 'Output:', solve(nums, k)
write (*, '(A)') 'Expected: 0'
write (*, *)
contains
function solve(nums, k) result(min_score)
implicit none
integer, intent(in) :: nums(:), k
integer :: min_score
integer :: i, j, x
do i = 1, size(nums)
do j = 1, size(nums)
if (i /= j) then
x = nums(j) + k
if (x >= 0 .and. x <= 100) then
nums(j) = x
end if
end if
end do
end do
min_score = maxval(nums) - minval(nums)
end function solve
end program main
temp.f95:47:24: 47 | nums(j) = x | 1 Error: Dummy argument ‘nums’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:19:4: 19 | nums = [0, 10] | 1 Error: Different shape for array assignment at (1) on dimension 1 (1 and 2) temp.f95:27:4: 27 | nums = [1, 3, 6] | 1 Error: Different shape for array assignment at (1) on dimension 1 (1 and 3)
module MinimumScoreTrips
implicit none
private
public :: minimumScoreTrips
contains
function minimumScoreTrips(nums, k) result(min_score)
integer, intent(in) :: nums(:)
integer, intent(in) :: k
integer :: min_score
integer :: max_val, min_val
integer :: i
! Find the maximum and minimum values in the array
max_val = maxval(nums)
min_val = minval(nums)
! Initialize the minimum score to the difference between the maximum and minimum values
min_score = max_val - min_val
! Loop through each index in the array
do i = 1, size(nums)
! Check if the value at the current index is within the range of k
if (abs(nums(i)) <= k) then
! If it is, then calculate the new maximum and minimum values
max_val = max(max_val, nums(i) + k)
min_val = min(min_val, nums(i) - k)
! Calculate the new score
min_score = min(min_score, max_val - min_val)
end if
end do
end function minimumScoreTrips
end module MinimumScoreTrips
! Test the function with the given examples
program test_minimum_score_trips
use MinimumScoreTrips
implicit none
integer, parameter :: nums1(1) = [1]
integer, parameter :: nums2(2) = [0, 10]
integer, parameter :: nums3(3) = [1, 3, 6]
integer, parameter :: k1 = 0
integer, parameter :: k2 = 2
integer, parameter :: k3 = 3
integer :: min_score
! Test case 1:
min_score = minimumScoreTrips(nums1, k1)
write (*,*) "Test case 1:", min_score
! Test case 2:
min_score = minimumScoreTrips(nums2, k2)
write (*,*) "Test case 2:", min_score
! Test case 3:
min_score = minimumScoreTrips(nums3, k3)
write (*,*) "Test case 3:", min_score
end program test_minimum_score_trips
temp.f95:4:31: 4 | public :: minimumScoreTrips | 1 Error: PUBLIC attribute applied to MODULE minimumscoretrips at (1) temp.f95:6:30: 6 | function minimumScoreTrips(nums, k) result(min_score) | 1 Error: MODULE attribute of ‘minimumscoretrips’ conflicts with PROCEDURE attribute at (1) temp.f95:7:38: 7 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:32: 8 | integer, intent(in) :: k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:28: 9 | integer :: min_score | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:35: 10 | integer :: max_val, min_val | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:20: 11 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:30: 14 | max_val = maxval(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:30: 15 | min_val = minval(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:37: 18 | min_score = max_val - min_val | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:21:28: 21 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:23:39: 23 | if (abs(nums(i)) <= k) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:25:51: 25 | max_val = max(max_val, nums(i) + k) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:51: 26 | min_val = min(min_val, nums(i) - k) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:61: 29 | min_score = min(min_score, max_val - min_val) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:15: 30 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:31:11: 31 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:32:7: 32 | end function minimumScoreTrips | 1 Error: Expecting END MODULE statement at (1) temp.f95:37:9: 37 | use MinimumScoreTrips | 1 Fatal Error: Cannot open module file ‘minimumscoretrips.mod’ for reading at (1): No such file or directory compilation terminated.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def middleNode(head):
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
The algorithm uses the tortoise and hare algorithm (slow and fast pointers). The slow pointer moves one step at a time, while the fast pointer moves two steps at a time. Since the fast pointer is moving twice as fast as the slow pointer, when the fast pointer reaches the end of the list, the slow pointer will be in the middle. Thus, we can just return the slow pointer as the middle node. If there are two middle nodes, the slow pointer will be on the second middle node, as specified in the problem statement.
class ListNode {
public:
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* middleNode(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
The algorithm uses the tortoise and hare algorithm (slow and fast pointers). The slow pointer moves one step at a time, while the fast pointer moves two steps at a time. Since the fast pointer is moving twice as fast as the slow pointer, when the fast pointer reaches the end of the list, the slow pointer will be in the middle. Thus, we can just return the slow pointer as the middle node. If there are two middle nodes, the slow pointer will be on the second middle node, as specified in the problem statement.