Java 最小交替二进制序列计数(投币问题)

Java 最小交替二进制序列计数(投币问题),java,binary,flip,alternating,Java,Binary,Flip,Alternating,假设一行有N个硬币,我需要计算使系列完全交替所需的最小变化。例如,[1,1,0,1,1]必须变成[0,1,0,1,0],这只需要两次更改。请注意,[1,0,1,0,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;/

假设一行有N个硬币,我需要计算使系列完全交替所需的最小变化。例如,[1,1,0,1,1]必须变成[0,1,0,1,0],这只需要两次更改。请注意,[1,0,1,0,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
}
public int解决方案(int[]num){
int[]sequence=num;//复制传入数组,以便更改值
int flips=0;//在此处存储值
boolean ChangeRequired=false;//如何知道是否必须发生翻转
对于(int i=1;i
但是,由于它从索引=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;
booleanChangeRequired=false//正如名字所说,如果必要,它会检查整数何时更改
int flips=0;
int[]序列=num//因此,如果需要,我可以复制和编辑传入阵列
for(int i=0;i
根本不需要修改原始数组

IIUC有两个选项:您需要将序列更改为1010。。。或0101。。。 您应该计算两个和return min都需要多少更改

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

为了改变阅读,你做了相反的事情

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);
}
public int解决方案(int[]num){
int changeswithreading0=0;
int changeswithreading1=0;
对于int(i=0;i
在尝试将算法转化为代码之前,您可能想用简单的英语或伪代码解释算法。此外,执行过程中存在各种问题。根据示例
int[]sequence=num
没有为传入数组创建副本。我认为那里的伪代码已经很好地解释了这一点,但我认为它是通过编写它的同一个疯子大脑过滤的。复制数组是什么意思?我一直在玩弄使用模来快速遍历所有内容,尽管我不知道如何从中获取最小值。