-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2712-MinimumCostToMakeAllCharactersEqual.go
More file actions
102 lines (89 loc) · 3.55 KB
/
2712-MinimumCostToMakeAllCharactersEqual.go
File metadata and controls
102 lines (89 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
// 2712. Minimum Cost to Make All Characters Equal
// You are given a 0-indexed binary string s of length n on which you can apply two types of operations:
// 1. Choose an index i and invert all characters from index 0 to index i (both inclusive), with a cost of i + 1
// 2. Choose an index i and invert all characters from index i to index n - 1 (both inclusive), with a cost of n - i
// Return the minimum cost to make all characters of the string equal.
// Invert a character means if its value is '0' it becomes '1' and vice-versa.
// Example 1:
// Input: s = "0011"
// Output: 2
// Explanation: Apply the second operation with i = 2 to obtain s = "0000" for a cost of 2. It can be shown that 2 is the minimum cost to make all characters equal.
// Example 2:
// Input: s = "010101"
// Output: 9
// Explanation: Apply the first operation with i = 2 to obtain s = "101101" for a cost of 3.
// Apply the first operation with i = 1 to obtain s = "011101" for a cost of 2.
// Apply the first operation with i = 0 to obtain s = "111101" for a cost of 1.
// Apply the second operation with i = 4 to obtain s = "111110" for a cost of 2.
// Apply the second operation with i = 5 to obtain s = "111111" for a cost of 1.
// The total cost to make all characters equal is 9. It can be shown that 9 is the minimum cost to make all characters equal.
// Constraints:
// 1 <= s.length == n <= 10^5
// s[i] is either '0' or '1'
import "fmt"
func minimumCost(s string) int64 {
res, n, l, r := 0, len(s), byte(s[0] - '0'), byte(0)
for i := 1; i < n; i++ {
b := ((s[i]-'0')^r)
if b == l { continue }
if i < n - i { // left toggle
res += i
l ^= 1
} else { // right toggle
res += (n - i)
r ^= 1
}
}
return int64(res)
}
func minimumCost1(s string) int64 {
res, n := 0, len(s)
for i := 0; i < n - 1; i++ {
if s[i] != s[i + 1] {
if i + 1 < n - i - 1 {
res += (i + 1)
} else {
res += (n - i - 1)
}
}
}
return int64(res)
}
func minimumCost2(s string) int64 {
n := len(s)
if n == 1 { return 0 }
res, mid := 0, n / 2
for i := mid - 1; i >= 0; i-- {
if s[i] != s[i + 1] {
res += (i + 1)
}
}
for i := mid + 1; i < n; i++ {
if s[i] != s[i - 1] {
res += (n - i)
}
}
return int64(res)
}
func main() {
// Example 1:
// Input: s = "0011"
// Output: 2
// Explanation: Apply the second operation with i = 2 to obtain s = "0000" for a cost of 2. It can be shown that 2 is the minimum cost to make all characters equal.
fmt.Println(minimumCost("0011")) // 2
// Example 2:
// Input: s = "010101"
// Output: 9
// Explanation: Apply the first operation with i = 2 to obtain s = "101101" for a cost of 3.
// Apply the first operation with i = 1 to obtain s = "011101" for a cost of 2.
// Apply the first operation with i = 0 to obtain s = "111101" for a cost of 1.
// Apply the second operation with i = 4 to obtain s = "111110" for a cost of 2.
// Apply the second operation with i = 5 to obtain s = "111111" for a cost of 1.
// The total cost to make all characters equal is 9. It can be shown that 9 is the minimum cost to make all characters equal.
fmt.Println(minimumCost("010101")) // 9
fmt.Println(minimumCost1("0011")) // 2
fmt.Println(minimumCost1("010101")) // 9
fmt.Println(minimumCost2("0011")) // 2
fmt.Println(minimumCost2("010101")) // 9
}