There is a biker going on a road trip. The road trip consists of n + 1
points at different altitudes. The biker starts his trip on point 0
with altitude equal 0
.
You are given an integer array gain
of length n
where gain[i]
is the net gain in altitude between points i
and i + 1
for all (0 <= i < n)
. Return the highest altitude of a point.
Example 1:
Input: gain = [-5,1,5,0,-7] Output: 1 Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
Example 2:
Input: gain = [-4,-3,-2,-1,4,3,2] Output: 0 Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
Constraints:
n == gain.length
1 <= n <= 100
-100 <= gain[i] <= 100
program main
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: gain
integer :: i
! Examples
gain = [-5, 1, 5, 0, -7]
call solve(gain)
gain = [-4, -3, -2, -1, 4, 3, 2]
call solve(gain)
contains
subroutine solve(gain)
implicit none
integer, intent(in) :: gain(:)
integer :: altitude
altitude = 0
do i = 1, size(gain)
altitude = max(altitude, altitude + gain(i))
end do
write (*, '(I0)') altitude
end subroutine solve
end program main
temp.f95:11:4: 11 | gain = [-4, -3, -2, -1, 4, 3, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 7)
module highest_altitude
implicit none
contains
function highest_altitude(gain) result(highest)
integer, intent(in) :: gain(:)
integer :: highest, altitude, i
altitude = 0
highest = 0
do i = 1, size(gain)
altitude = altitude + gain(i)
if (altitude > highest) then
highest = altitude
end if
end do
end function highest_altitude
end module highest_altitude
program test_highest_altitude
use highest_altitude
implicit none
integer :: gain(5)
! Example 1:
gain = [-5, 1, 5, 0, -7]
write (*, '(A, I0)') 'Example 1: ', highest_altitude(gain)
! Example 2:
gain = [-4, -3, -2, -1, 4, 3, 2]
write (*, '(A, I0)') 'Example 2: ', highest_altitude(gain)
end program test_highest_altitude
temp.f95:4:29: 4 | function highest_altitude(gain) result(highest) | 1 Error: MODULE attribute of ‘highest_altitude’ conflicts with PROCEDURE attribute at (1) temp.f95:5:38: 5 | integer, intent(in) :: gain(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:6:39: 6 | integer :: highest, altitude, i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:20: 8 | altitude = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:9:19: 9 | highest = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:11:28: 11 | do i = 1, size(gain) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:12:41: 12 | altitude = altitude + gain(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:13:40: 13 | if (altitude > highest) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:14:34: 14 | highest = altitude | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:15: 15 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:16:11: 16 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:17:7: 17 | end function highest_altitude | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:9: 21 | use highest_altitude | 1 Fatal Error: Cannot open module file ‘highest_altitude.mod’ for reading at (1): No such file or directory compilation terminated.
def min_operations(n):
operations = 0
while n > 0:
if n % 2 == 0:
n //= 2
else:
n -= 1
operations += 1
return operations
The algorithm initializes a variable operations
to store the number of operations taken. It then enters a loop that iterates while n
is greater than 0. In each iteration, it checks whether n
is divisible by 2 (i.e., even). If it is, the algorithm divides n
by 2; otherwise, it decrements n
by 1. In each loop iteration, the algorithm also increments the operations
counter. The loop ends when n
reaches 0, and the algorithm returns the total count of operations performed.
This algorithm finds the minimum number of operations required because dividing n
by 2 is more optimal than decrementing n
by 1 whenever n
is even, as it reduces the number more rapidly.
int min_operations(int n) {
int operations = 0;
while (n > 0) {
if (n % 2 == 0) {
n /= 2;
} else {
n--;
}
operations++;
}
return operations;
}
The algorithm initializes a variable operations
to store the number of operations taken. It then enters a loop that iterates while n
is greater than 0. In each iteration, it checks whether n
is divisible by 2 (i.e., even). If it is, the algorithm divides n
by 2; otherwise, it decrements n
by 1. In each loop iteration, the algorithm also increments the operations
counter. The loop ends when n
reaches 0, and the algorithm returns the total count of operations performed.
This algorithm finds the minimum number of operations required because dividing n
by 2 is more optimal than decrementing n
by 1 whenever n
is even, as it reduces the number more rapidly.