Given a binary string s
βββββwithout leading zeros, return true
βββ if s
contains at most one contiguous segment of ones. Otherwise, return false
.
Example 1:
Input: s = "1001 " Output: false Explanation: The ones do not form a contiguous segment.
Example 2:
Input: s = "110 " Output: true
Constraints:
1 <= s.length <= 100
s[i]
ββββ is either '0'
or '1'
.s[0]
is '1'
.program main
implicit none
character(len=100) :: s
logical :: result
! Example 1
s = "1001"
result = contains_contiguous_segment_of_ones(s)
if (result) then
write (*,*) "FAILURE: Expected false but got true for input ", s
else
write (*,*) "SUCCESS: Expected false and got false for input ", s
end if
! Example 2
s = "110"
result = contains_contiguous_segment_of_ones(s)
if (.not. result) then
write (*,*) "FAILURE: Expected true but got false for input ", s
else
write (*,*) "SUCCESS: Expected true and got true for input ", s
end if
contains
function contains_contiguous_segment_of_ones(s) result(res)
implicit none
character(len=*), intent(in) :: s
logical :: res
integer :: i, j
res = .false.
do i = 2, len(s) - 1
if (s(i:i) == '1') then
do j = i + 1, len(s)
if (s(j:j) /= '1') exit
end do
if (j - i > 1) then
res = .true.
exit
end if
end if
end do
end function contains_contiguous_segment_of_ones
end program main
SUCCESS: Expected false and got false for input 1001 FAILURE: Expected true but got false for input 110
module one_segment
implicit none
private
public :: one_segment
contains
function one_segment(s) result(output)
implicit none
character(len=*), intent(in) :: s
logical :: output
integer :: i, j
output = .false.
do i = 2, len(s)
if (s(i:i) == '1') then
j = i
do while (j <= len(s) .and. s(j:j) == '1')
j = j + 1
end do
if (j - i > 1) then
output = .true.
exit
end if
end if
end do
end function one_segment
end module one_segment
program test_one_segment
use one_segment
implicit none
character(len=100) :: s
logical :: output
s = "1001 "
output = one_segment(s)
if (output) then
print *, "The ones form a contiguous segment."
else
print *, "The ones do not form a contiguous segment."
end if
s = "110 "
output = one_segment(s)
if (output) then
print *, "The ones form a contiguous segment."
else
print *, "The ones do not form a contiguous segment."
end if
end program test_one_segment
temp.f95:4:25: 4 | public :: one_segment | 1 Error: PUBLIC attribute applied to MODULE one_segment at (1) temp.f95:6:24: 6 | function one_segment(s) result(output) | 1 Error: MODULE attribute of βone_segmentβ conflicts with PROCEDURE attribute at (1) temp.f95:7:21: 7 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:8:41: 8 | character(len=*), intent(in) :: s | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:25: 9 | logical :: output | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:23: 10 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:24: 11 | output = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:12:24: 12 | do i = 2, len(s) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:13:20: 13 | if (s(i:i) == '1') then | 1 Error: Syntax error in argument list at (1) temp.f95:14:21: 14 | j = i | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:48: 15 | do while (j <= len(s) .and. s(j:j) == '1') | 1 Error: Syntax error in argument list at (1) temp.f95:16:29: 16 | j = j + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:19: 17 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:18:35: 18 | if (j - i > 1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:19:35: 19 | output = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:24: 20 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:21:19: 21 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:15: 22 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:11: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:7: 24 | end function one_segment | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:9: 28 | use one_segment | 1 Fatal Error: Cannot open module file βone_segment.modβ for reading at (1): No such file or directory compilation terminated.
def minimum_energy(tasks):
tasks.sort(key=lambda task: task[1] - task[0], reverse=True)
energy = 0
for task in tasks:
energy = max(energy + task[0], task[1])
return energy
(minimum[i] - actual[i])
.#include <vector>
#include <algorithm>
int minimumEnergy(std::vector<std::vector<int>>& tasks) {
std::sort(tasks.begin(), tasks.end(), [](const std::vector<int>& a, const std::vector<int>& b) {
return a[1] - a[0] > b[1] - b[0];
});
int energy = 0;
for (const auto& task : tasks) {
energy = std::max(energy + task[0], task[1]);
}
return energy;
}
(minimum[i] - actual[i])
.