高级农民
- 积分
- 2513
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-1-21
- 最后登录
- 1970-1-1
|
- public int getMinErrors(String errorString, int x, int y) {
- int mod = (int) (1e9 + 7);
- int a1 = 0, b1 = 0; // a is 0 and b is 1
- int m1 = 0, n1 = 0; // m is number of 01 and n is number of 10
- int a2 = 0, b2 = 0; // a is 0 and b is 1
- int m2 = 0, n2 = 0; // m is number of 01 and n is number of 10
- for (char c : errorString.toCharArray()) {
- if (c == '0') {
- a1++;
- a2++;
- n1 += b1;//all previous 1 can combine with this 0
- n2 += b2;
- } else if (c == '1') {
- b1++;
- b2++;
- m1 += a1;//all previous 0 can combine with this 1
- m2 += a2;
- } else {
- a1++;//! is 0
- n1 += b1;
- b2++;//! is 1
- m2 += a2;
- }
- }
- return Math.min(module(m1, n1, x, y, mod), module(m2, n2, x, y, mod));
- }
- private int module(int a, int b, int x, int y, int mod) {
- return (int)((1L * a * x) % mod + (1L * b * y) % mod) % mod;
- }
复制代码 |
|