Given an integer array nums
of size n
, return the number with the value closest to 0
in nums
. If there are multiple answers, return the number with the largest value.
Example 1:
Input: nums = [-4,-2,1,4,8] Output: 1 Explanation: The distance from -4 to 0 is |-4| = 4. The distance from -2 to 0 is |-2| = 2. The distance from 1 to 0 is |1| = 1. The distance from 4 to 0 is |4| = 4. The distance from 8 to 0 is |8| = 8. Thus, the closest number to 0 in the array is 1.
Example 2:
Input: nums = [2,-1,1] Output: 1 Explanation: 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.
Constraints:
1 <= n <= 1000
-105 <= nums[i] <= 105
program main
! Solve the problem: Given an integer array `nums` of size `n`, return the number with the value closest to 0 in `nums`. If there are multiple answers, return the number with the largest value.
implicit none
integer, parameter :: n = 5
integer, dimension(n) :: nums
integer :: i
nums = [-4, -2, 1, 4, 8]
call solve(nums)
nums = [2, -1, 1]
call solve(nums)
contains
subroutine solve(nums)
implicit none
integer, dimension(:), intent(in) :: nums
integer :: closest, largest
integer :: i
closest = nums(1)
largest = nums(1)
do i = 2, size(nums)
if (abs(nums(i)) < abs(closest)) then
closest = nums(i)
end if
if (abs(nums(i)) > abs(largest)) then
largest = nums(i)
end if
end do
print '(I0)', closest
print '(I0)', largest
end subroutine solve
end program main
temp.f95:11:4: 11 | nums = [2, -1, 1] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 3)
module closest_number
contains
function closest_number(nums) result(closest)
implicit none
integer, intent(in) :: nums(:)
integer :: closest, i, j, diff, min_diff
logical :: found
! Initialize closest to the first element
closest = nums(1)
! Loop through the array and find the closest number to 0
do i = 2, size(nums)
diff = abs(nums(i) - 0)
if (diff < min_diff .or. min_diff == -1) then
closest = nums(i)
min_diff = diff
end if
end do
! If there are multiple closest numbers, return the one with the largest value
if (min_diff == 0) then
do i = 1, size(nums)
if (nums(i) == closest) then
found = .true.
exit
end if
end do
if (.not. found) then
do i = 1, size(nums)
if (nums(i) > closest) then
closest = nums(i)
end if
end do
end if
end if
end function closest_number
end module closest_number
program test_closest_number
use closest_number
implicit none
integer, parameter :: n = 5
integer :: nums(n) = [ -4, -2, 1, 4, 8 ]
integer :: closest
closest = closest_number(nums)
write (*,*) 'Closest number to 0 in nums:', closest
nums = [ 2, -1, 1 ]
closest = closest_number(nums)
write (*,*) 'Closest number to 0 in nums:', closest
nums = [ 1, 2, 3, 4, 5 ]
closest = closest_number(nums)
write (*,*) 'Closest number to 0 in nums:', closest
nums = [ -1, -2, -3, -4, -5 ]
closest = closest_number(nums)
write (*,*) 'Closest number to 0 in nums:', closest
end program test_closest_number
temp.f95:5:25: 5 | function closest_number(nums) result(closest) | 1 Error: MODULE attribute of ‘closest_number’ conflicts with PROCEDURE attribute at (1) temp.f95:6:17: 6 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:7:34: 7 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:44: 8 | integer :: closest, i, j, diff, min_diff | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:20: 9 | logical :: found | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:21: 12 | closest = nums(1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:24: 15 | do i = 2, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:29: 16 | diff = abs(nums(i) - 0) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:51: 17 | if (diff < min_diff .or. min_diff == -1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:25: 18 | closest = nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:23: 19 | min_diff = diff | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:9: 20 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:7: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:27: 24 | if (min_diff == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:25:26: 25 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:26:36: 26 | if (nums(i) == closest) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:27:24: 27 | found = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:14: 28 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:29:11: 29 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:30:9: 30 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:31:27: 31 | if (.not. found) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:32:28: 32 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:33:37: 33 | if (nums(i) > closest) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:34:29: 34 | closest = nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:13: 35 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:36:11: 36 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:37:9: 37 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:38:7: 38 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:40:5: 40 | end function closest_number | 1 Error: Expecting END MODULE statement at (1) temp.f95:45:7: 45 | use closest_number | 1 Fatal Error: Cannot open module file ‘closest_number.mod’ for reading at (1): No such file or directory compilation terminated.
def robot_instructions(n: int, start_pos: list, s: str) -> list:
answer = [0] * len(s)
for idx in range(len(s)):
row, col = start_pos
for i in range(idx, len(s)):
move = s[i]
if move == 'L':
col -= 1
elif move == 'R':
col += 1
elif move == 'U':
row -= 1
elif move == 'D':
row += 1
if row < 0 or row >= n or col < 0 or col >= n:
break
answer[idx] += 1
return answer
This algorithm implements a nested loop. The outer loop iterates through each starting instruction index. In the inner loop, we execute the instructions while updating the position of the robot in terms of row and column. If a move will take the robot outside the grid, we break from the inner loop. If the robot hasn't exited the grid, we increase the number of instructions it has executed, which is stored in the answer array indexed by the starting instruction index.
The time complexity is O(m^2), where m is the length of the instructions string s. The space complexity is O(m), as we need to store the answer for each starting instruction index in an array.
#include <vector>
#include <string>
std::vector<int> robotInstructions(int n, std::vector<int>& startPos, const std::string& s) {
std::vector<int> answer(s.length(), 0);
for (int idx = 0; idx < s.length(); ++idx) {
int row = startPos[0];
int col = startPos[1];
for (int i = idx; i < s.length(); ++i) {
if (s[i] == 'L') --col;
else if (s[i] == 'R') ++col;
else if (s[i] == 'U') --row;
else if (s[i] == 'D') ++row;
if (row < 0 || row >= n || col < 0 || col >= n) {
break;
}
answer[idx]++;
}
}
return answer;
}
This algorithm implements a nested loop. The outer loop iterates through each starting instruction index. In the inner loop, we execute the instructions while updating the position of the robot in terms of row and column. If a move will take the robot outside the grid, we break from the inner loop. If the robot hasn't exited the grid, we increase the number of instructions it has executed, which is stored in the answer array indexed by the starting instruction index.
The time complexity is O(m^2), where m is the length of the instructions string s. The space complexity is O(m), as we need to store the answer for each starting instruction index in an array.