Check if Binary String Has at Most One Segment of Ones

🏠 ⬅️ ➑️

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'.

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
⚠️ Correct
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
❌ Compiled
❌ Executed
❌ Correct
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
🌐 Data from online sources
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
  1. Sort the tasks in descending order based on the difference between the minimum energy required to start the task and the actual energy cost to complete the task (minimum[i] - actual[i]).
  2. Initialize the current energy to 0.
  3. Iterate through the sorted list of tasks, for each task:
  4. Calculate the maximum of the current energy plus the actual energy cost to complete the task, or the minimum energy required to start the task, and set it as the current energy.
  5. Return the current energy after processing all tasks. This will be the minimum initial amount of energy required to finish all tasks.
🌐 Data from online sources
#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;
}
  1. Sort the tasks in descending order based on the difference between the minimum energy required to start the task and the actual energy cost to complete the task (minimum[i] - actual[i]).
  2. Initialize the current energy to 0.
  3. Iterate through the sorted list of tasks, for each task:
  4. Calculate the maximum of the current energy plus the actual energy cost to complete the task, or the minimum energy required to start the task, and set it as the current energy.
  5. Return the current energy after processing all tasks. This will be the minimum initial amount of energy required to finish all tasks.