第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用于查找循環(huán)排序數(shù)組中正數(shù)之和的分而治之算法

用于查找循環(huán)排序數(shù)組中正數(shù)之和的分而治之算法

婷婷同學(xué)_ 2023-09-27 15:00:04
我正在嘗試使用分而治之的 O(N) 解決方案來解決下一個問題:給定一個循環(huán)排序的數(shù)組,我需要它的正數(shù)之和。IE:If the array is: {-2, 0, 3, 4, 11, 13, -23, -15, -8}Then the algorithm should return 31.我認(rèn)為我已經(jīng)通過以下代碼接近它,但它奇怪地返回 -17 并且我無法找到問題調(diào)試:public class Main {private static final int[] TEST_VECTOR = new int[]{-2, 0, 3, 4, 11, 13, -23, -15, -8};public static int sumPositives2(int[] vector) {    return maximumSum(vector,0, vector.length - 1);}// Function to find Maximum subarray sum using// divide and conquerpublic static int maximumSum(int[] A, int left, int right){    // If array contains only one element    if (right == left) {        return A[left];    }    // Find middle element of the array    int mid = (left + right) / 2;    // Find maximum subarray sum for the left subarray    // including the middle element    int leftMax = Integer.MIN_VALUE;    int sum = 0;    for (int i = mid; i >= left; i--)    {        if(A[i] > 0) {            sum += A[i];        }    }    // Find maximum subarray sum for the right subarray    // excluding the middle element    int rightMax = Integer.MIN_VALUE;    sum = 0;    // reset sum to 0    for (int i = mid + 1; i <= right; i++)    {        if(A[i] > 0) {            sum += A[i];        }    }    // Recursively find the maximum subarray sum for left    // subarray and right subarray and tale maximum    int maxLeftRight = maximumSum(A, left, mid) +            maximumSum(A, mid + 1, right);    // return maximum of the three    return maxLeftRight + leftMax + rightMax;}public static void main(String[] args){    System.out.println("The Maximum sum of the subarray is " +            maximumSum(TEST_VECTOR, 0, TEST_VECTOR.length - 1));//Should be 31}}編輯:這個解決方案是 O(N) 嗎?感謝您的時間。
查看完整描述

2 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個贊

O(n)如果您遍歷該數(shù)組一次并僅對正數(shù)求和,您就可以輕松獲得解決方案。增強(qiáng)for循環(huán)似乎合適:


public static void main(String[] args) {

    int[] circularlySortedArray = {-2, 0, 3, 4, 11, 13, -23, -15, -8};


    // define a variable for the sum

    int sumOfPositives = 0;


    // go through all numbers in the array (n numbers)

    for (int number : circularlySortedArray) {

        // check if the number is positive

        if (number >= 0) {

            // and add it to the sum variable

            sumOfPositives += number;

        }

    }


    // then print the result

    System.out.println("The sum of positive numbers is " + sumOfPositives);

}

這種情況下的輸出是


The sum of positive numbers is 31

排序?qū)λ惴]有任何影響。


您的解決方案是,O(n)如果它有效,但您實(shí)際上不必在這里分而治之,因?yàn)闊o論如何它都不能完成O(n),它不是二分搜索。


編輯

由于上面的文本和代碼似乎不夠令人滿意,我重新思考了我在這里關(guān)于分而治之的陳述。該任務(wù)可能只需不到n幾步即可完成,但O(n)仍然是正確的。排序確實(shí)提供了不遍歷數(shù)組的所有元素的可能性,但這并不是在所有情況下都可以實(shí)現(xiàn)。


想象一下下面的數(shù)組,它們都是循環(huán)排序的,請看一下它們下面的模式,它們可能是這里分而治之解決方案和/或平均情況(Big Theta)解決方案的關(guān)鍵小于n,而最壞的情況(大 O)仍然會存在O(n)……


這些例子都有n = 7元素,每個例子都有p = 4正元素(包括0)和l = 3負(fù)元素。遍歷所有這些將始終是O(n),這將在O(6)這里。在某些情況下,排序提供了有關(guān)數(shù)組末尾內(nèi)容的信息,這使程序員能夠在某些情況下將最佳情況(Big Omega)減少到O(p + 1) = O(p) = O(4):


