-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1758. Minimum Changes To Make Alternating Binary String
- Loading branch information
1 parent
a3fed90
commit 54bd23e
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string | ||
|
||
namespace LeetCode.Problems; | ||
|
||
public sealed class MinOperations : ProblemBase | ||
{ | ||
[Theory] | ||
[ClassData(typeof(MinOperations))] | ||
public override void Test(object[] data) => base.Test(data); | ||
|
||
protected override void AddTestCases() | ||
=> Add(it => it.Param("0100").Result(1)) | ||
.Add(it => it.Param(10).Result(0)) | ||
.Add(it => it.Param("1111").Result(2)) | ||
.Add(it => it.Param("00010111").Result(2)) | ||
.Add(it => it.Param("00001111").Result(4)); | ||
|
||
private int Solution(string s) | ||
{ | ||
var operations = 0; | ||
for (int i = 0; i < s.Length; i++) | ||
{ | ||
var mod = i % 2; | ||
if (mod == 0 && s[i] != '1' | ||
|| mod != 0 && s[i] == '1') | ||
{ | ||
operations++; | ||
} | ||
} | ||
|
||
return Math.Min(operations, s.Length - operations); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string | ||
|
||
package problems | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dobermanch/leetcode/core" | ||
) | ||
|
||
type MinOperations struct{} | ||
|
||
func TestMinOperations(t *testing.T) { | ||
gen := core.TestSuite[MinOperations]{} | ||
gen.Add(func(tc *core.TestCase) { | ||
tc.Param("0100").Result(1) | ||
}).Add(func(tc *core.TestCase) { | ||
tc.Param("10").Result(0) | ||
}).Add(func(tc *core.TestCase) { | ||
tc.Param("1111").Result(2) | ||
}).Run(t) | ||
} | ||
|
||
func (MinOperations) Solution(s string) int { | ||
operations1 := 0 | ||
for i := 0; i < len(s); i++ { | ||
if i%2 == 0 && s[i] != '1' || i%2 != 0 && s[i] == '1' { | ||
operations1++ | ||
} | ||
} | ||
|
||
operations2 := len(s) - operations1 | ||
if operations2 < operations1 { | ||
return operations2 | ||
} | ||
|
||
return operations1 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string | ||
|
||
from core.problem_base import * | ||
|
||
class MinOperations(ProblemBase): | ||
def Solution(self, s: str) -> int: | ||
operations = 0 | ||
for i in range(len(s)): | ||
if i % 2 == 0 and s[i] != '1' \ | ||
or i % 2 != 0 and s[i] == '1': | ||
operations += 1 | ||
|
||
return min(operations, len(s) - operations) | ||
|
||
if __name__ == '__main__': | ||
TestGen(MinOperations) \ | ||
.Add(lambda tc: tc.Param("1111").Result(2)) \ | ||
.Add(lambda tc: tc.Param("0100").Result(1)) \ | ||
.Add(lambda tc: tc.Param("10").Result(0)) \ | ||
.Run() |