A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
You are given an array of strings sentences
, where each sentences[i]
represents a single sentence.
Return the maximum number of words that appear in a single sentence.
Example 1:
Input: sentences = [ "alice and bob love leetcode ", "i think so too ", "this is great thanks very much "] Output: 6 Explanation: - The first sentence, "alice and bob love leetcode ", has 5 words in total. - The second sentence, "i think so too ", has 4 words in total. - The third sentence, "this is great thanks very much ", has 6 words in total. Thus, the maximum number of words in a single sentence comes from the third sentence, which has 6 words.
Example 2:
Input: sentences = [ "please wait ", "continue to fight ", "continue to win "] Output: 3 Explanation: It is possible that multiple sentences contain the same number of words. In this example, the second and third sentences (underlined) have the same number of words.
Constraints:
1 <= sentences.length <= 100
1 <= sentences[i].length <= 100
sentences[i]
consists only of lowercase English letters and ' '
only.sentences[i]
does not have leading or trailing spaces.sentences[i]
are separated by a single space.program main
implicit none
integer :: i, j, n, m
character(len=100), allocatable :: sentences(:)
integer, allocatable :: counts(:)
call get_examples()
contains
subroutine get_examples()
implicit none
integer, parameter :: num_examples = 2
character(len=100), parameter :: examples(num_examples, *) = reshape([character(len=100) :: &
["alice and bob love leetcode ", "i think so too ", "this is great thanks very much "], &
["please wait ", "continue to fight ", "continue to win "]], shape(examples))
do i = 1, num_examples
allocate(sentences(size(examples(i, :))))
do j = 1, size(sentences)
sentences(j) = examples(i, j)
end do
write(*, "(A)", advance="no") "Example #", i, ": "
write(*, "(A)") solve(sentences)
end do
end subroutine get_examples
function solve(sentences) result(max_words)
implicit none
character(len=*), intent(in) :: sentences(:)
integer :: max_words
integer :: i, n
character(len=:), allocatable :: words(:)
n = size(sentences)
allocate(counts(n))
do i = 1, n
call split(sentences(i), words)
counts(i) = size(words)
end do
max_words = maxval(counts)
end function solve
subroutine split(str, words)
implicit none
character(len=*), intent(in) :: str
character(len=:), allocatable, intent(out) :: words(:)
integer :: i, pos
allocate(words(0))
do while (str /= "")
pos = index(str, " ")
if (pos == 0) then
words = [words, trim(str)]
exit
else
words = [words, trim(str(:pos-1))]
str = str(pos+1:)
end if
end do
end subroutine split
end program main
temp.f95:18:28: 18 | ["please wait ", "continue to fight ", "continue to win "]], shape(examples)) | 1 Error: Different CHARACTER lengths (12/18) in array constructor at (1) temp.f95:21:49: 21 | allocate(sentences(size(examples(i, :)))) | 1 Error: Syntax error in argument list at (1) temp.f95:58:17: 58 | allocate(words(0)) | 1 Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag temp.f95:67:16: 67 | str = str(pos+1:) | 1 Error: Dummy argument ‘str’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:23:31: 23 | sentences(j) = examples(i, j) | 1 Error: Function ‘examples’ at (1) has no IMPLICIT type
module MaxWords
implicit none
contains
function max_words(sentences) result(max_words)
character(len=*), intent(in) :: sentences(:)
integer :: max_words, words
integer :: i, j
max_words = 0
do i = 1, size(sentences)
words = 0
do j = 1, len_trim(sentences(i))
if (sentences(i)(j:j) == ' ') then
words = words + 1
end if
end do
if (words > max_words) then
max_words = words
end if
end do
end function max_words
end module MaxWords
program test_max_words
use MaxWords
implicit none
character(len=*), parameter :: sentences(3) = ["alice and bob love leetcode ", "i think so too ", "this is great thanks very much "]
integer :: max_words
max_words = max_words(sentences)
write (*,*) "Max words:", max_words
max_words = max_words(["please wait ", "continue to fight ", "continue to win "])
write (*,*) "Max words:", max_words
end program test_max_words
temp.f95:4:49: 4 | function max_words(sentences) result(max_words) | 1 Error: RESULT variable at (1) must be different than function name temp.f95:5:48: 5 | character(len=*), intent(in) :: sentences(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:6:25: 6 | integer :: max_words, words | 1 Error: Symbol ‘max_words’ at (1) has already been host associated temp.f95:7:19: 7 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:14: 9 | max_words = 0 | 1 Error: Symbol ‘max_words’ at (1) has already been host associated temp.f95:10:29: 10 | do i = 1, size(sentences) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:11:15: 11 | words = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:12:38: 12 | do j = 1, len_trim(sentences(i)) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:13:25: 13 | if (sentences(i)(j:j) == ' ') then | 1 Error: Syntax error in IF-expression at (1) temp.f95:14:27: 14 | words = words + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:11: 15 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:16:9: 16 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:17:28: 17 | if (words > max_words) then | 1 Error: Function ‘max_words’ requires an argument list at (1) temp.f95:18:18: 18 | max_words = words | 1 Error: Symbol ‘max_words’ at (1) has already been host associated temp.f95:19:9: 19 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:20:7: 20 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:5: 21 | end function max_words | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:7: 25 | use MaxWords | 1 Fatal Error: Cannot open module file ‘maxwords.mod’ for reading at (1): No such file or directory compilation terminated.
def min_sessions(tasks, session_time):
sessions = []
tasks.sort(reverse=True)
for task in tasks:
added = False
for session_idx, session in enumerate(sessions):
if session + task <= session_time:
sessions[session_idx] += task
added = True
break
if not added:
sessions.append(task)
return len(sessions)
The algorithm starts by sorting the tasks array in descending order. Then, for each task in tasks, it iterates over the sessions array and tries to insert the task into the first session that has sufficient remaining time. If no session can accommodate the task, a new session is created.
In the end, the size of the sessions array is the answer, which represents the minimum number of work sessions required to finish all tasks.
#include <vector>
#include <algorithm>
int minSessions(std::vector<int>& tasks, int sessionTime) {
std::sort(tasks.begin(), tasks.end(), std::greater<int>());
std::vector<int> sessions;
for (int task : tasks) {
bool added = false;
for (int& session : sessions) {
if (session + task <= sessionTime) {
session += task;
added = true;
break;
}
}
if (!added) {
sessions.push_back(task);
}
}
return sessions.size();
}
The algorithm starts by sorting the tasks array in descending order. Then, for each task in tasks, it iterates over the sessions array and tries to insert the task into the first session that has sufficient remaining time. If no session can accommodate the task, a new session is created.
In the end, the size of the sessions array is the answer, which represents the minimum number of work sessions required to finish all tasks.