繁体   English   中英

任务错误答案 - 算法错误

[英]Wrong answer in task - mistake in algorithm

我在做作业(首先对英语感到抱歉)。

考虑排成一行的 N 个硬币。 每枚硬币都显示正面或反面。 这些硬币的邻接度是相邻硬币显示相同面的对数。返回通过反转一枚硬币可以获得的最大可能邻接度,其中一枚硬币必须反转

例如我有

1 1 0 1 0 0 

在改变third之后我们得到1 1 1 1 0 0所以我们有 4 对。

但是我的代码不起作用,例如

1 1 

我应该得到0但得到1

  public static void main(String[] args) {
    int[] A = {1, 1};

    int n = A.length;
    int result = 0;
    for (int i = 0; i < n - 1; i++) {
        if (A[i] == A[i + 1])
            result = result + 1;
    }
    int r = 0;
    for (int i = 0; i < n; i++) {
        int count = 0;
        if (i > 0) {
            if (A[i - 1] != A[i])
                count = count + 1;
            else
                count = count - 1;
        }
        if (i < n - 1) {
            if (A[i + 1] != A[i])
                count = count + 1;
            else
                count = count - 1;
        }
        r = Math.max(r, count);
    }
    System.out.println(result + r);
}

我在哪里弄错了?

请注意“其中一枚硬币必须反转”

这意味着您必须翻转并且只有一枚硬币!!!

我们来分析一下:

  1. 测试用例 A = {1,1} 或 A ={0,0},
    那么结果=1,所以在“反转一枚硬币”后,结果将变为0。

  2. 测试用例 A = {1,1,1} 或 A ={0,0,0}(所有硬币都朝上或朝下),
    那么结果将是 2,因此在“反转一枚硬币”后,结果应更改为 1(取最大值)。

  3. 等等..

因此if (result == n-1) return result-1; 必须放在你的程序中。 没有这样的声明,你的程序就有缺陷。

public static int solution(int[] A) 
{
            int n = A.length;
            int result = 0; 

            for (int i = 0; i < n - 1; ) 
            {
                if (A[i] == A[i + 1]) 
                    result = result + 1;
                i = i+1;
            }      
            if (result == n-1)   return result-1;   // to cover {1,1}, {1,1,1}

            int max = 0;
            for (int i = 0; i <n; i++) 
            {
                int count = 0;
                if (i > 0)   // starting up from 1 and  covering the last
                {
                    if (A[i-1] != A[i])
                        count = count + 1;    
                    else
                       count = count - 1;
                }
                if (i < n - 1) 
                {
                    if (A[i] != A[i + 1])   // starting with 0
                        count = count + 1;
                    else
                        count = count - 1;
                }               
                max = Math.max(max,count);              
            }     
            return result + max; // 
        }

我同意这里的一个答案,即您必须注意规则: 必须反转一枚硬币 ,这意味着您必须翻转一枚硬币。

而且也有一个规则说你不能添加或删除的行代码,而只能修改最多三行代码,对不对?

问题是当硬币最初都在同一面上时。 假设我们有这个测试用例:

0 0 0 0 0 0

初始状态是5对,但在正好翻转一枚硬币后,可能的最大对数应该是4 (而原始代码返回5 )。

因此,我对这个问题的决定是将变量max的初始值更改为 -1。

这是生成的代码,我们只需要修改一行代码:

public static void main(String[] args) {
    int[] A = {1, 1};

    int n = A.length;
    int result = 0;
    for (int i = 0; i < n - 1; i++) {
        if (A[i] == A[i + 1])
            result = result + 1;
    }
    int r = -1;
    for (int i = 0; i < n; i++) {
        int count = 0;
        if (i > 0) {
            if (A[i - 1] != A[i])
                count = count + 1;
            else
                count = count - 1;
        }
        if (i < n - 1) {
            if (A[i + 1] != A[i])
                count = count + 1;
            else
                count = count - 1;
        }
        r = Math.max(r, count);
    }
    System.out.println(result + r);
}

