簡體   English   中英

3個嵌套循環的時間復雜度計算

[英]Time complexity calculation for 3 nested loops

我正在努力提高我的算法技能。 我有一個非常簡單的代碼。

Qs:找出所有等於 0 的三元組(非重復)。

我認為無論嵌套循環(n ^ 3)如何,時間復雜度都是O(nlogn)。 我的理由是:可以這么說

nums length = 3。然后代碼運行 1 次。 {-1,0,-1} nums length = 3。然后代碼運行 1 次。 {-1,0,1,2}然后代碼運行 3 次。 -1,1,2 , -1,0,1 , 01,0,2

同樣,當長度為5時,代碼運行 6 次[] [] [] [] [] [] ,長度為 7 時運行 9 次。

所以看起來被考慮的三胞胎的數量增加了3(n-2) ,其中3<=n 因此,時間復雜度為n因為3n-6 ~ n

但是因為我有Arrays.sort時間復雜度變成O(nlogn)

我在看什么?

int[] nums = { -1, 0, 1, 2, -1, -4};
List<List<Integer>> test = new ArrayList<List<Integer>>();
nums = new int[] { -1, 0, 1};
Arrays.sort(nums);
HashSet<String> duplicates = new HashSet<String> ();

for (int i = 0 ; i < nums.length - 2 ; i++) { //i->0 - 3
    for (int j = i + 1; j < nums.length - 1; j++) { // j -> 1-4
        for (int k = j + 1; k < nums.length; k++) { //k ->2-5

            String sInt = nums[i] + "" + nums[j] + "" + nums[k];

            if ((nums[i] + nums[j] + nums[k]) == 0 && !duplicates.contains(sInt)) {
                ArrayList<Integer> t = new ArrayList<Integer> ();
                t.add(nums[i]);
                t.add(nums[j]);
                t.add(nums[k]);
                test.add(t);
            }

            duplicates.add(sInt);
        }
    }
}

return test;

n*(n-1)(n-2)/6三元組,代碼檢查一個。 時間復雜度為O(n^3) 我看不出Arrays.sort()在這里有什么意義。

看來你正在解決LeetCode (15)的 3Sum 問題

您關於 N * Log N 排序的邏輯是正確的。 但是,您的循環在 N ^ 3 處運行,正如本答案中所解釋的那樣

最優解(Order of N ^ 2)是:

Java

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new LinkedList<>();

        for (int i = 0; i < nums.length - 2; i++) {
            if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
                int lo = i + 1, hi = nums.length - 1, sum = 0 - nums[i];
                while (lo < hi) {
                    if (nums[lo] + nums[hi] == sum) {
                        res.add(Arrays.asList(nums[i], nums[lo], nums[hi]));
                        while (lo < hi && nums[lo] == nums[lo + 1])
                            lo++;
                        while (lo < hi && nums[hi] == nums[hi - 1])
                            hi--;
                        lo++;
                        hi--;
                    } else if (nums[lo] + nums[hi] < sum) {
                        lo++;
                    } else {
                        hi--;
                    }
                }
            }
        }
        return res;
    }
}

Python

class Solution:
    def threeSum(self, nums):
        res = []
        nums.sort()
        for i in range(len(nums) - 2):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            lo, hi = i + 1, len(nums) - 1
            while lo < hi:
                tsum = nums[i] + nums[lo] + nums[hi]
                if tsum < 0:
                    lo += 1
                if tsum > 0:
                    hi -= 1
                if tsum == 0:
                    res.append((nums[i], nums[lo], nums[hi]))
                    while lo < hi and nums[lo] == nums[lo + 1]:
                        lo += 1
                    while lo < hi and nums[hi] == nums[hi - 1]:
                        hi -= 1
                    lo += 1
                    hi -= 1
        return res

參考

您通常可以在此鏈接中找到最有效的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM