Given an integer array nums
, return true
if nums
is consecutive, otherwise return false
.
An array is consecutive if it contains every number in the range [x, x + n - 1]
(inclusive), where x
is the minimum number in the array and n
is the length of the array.
Example 1:
Input: nums = [1,3,4,2] Output: true Explanation: The minimum value is 1 and the length of nums is 4. All of the values in the range [x, x + n - 1] = [1, 1 + 4 - 1] = [1, 4] = (1, 2, 3, 4) occur in nums. Therefore, nums is consecutive.
Example 2:
Input: nums = [1,3] Output: false Explanation: The minimum value is 1 and the length of nums is 2. The value 2 in the range [x, x + n - 1] = [1, 1 + 2 - 1], = [1, 2] = (1, 2) does not occur in nums. Therefore, nums is not consecutive.
Example 3:
Input: nums = [3,5,4] Output: true Explanation: The minimum value is 3 and the length of nums is 3. All of the values in the range [x, x + n - 1] = [3, 3 + 3 - 1] = [3, 5] = (3, 4, 5) occur in nums. Therefore, nums is consecutive.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 105
program main
implicit none
integer, parameter :: n = 4
integer, dimension(n) :: nums
logical :: result
! Example 1
nums = [1, 3, 4, 2]
result = is_consecutive(nums)
write (*, '(A, L1)') 'Example 1: ', result
! Example 2
nums = [1, 3]
result = is_consecutive(nums)
write (*, '(A, L1)') 'Example 2: ', result
! Example 3
nums = [3, 5, 4]
result = is_consecutive(nums)
write (*, '(A, L1)') 'Example 3: ', result
contains
function is_consecutive(nums) result(result)
implicit none
integer, dimension(:), intent(in) :: nums
logical :: result
integer :: x, n
x = minval(nums)
n = size(nums)
result = all([x, x + n - 1] == nums)
end function is_consecutive
end program main
temp.f95:14:4: 14 | nums = [1, 3] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 2) temp.f95:19:4: 19 | nums = [3, 5, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 3)
module is_consecutive
implicit none
contains
function is_consecutive(nums) result(is_consecutive)
integer, intent(in) :: nums(:)
logical :: is_consecutive
! Variables
integer :: x, n, i
! Initialize variables
x = minval(nums)
n = size(nums)
is_consecutive = .true.
! Check if all values in the range [x, x + n - 1] occur in nums
do i = x, x + n - 1
if (.not. any(nums == i)) then
is_consecutive = .false.
exit
end if
end do
end function is_consecutive
end module is_consecutive
program test_is_consecutive
use is_consecutive, only : is_consecutive
implicit none
! Test case 1:
print *, is_consecutive([1, 3, 4, 2])
! Expected output:
! .true.
! Test case 2:
print *, is_consecutive([1, 3])
! Expected output:
! .false.
! Test case 3:
print *, is_consecutive([3, 5, 4])
! Expected output:
! .true.
end program test_is_consecutive
temp.f95:7:23: 7 | function is_consecutive(nums) result(is_consecutive) | 1 Error: MODULE attribute of āis_consecutiveā conflicts with PROCEDURE attribute at (1) temp.f95:9:30: 9 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:25: 10 | logical :: is_consecutive | 1 Error: Symbol āis_consecutiveā at (1) has already been host associated temp.f95:13:18: 13 | integer :: x, n, i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:16:16: 16 | x = minval(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:14: 17 | n = size(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:15: 18 | is_consecutive = .true. | 1 Error: Symbol āis_consecutiveā at (1) has already been host associated temp.f95:21:19: 21 | do i = x, x + n - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:22:34: 22 | if (.not. any(nums == i)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:23:23: 23 | is_consecutive = .false. | 1 Error: Symbol āis_consecutiveā at (1) has already been host associated temp.f95:24:12: 24 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:25:7: 25 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:3: 26 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:3: 28 | end function is_consecutive | 1 Error: Expecting END MODULE statement at (1) temp.f95:34:41: 34 | use is_consecutive, only : is_consecutive | 1 Error: The name āis_consecutiveā at (1) has already been used as an external module name temp.f95:38:8: 1 | module is_consecutive | 2 ...... 38 | print *, is_consecutive([1, 3, 4, 2]) | 1 Error: Global name āis_consecutiveā at (1) is already being used as a MODULE at (2) temp.f95:38:23: 38 | print *, is_consecutive([1, 3, 4, 2]) | 1 Warning: Interface mismatch in global procedure āis_consecutiveā at (1): 'is_consecutive' is not a function temp.f95:38:8: 38 | print *, is_consecutive([1, 3, 4, 2]) | 1 Error: Function āis_consecutiveā at (1) has no IMPLICIT type temp.f95:43:8: 1 | module is_consecutive | 2 ...... 43 | print *, is_consecutive([1, 3]) | 1 Error: Global name āis_consecutiveā at (1) is already being used as a MODULE at (2) temp.f95:38:23: 38 | print *, is_consecutive([1, 3, 4, 2]) | 1 Warning: Interface mismatch in global procedure āis_consecutiveā at (1): 'is_consecutive' is not a function temp.f95:43:8: 43 | print *, is_consecutive([1, 3]) | 1 Error: Function āis_consecutiveā at (1) has no IMPLICIT type temp.f95:48:8: 1 | module is_consecutive | 2 ...... 48 | print *, is_consecutive([3, 5, 4]) | 1 Error: Global name āis_consecutiveā at (1) is already being used as a MODULE at (2) temp.f95:38:23: 38 | print *, is_consecutive([1, 3, 4, 2]) | 1 Warning: Interface mismatch in global procedure āis_consecutiveā at (1): 'is_consecutive' is not a function temp.f95:48:8: 48 | print *, is_consecutive([3, 5, 4]) | 1 Error: Function āis_consecutiveā at (1) has no IMPLICIT type temp.f95:38:8: 38 | print *, is_consecutive([1, 3, 4, 2]) | 1 Error: More actual than formal arguments in procedure call at (1) temp.f95:43:8: 43 | print *, is_consecutive([1, 3]) | 1 Error: More actual than formal arguments in procedure call at (1) temp.f95:48:8: 48 | print *, is_consecutive([3, 5, 4]) | 1 Error: More actual than formal arguments in procedure call at (1)
from bisect import bisect_left
def maxFruits(fruits, startPos, k):
n = len(fruits)
left, right = [0] * (n + 1), [0] * (n + 1)
j = 0
for i in range(n):
while j < n and fruits[j][0] - fruits[i][0] <= k:
right[i + 1] += fruits[j][1]
j += 1
right[i + 1] += right[i]
if j < n and fruits[j][0] - startPos <= k:
right[0] += fruits[j][1]
j += 1
j = n - 1
for i in range(n - 1, -1, -1):
while j >= 0 and fruits[j][0] - fruits[i][0] <= k:
left[i] += fruits[j][1]
j -= 1
left[i] += left[i + 1]
ans = 0
for i in range(n):
rest = max(0, k - abs(fruits[i][0] - startPos))
idx = bisect_left(fruits, [fruits[i][0] + rest, 0])
ans = max(ans, left[i] + right[idx])
return ans
1. Create left and right arrays of size n+1 filled with zeros, to store the cumulative fruits at each position.
#include <vector>
#include <algorithm>
using namespace std;
int maxFruits(vector<vector<int>>& fruits, int startPos, int k) {
int n = fruits.size();
vector<int> left(n + 1, 0), right(n + 1, 0);
int j = 0;
for (int i = 0; i < n; i++) {
while (j < n && fruits[j][0] - fruits[i][0] <= k) {
right[i + 1] += fruits[j][1];
j++;
}
right[i + 1] += right[i];
if (j < n && fruits[j][0] - startPos <= k) {
right[0] += fruits[j][1];
j++;
}
}
j = n - 1;
for (int i = n - 1; i >= 0; i--) {
while (j >= 0 && fruits[j][0] - fruits[i][0] <= k) {
left[i] += fruits[j][1];
j--;
}
left[i] += left[i + 1];
}
int ans = 0;
for (int i = 0; i < n; i++) {
int rest = max(0, k - abs(fruits[i][0] - startPos));
int idx = upper_bound(fruits.begin(), fruits.end(), vector<int>{fruits[i][0] + rest, 0}) - fruits.begin();
ans = max(ans, left[i] + right[idx]);
}
return ans;
}
1. Create left and right arrays of size n+1 filled with zeros, to store the cumulative fruits at each position.