簡體   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