2 回答

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);
}

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個贊
你所要求的是不可能的。
輸入數(shù)組可能都是正值,這意味著您至少必須讀取所有元素并對它們求和。那是 O(n)。
即使并非所有元素都是正數(shù),除非定義不超過 O(log n) 個元素是正數(shù),否則仍會得出相同的結(jié)論。
添加回答
舉報