最小交替二进制系列计数硬币翻转问题

问题描述

给定连续 N 个硬币,我需要计算使系列完美交替所需的最小变化。例如,[1,1,1] 必须变成 [0,0] ,它只需要进行 2 次更改。请注意, [1,1] 是不正确的,因为它需要进行 3 次更改。我这里有一个主要是函数程序:

public int solution(int[] num) {
    int[] sequence = num;//Make a copy of the incoming array so I can change values
    int flips = 0;//Store values here
    boolean changeNeeded = false;//How to kNow if a flip must occur
    for (int i = 1; i < sequence.length; i++) {//Count entire array,starting with index 1 so the pprevIoUs entry can be checked for diplicity
        if (sequence[i] == sequence[i - 1]) {//checking prevIoUs entry
            flips++;//increment neccessary flip
            changeNeeded = true;//Make sure to change the value so it doesn't get incremented twice
        }
        if (sequence[i] == 1 && changeNeeded) {//Change a 1 to a 0
        sequence[i] = 0; 
        changeNeeded = false;
        } else if (sequence[i] == 0 && changeNeeded) {//change a 0 to a 1
        sequence[i] = 1;
        changeNeeded = false;
        }
    }
    return flips;//done
}

但是,因为它从 index = 1 开始计数,所以无法正确解决上述问题。我还没有找到一种方法来计算结束边界并保持在边界内。

解决方案: 我设法调整我的代码,直到它正常工作,尽管它是“我不知道为什么,但这是有效的”答案之一。我标记的答案要好得多。

boolean changeNeeded = false; //Just as the name says,it checks for when an integer change if it is necessary
    int flips = 0;
    int[] sequence = num; //So I can copy and edit the incoming array if needed
    for (int i = 0; i < num.length; i++) {
        sequence[i] = A[i];//copy all elements
    }
    for (int i = 0; i < sequence.length - 1; i++) { //Count the array,capping our count so we avoid indexOutOfBounds errors
        
        if (sequence[i] == sequence[i + 1]) //Compare current to next entry
        {
            flips++;//increment a fix
            changeNeeded = true;//tell the system that a digit needs changed
        }
        if (sequence[i] == 1 && changeNeeded) //change a 1 to a 0
        {
            sequence[i] = 0;
            changeNeeded = false; //reset our change detection
        }
        else if (sequence[i] == 0 && changeNeeded) //change a 0 to a 1
        {
            sequence[i] = 1;
            changeNeeded = false; //see above
        }
    }
    if (sequence[0] == sequence[1]) {//The above system skips properly adjusting the 0th index,so it needs to be checked after
        flips++;
        //If checked within the loop it will add an extra,unnecessary flip. I don't kNow why.
    }
    return flips;

解决方法

完全不需要修改原始数组。

IIUC 有两个选项:你需要把你的序列改为 1010... 或 0101... 您应该计算两者都需要多少更改并返回 min?

因此,对于每个偶数索引,如果值不为 0,则增加 changesWithLeading0。 对于每个奇数索引,如果值不是 1,则增加 changesWithLeading0。

对于 changesWithLeading1,你做相反的事情。

public int solution(int[] num) {
  int changesWithLeading0 = 0;
  int changesWithLeading1 = 0;
  for int(i = 0; i < sequence.length; i++) {
    if (sequence[i] == 1 - (i % 2)) {
      changesWithLeading0 ++;
    }
    if (sequence[i] == i % 2) {
      changesWithLeading1 ++;
    }
  }
  return Math.min(changesWithLeading0,changesWithLeading1);
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...