【问题标题】:Minimum alternating binary series count (coin flip problem)最小交替二进制系列计数(硬币翻转问题)
【发布时间】:2021-08-09 05:41:34
【问题描述】:

连续给定 N 个硬币,我需要计算使系列完美交替所需的最小变化。例如,[1, 1, 0, 1, 1] 必须变为 [0, 1, 0, 1, 0] ,这只需 2 次更改。请注意 [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
}

但是,由于它从 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;

【问题讨论】:

  • 您可能想在尝试将其放入代码之前用简单的英语或伪代码解释您的算法。此外,实施中存在各种问题。例如,int[] sequence=num 不会为传入的数组创建副本。
  • 我认为那里的伪代码解释得很好,但我想它是通过编写它的同一个疯子大脑过滤的。复制数组有什么用?

标签: java binary flip alternating


【解决方案1】:

根本不需要修改原始数组。

IIUC 有两种选择:您需要将序列更改为 1010... 或 0101... 您应该计算两者需要多少更改并返回最小值?

因此,对于每个偶数索引,如果值不为 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);
}

【讨论】:

  • 我在玩弄使用模数来快速浏览所有内容,虽然我不知道如何从中获取最小值。
猜你喜欢
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多