Given an unsorted array of integers nums
, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
A continuous increasing subsequence is defined by two indices l
and r
(l < r
) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]
and for each l <= i < r
, nums[i] < nums[i + 1]
.
Example 1:
Input: nums = [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3. Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element 4.
Example 2:
Input: nums = [2,2,2,2,2] Output: 1 Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly increasing.
Constraints:
1 <= nums.length <= 104
-109 <= nums[i] <= 109
program main
! Solves the problem:
! Given an unsorted array of integers nums, return the length of the longest
! continuous increasing subsequence (i.e., subarray). The subsequence must be
! strictly increasing.
implicit none
integer :: nums(10), len_longest_subseq
nums = [1, 3, 5, 4, 7]
print '(I0)', len_longest_subseq(nums)
nums = [2, 2, 2, 2, 2]
print '(I0)', len_longest_subseq(nums)
contains
function len_longest_subseq(nums) result(len)
implicit none
integer, intent(in) :: nums(:)
integer :: len, i, j, n
n = size(nums)
if (n == 0) then
len = 0
return
end if
len = 1
do i = 2, n
if (nums(i-1) < nums(i)) then
len = len + 1
else
j = i - 1
do while (j > 1 .and. nums(j-1) >= nums(j))
j = j - 1
end do
len = max(len, i - j)
end if
end do
end function len_longest_subseq
end program main
temp.f95:18:32: 18 | function len_longest_subseq(nums) result(len) | 1 Error: Procedure ‘len_longest_subseq’ at (1) has an explicit interface from a previous declaration temp.f95:19:21: 19 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:20:38: 20 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:21:31: 21 | integer :: len, i, j, n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:22: 23 | n = size(nums) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:24:24: 24 | if (n == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:25:19: 25 | len = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:18: 26 | return | 1 Error: Unexpected RETURN statement in CONTAINS section at (1) temp.f95:27:11: 27 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:29:15: 29 | len = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:30:19: 30 | do i = 2, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:31:41: 31 | if (nums(i-1) < nums(i)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:32:29: 32 | len = len + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:16: 33 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:34:25: 34 | j = i - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:59: 35 | do while (j > 1 .and. nums(j-1) >= nums(j)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:36:29: 36 | j = j - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:37:19: 37 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:38:37: 38 | len = max(len, i - j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:39:15: 39 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:40:11: 40 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:41:7: 41 | end function len_longest_subseq | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:10:4: 10 | nums = [1, 3, 5, 4, 7] | 1 Error: Different shape for array assignment at (1) on dimension 1 (10 and 5) temp.f95:13:4: 13 | nums = [2, 2, 2, 2, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (10 and 5)
module longest_continuous_increasing_subsequence
implicit none
contains
integer function longest_increasing_subsequence(nums) result(ans)
! Given an unsorted array of integers `nums`, return the length of the longest
! continuous increasing subsequence (i.e. subarray)
! A continuous increasing subsequence is defined by two indices `l` and `r` (`l < r`) such that it is
! `[nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]` and for each `l <= i < r`, `nums[i] < nums[i + 1]`.
! Example 1:
! Input: nums = [1,3,5,4,7]
! Output: 3
! Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
! Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element 4.
! Example 2:
! Input: nums = [2,2,2,2,2]
! Output: 1
! Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly increasing.
! Constraints:
! 1 <= nums.length <= 104
! -109 <= nums[i] <= 109
integer, intent(in) :: nums(:)
! dp(i) represents the length of the longest continuous increasing subsequence ending at index i
integer :: dp(size(nums))
! Initialize dp(0) to 1, as there is always a continuous increasing subsequence of length 1 ending at index 0
dp(0) = 1
! Loop through the array and update dp(i) for each index i
do i = 1, size(nums)
! If the current element is greater than the previous element,
! then the current element can be added to the subsequence
! ending at index i - 1, so dp(i) = dp(i - 1) + 1
if (nums(i) > nums(i - 1)) then
dp(i) = dp(i - 1) + 1
! Otherwise, the current element cannot be added to the subsequence
! ending at index i - 1, so dp(i) = 1
else
dp(i) = 1
end if
end do
! Return the maximum value in dp
ans = maxval(dp)
end function longest_increasing_subsequence
end module longest_continuous_increasing_subsequence
! Test the longest_increasing_subsequence function with the following examples:
program test_longest_increasing_subsequence
use longest_continuous_increasing_subsequence, only: longest_increasing_subsequence
implicit none
! Examples taken from the problem statement
integer, parameter :: example1(5) = [1, 3, 5, 4, 7]
integer, parameter :: example2(5) = [2, 2, 2, 2, 2]
! Output the length of the longest continuous increasing subsequence for each example
write (*,*) "Example 1:", longest_increasing_subsequence(example1)
write (*,*) "Example 2:", longest_increasing_subsequence(example2)
end program test_longest_increasing_subsequence
temp.f95:39:4: 39 | do i = 1, size(nums) | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:36:3: 36 | dp(0) = 1 | 1 Warning: Array reference at (1) is out of bounds (0 < 1) in dimension 1 temp.f95:62:5: 62 | use longest_continuous_increasing_subsequence, only: longest_increasing_subsequence | 1 Fatal Error: Cannot open module file ‘longest_continuous_increasing_subsequence.mod’ for reading at (1): No such file or directory compilation terminated.
def findLengthOfLCIS(nums):
if not nums:
return 0
result, current = 1, 1
for i in range(1, len(nums)):
current = current + 1 if nums[i] > nums[i - 1] else 1
result = max(result, current)
return result
The algorithm works as follows:
result
(to hold the maximum length found so far) and current
(to hold the length of the current increasing subsequence). Both are initially set to 1.This algorithm has a linear time complexity, i.e., O(n), where n is the length of the input array.
int findLengthOfLCIS(vector<int>& nums) {
if (nums.empty()) return 0;
int result = 1, current = 1;
for (int i = 1; i < nums.size(); ++i) {
current = nums[i] > nums[i - 1] ? current + 1 : 1;
result = max(result, current);
}
return result;
}
The algorithm works as follows:
result
(to hold the maximum length found so far) and current
(to hold the length of the current increasing subsequence). Both are initially set to 1.This algorithm has a linear time complexity, i.e., O(n), where n is the length of the input array.