下面的數(shù)組需要 n 步,因?yàn)楸仨殭z查每個元素


{-3, -2, -1, 0, 1, 2, 3}

  n   n   n  p  p  p  p

下一個示例需要采取n - 1步驟,因?yàn)樵谒姓龜?shù)之后還有兩個負(fù)數(shù),您只需找到第一個即可滿足條件break。那是因?yàn)檩^低的指數(shù)已經(jīng)存在負(fù)數(shù)。


{-1, 0, 1, 2, 3, -2, -3}

  n  p  p  p  p   n   n

下一個僅需要p + 1(這意味著O(p + 1) = O(p))步,因?yàn)槟梢詁reak在找到的第一個負(fù)數(shù)處進(jìn)行循環(huán)。為什么?因?yàn)閿?shù)組從盡可能小的正數(shù)(根據(jù)定義)開始,找到的第一個負(fù)數(shù)表示不需要進(jìn)一步處理。


{0, 1, 2, 3, -1, -2, -3}

 p  p  p  p   n   n   n

最后一個示例n再次需要步驟,因?yàn)槟诓檎椅挥跀?shù)組開頭和結(jié)尾的正數(shù)。沒有機(jī)會直接知道它們處于哪個索引。


{3, -3, -2, -1, 0, 1, 2}

 p   n   n   n  p  p  p

(我知道)優(yōu)化平均情況的唯一方法是break根據(jù)可能的模式實(shí)現(xiàn)循環(huán)的條件。所以存儲索引并根據(jù)它們進(jìn)行檢查,但我認(rèn)為效果不會那么大。


這是我的第一個方法,可能會通過多種方式進(jìn)行優(yōu)化,我只嘗試過此編輯的示例:


public static void main(String[] args) {

    int[] circularlySortedArray = { 0, 1, 2, 3, -1, -2, -3 };


    // define a variable for the sum of positive values

    int sumOfPositives = 0;

    // define a variable for the lowest index of a positive number

    int firstPositiveIndex = -1;

    // define a variable for the lowest positive number found

    int smallesPositiveNumber = 0;


    // start iterating the array

    for (int i = 0; i < circularlySortedArray.length; i++) {

        System.out.println("Current index: " + i 

                + ", current value: " + circularlySortedArray[i]);

        // provide a variable for the current number to make this code a little more

        // readable

        int number = circularlySortedArray[i];


        // check if the current number is positive

        if (number >= 0) {

            // add it to the sum

            sumOfPositives += number;

            System.out.println("Added " + number 

                    + " to sumOfPositives (now: " + sumOfPositives + ")");

            // check if it is the first positive number found

            if (firstPositiveIndex < 0) {

                // if yes, set the variable value accordingly

                System.out.println("First and smallest positive number (" 

                                    + number 

                                    + ") found at index " 

                                    + i);

                firstPositiveIndex = i;

                smallesPositiveNumber = number;

            }

            System.out.println("————————————————————————————————");

        } else {

            // break conditions based on index & value of the smallest positive number found

            if (i > firstPositiveIndex && firstPositiveIndex > 0) {

                System.out.println("Stopped the loop at index " + i);

                break;

            } else if (smallesPositiveNumber == 0 && firstPositiveIndex == 0) {

                System.out.println("Stopped the loop at index " + i);

                break;

            }

            System.out.println(number + " is not positive, skip it");

            System.out.println("————————————————————————————————");

            continue;

        }

    }


    System.out.println("The sum of positive numbers is " + sumOfPositives);

}




查看完整回答
反對 回復(fù) 2023-09-27
?
手掌心

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個贊

你所要求的是不可能的。

輸入數(shù)組可能都是正值,這意味著您至少必須讀取所有元素并對它們求和。那是 O(n)。

即使并非所有元素都是正數(shù),除非定義不超過 O(log n) 個元素是正數(shù),否則仍會得出相同的結(jié)論。


查看完整回答
反對 回復(fù) 2023-09-27
  • 2 回答
  • 0 關(guān)注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號