您可以通过拆分工作来实现这一点:

  • 遍历数组,一一换一枚硬币
  • 对于每次更改,计算您可以制作多少对(方法nbPair

public static void main(String[] args) {
    int[] A = {1, 1, 0, 1, 0, 0};

    int nbPairMax = 0;
    for (int i = 0; i < A.length; i++) {
        int[] copy = Arrays.copyOf(A, A.length);
        copy[i] = (copy[i] + 1) % 2; // 0->1, 1->0
        nbPairMax = Math.max(nbPairMax, nbPair(copy));
    }
    System.out.println(nbPairMax);

}

private static int nbPair(int[] array) {
    int result = 0;
    for (int i = 0; i < array.length - 1; i++) {
        if (array[i] == array[i + 1]) {
            result = result + 1;
        }
    }
    return result;
}

例如,对于{1, 1, 0, 1, 0, 0} ,循环将使用 6 种不同的可能更改调用方法nbPair() ,并计算您可以制作的对数:

[0, 1, 0, 1, 0, 0] >> 1
[1, 0, 0, 1, 0, 0] >> 2
[1, 1, 1, 1, 0, 0] >> 4
[1, 1, 0, 0, 0, 0] >> 4
[1, 1, 0, 1, 1, 0] >> 2
[1, 1, 0, 1, 0, 1] >> 1
public static int coinReversal(int a[]){
        int cost1 = 1, cost2 = 0;
        int b[] = a.clone();
        b[0] = 0;
        if(b[0] == 0){
            for(int i = 0 ; i < b.length-1 ; i++ ){
                if(b[i] == b[i+1] ){
                    cost1+=1;
                    if(b[i+1] == 1){
                        b[i+1] = 0;
                    }else{
                        b[i+1] = 1;
                    }
                }
            }
        }
        if(a[0] == 1){
            for(int i = 0 ; i < a.length-1 ; i++ ){
                if(a[i] == a[i+1] ){
                    cost2+=1;
                    if(a[i+1] == 1){
                        a[i+1] = 0;
                    }else{
                        a[i+1] = 1;
                    }
                }
            }
        }

        return  cost2 > cost1 ? cost1 : cost2;
    }

我发现了两个错误; 如果数组中的所有元素都相同,则第一个相关。

[1,1,1,1,1]

如果我们的数组只有一个元素,则为第二个。

[1]

代码:

public static int solution(int[] A) 
{
            int n = A.length;
            int result = 0; 

            for (int i = 0; i < n - 1; ) 
            {
                if (A[i] == A[i + 1]) 
                    result = result + 1;
                i = i+1;
            }
            // Add these 2 lines below

            if (A.length == 1 )  return 0; 
            if (result == n-1)   return result-1;

            int max = 0;
            for (int i = 0; i <n; i++) 
            {
                int count = 0;
                if (i > 0)   
                {
                    if (A[i-1] != A[i])
                        count = count + 1;    
                    else
                       count = count - 1;
                }
                if (i < n - 1) 
                {
                    if (A[i] != A[i + 1])   // starting with 0
                        count = count + 1;
                    else
                        count = count - 1;
                }               
                max = Math.max(max,count);              
            }     
            return result + max; // 
        }

在这个解决方案中,我以线性方式做到了

public static void main(String[] args) {
    int[] A = { 1, 1, 0, 1, 0, 0};

    int sum1 = 0, sum2 = 0;
    for (int i = 0; i < A.length; i++) {
        if (i % 2 === 0) {
            if (0 !== A[i]) {
                sum1++;
            }
            if (1 !== A[i]) {
                sum2++;
            }
        } else {
            if (1 !== A[i]) {
                sum1++;
            }
            if (0 !== A[i]) {
                sum2++;
            }
        }
    }
    if (sum1 > sum2) {
        System.out.println(sum2);
    }
    else {
        System.out.println(sum1);
    }
}

我在 python 中解决了这个硬币反转问题以获得替代的正面和反面。这是正确通过测试用例的问题的解决方案。

def coins_reversal(A):
"""
Reverse the coins to get alternative heads and tails.

:param list A: list of heads and tails of coins
:return: number of coins to reverse to get alternative heads and tails
"""
   result = 0
   NEW_A = A.copy()
   for i in range(0, len(A) - 1):
       if NEW_A[i] == 0 and NEW_A[i+1] == 0:
           result += 1
           if NEW_A[i - 1] == 0:
               NEW_A[i] = 1
           else:
               NEW_A[i + 1] = 1
       elif NEW_A[i] == 1 and NEW_A[i+1] == 1:
           result += 1
           if NEW_A[i - 1] == 1:
               NEW_A[i] = 0
           else:
               NEW_A[i + 1] = 0
   return result

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM