Created
October 27, 2021 17:56
-
-
Save R11baka/9fd1357ca00dfb2dee7b59da2d613144 to your computer and use it in GitHub Desktop.
Цепочка единиц
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func maxOnesAfterRemoveItem(list []byte) uint { | |
oneLength := uint(0) | |
maxLen := uint(0) | |
skipFirst0 := true | |
skippedPrevZeroIndex := 0 | |
for i := 0; i < len(list); i++ { | |
currentValue := list[i] | |
if currentValue == 1 { | |
oneLength = oneLength + 1 | |
if oneLength > maxLen { | |
maxLen = oneLength | |
} | |
} else { | |
if currentValue == 0 && skipFirst0 { | |
skippedPrevZeroIndex = i | |
skipFirst0 = false | |
continue | |
} | |
if currentValue == 0 && !skipFirst0 { | |
i = skippedPrevZeroIndex | |
oneLength = 0 | |
skipFirst0 = true | |
} | |
} | |
} | |
if maxLen == uint(len(list)) { | |
return maxLen - 1 | |
} | |
return maxLen | |
} | |
func main() { | |
fmt.Println(maxOnesAfterRemoveItem([]byte{0, 0})) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
/* | |
assert(maxOnesAfterRemoveItem[0,1] == 1) | |
assert(maxOnesAfterRemoveItem[1,0] == 1) | |
assert(maxOnesAfterRemoveItem[1,1] == 1) | |
assert(maxOnesAfterRemoveItem[1, 1, 0, 1, 1] == 4) | |
assert(maxOnesAfterRemoveItem[1, 1, 0, 1, 1, 0, 1, 1, 1] == 5) | |
assert(maxOnesAfterRemoveItem[1, 1, 0, 1, 1, 0, 1, 1, 1, 0] == 5) | |
*/ | |
func TestSequenceLength(t *testing.T) { | |
areaTests := []struct { | |
seq []byte | |
want uint | |
}{ | |
{[]byte{0, 0}, 0}, | |
{[]byte{1, 0}, 1}, | |
{[]byte{0, 1}, 1}, | |
{[]byte{1, 1, 0, 1, 1}, 4}, | |
{[]byte{1, 1, 0, 1, 1, 0, 1, 1, 1}, 5}, | |
{[]byte{1, 1, 0, 1, 1, 0, 1, 1, 1, 0}, 5}, | |
{[]byte{1, 1}, 1}, | |
{[]byte{1}, 0}, | |
{[]byte{0}, 0}, | |
} | |
for _, tt := range areaTests { | |
got := maxOnesAfterRemoveItem(tt.seq) | |
if got != tt.want { | |
t.Errorf("got %d want %d", got, tt.want) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment