You are given an integer array nums
and an integer k
. You want to find a subsequence of nums
of length k
that has the largest sum.
Return any such subsequence as an integer array of length k
.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [2,1,3,3], k = 2 Output: [3,3] Explanation: The subsequence has the largest sum of 3 + 3 = 6.
Example 2:
Input: nums = [-1,-2,3,4], k = 3 Output: [-1,3,4] Explanation: The subsequence has the largest sum of -1 + 3 + 4 = 6.
Example 3:
Input: nums = [3,4,3,3], k = 2 Output: [3,4] Explanation: The subsequence has the largest sum of 3 + 4 = 7. Another possible subsequence is [4, 3].
Constraints:
1 <= nums.length <= 1000
-105 <= nums[i] <= 105
1 <= k <= nums.length
program main
implicit none
integer, parameter :: k = 2
integer, dimension(4) :: nums = [2, 1, 3, 3]
integer, dimension(k) :: result
call solve(nums, k, result)
write (*, '(A, I0, A, I0)') 'Result: ', result(1), ',', result(2)
contains
subroutine solve(nums, k, result)
implicit none
integer, intent(in) :: nums(:), k
integer, intent(out) :: result(:)
integer :: i, j, max_sum
logical :: found
do i = 1, size(nums) - k + 1
max_sum = -huge(max_sum)
found = .false.
do j = i, i + k - 1
if (nums(j) > max_sum) then
max_sum = nums(j)
found = .true.
end if
end do
if (found) then
result = nums(i:i+k-1)
return
end if
end do
end subroutine solve
end program main
Result: 2,1
module largest_subsequence
implicit none
contains
function largest_subsequence(nums, k) result(subsequence)
integer, intent(in) :: nums(:), k
integer :: subsequence(k)
! Local variables
integer :: i, j, max_sum, current_sum
! Initialize variables
max_sum = -1000000000
current_sum = 0
! Loop through the array
do i = 1, size(nums) - k + 1
! Calculate the sum of the current subsequence
current_sum = sum(nums(i:i+k-1))
! Check if the sum is greater than the maximum sum
if (current_sum > max_sum) then
! If it is, update the maximum sum and the subsequence
max_sum = current_sum
subsequence = nums(i:i+k-1)
end if
end do
end function largest_subsequence
end module
program largest_subsequence_test
use largest_subsequence
implicit none
integer, parameter :: nums(4) = [2,1,3,3]
integer, parameter :: k = 2
integer :: subsequence(k)
subsequence = largest_subsequence(nums, k)
write (*,*) subsequence
end program
temp.f95:4:30: 4 | function largest_subsequence(nums, k) result(subsequence) | 1 Error: MODULE attribute of ‘largest_subsequence’ conflicts with PROCEDURE attribute at (1) temp.f95:5:37: 5 | integer, intent(in) :: nums(:), k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:6:29: 6 | integer :: subsequence(k) | 1 Error: Explicit array shape at (1) must be constant of INTEGER type and not UNKNOWN type temp.f95:8:41: 8 | integer :: i, j, max_sum, current_sum | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:25: 10 | max_sum = -1000000000 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:11:19: 11 | current_sum = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:13:32: 13 | do i = 1, size(nums) - k + 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:31: 15 | current_sum = sum(nums(i:i+k-1)) | 1 Error: Syntax error in argument list at (1) temp.f95:17:37: 17 | if (current_sum > max_sum) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:19:29: 19 | max_sum = current_sum | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:29: 20 | subsequence = nums(i:i+k-1) | 1 Error: Syntax error in argument list at (1) temp.f95:21:9: 21 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:7: 22 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:5: 23 | end function largest_subsequence | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:7: 27 | use largest_subsequence | 1 Fatal Error: Cannot open module file ‘largest_subsequence.mod’ for reading at (1): No such file or directory compilation terminated.
def count_patterns_in_word(patterns, word):
count = 0
for pattern in patterns:
if pattern in word:
count += 1
return count
The algorithm iterates through each string in the given patterns
array. In each iteration, it checks if the current pattern is a substring of the given word
. If it is, it increases the count.
For C++, we use the find
method of the std::string
class and compare the result to std::string::npos
to check if the pattern is present.
For Java, we use the contains
method of the String
class to check if the pattern is present.
For Python, we use the in
operator to check if the pattern is present in the word.
For JavaScript, we use the includes
method of the String
class to check if the pattern is present.
At the end of the loop, the function returns the count of the patterns that are substrings of the given word.
#include <string>
#include <vector>
int countPatternsInWord(const std::vector<std::string> &patterns, const std::string &word) {
int count = 0;
for (const std::string &pattern : patterns) {
if (word.find(pattern) != std::string::npos) {
count++;
}
}
return count;
}
The algorithm iterates through each string in the given patterns
array. In each iteration, it checks if the current pattern is a substring of the given word
. If it is, it increases the count.
For C++, we use the find
method of the std::string
class and compare the result to std::string::npos
to check if the pattern is present.
For Java, we use the contains
method of the String
class to check if the pattern is present.
For Python, we use the in
operator to check if the pattern is present in the word.
For JavaScript, we use the includes
method of the String
class to check if the pattern is present.
At the end of the loop, the function returns the count of the patterns that are substrings of the given word.