Given a string s
of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).
The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring.
Example 1:
Input: s = "011101 " Output: 5 Explanation: All possible ways of splitting s into two non-empty substrings are: left = "0 " and right = "11101 ", score = 1 + 4 = 5 left = "01 " and right = "1101 ", score = 1 + 3 = 4 left = "011 " and right = "101 ", score = 1 + 2 = 3 left = "0111 " and right = "01 ", score = 1 + 1 = 2 left = "01110 " and right = "1 ", score = 2 + 1 = 3
Example 2:
Input: s = "00111 " Output: 5 Explanation: When left = "00 " and right = "111 ", we get the maximum score = 2 + 3 = 5
Example 3:
Input: s = "1111 " Output: 3
Constraints:
2 <= s.length <= 500
s
consists of characters '0'
and '1'
only.program main
implicit none
character(len=500) :: s
integer :: i, j, n
integer :: max_score
! read input
read(*,*) s
! find the maximum score
max_score = -1
do i = 1, len(s)-1
do j = i+1, len(s)
if (s(i:j) /= '0' .and. s(i:j) /= '1') cycle
n = count(s(i:j) == '0') + count(s(i:j) == '1')
if (n > max_score) max_score = n
end do
end do
! print output
write(*,*) max_score
end program main
temp.f95:16:45: 16 | n = count(s(i:j) == '0') + count(s(i:j) == '1') | 1 Error: βmaskβ argument of βcountβ intrinsic at (1) must be a logical array
! Declare variables
integer :: i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
character(len=500) :: s
integer :: left, right, score
! Read input
read(*,*) s
! Initialize variables
left = 0
right = 0
score = 0
! Loop through the string
do i = 1, len(s)
if (s(i:i) == '0') then
left = left + 1
else
right = right + 1
end if
end do
! Calculate the score
score = left + right
! Print the result
write(*,*) score
end
temp.f95:3:23: 3 | character(len=500) :: s | 1 Error: Symbol βsβ at (1) already has basic type of INTEGER temp.f95:16:10: 16 | if (s(i:i) == '0') then | 1 Error: Syntax error in IF-expression at (1) temp.f95:18:8: 18 | else | 1 Error: Unexpected ELSE statement at (1) temp.f95:20:7: 20 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:15:14: 15 | do i = 1, len(s) | 1 Error: βstringβ argument of βlenβ intrinsic at (1) must be CHARACTER
from collections import Counter
def is_possible_divide(nums: List[int], k: int) -> bool:
counts = Counter(nums)
for key in sorted(counts):
if counts[key] > 0:
for i in range(k-1, -1, -1):
if counts[key+i] < counts[key]:
return False
counts[key+i] -= counts[key]
return True
#include <map>
bool isPossibleDivide(std::vector<int>& nums, int k) {
std::map<int, int> counts;
for (int num : nums) counts[num]++;
for (auto it = counts.begin(); it != counts.end(); ++it) {
if (it->second > 0) {
for (int i = k - 1; i >= 0; --i) {
if ((counts[it->first + i] -= it->second) < 0) return false;
}
}
}
return true;
}