最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - time complexity of returning power set (leetcode 78 subsets) - Stack Overflow

programmeradmin1浏览0评论

Why the time complexity of generating power set of given array is O(n * 2^n). The solution which I created or even the solution which is shared on leetcode runs 2^n times. 1 loop to generate 1 subset. I tested the run count as well and it is always meeting 2^n. The solution is given below and the leetcode also mentions the time complexity of their solution as O(n * 2^n). Cant figure out how it is possible.

class Solution {

    private List<List<Integer>> output = new ArrayList();
    private int n;
    private int runStatus=0;

    public void backtrack(int first, ArrayList<Integer> curr, int[] nums) {
        // Add the current subset to the output
        output.add(new ArrayList(curr));
        // Generate subsets starting from the current index
        for (int i = first; i < n; ++i) {
            curr.add(nums[i]);
            System.out.println("runstatus is : "+(runStatus++));
            backtrack(i + 1, curr, nums);
            curr.remove(curr.size() - 1);
        }
    }

    public List<List<Integer>> subsets(int[] nums) {
        n = nums.length;
        ArrayList<Integer> currCombo = new ArrayList<Integer>();
        backtrack(0, currCombo, nums); // One call generates all subsets
        return output;
    }
}

Now, if you track how many times the "System.out.println("runstatus is : "+(runStatus++));" has run, it will always be O(2^n). Please throw some light on this, what I am interpreting incorrectly?

Why the time complexity of generating power set of given array is O(n * 2^n). The solution which I created or even the solution which is shared on leetcode runs 2^n times. 1 loop to generate 1 subset. I tested the run count as well and it is always meeting 2^n. The solution is given below and the leetcode also mentions the time complexity of their solution as O(n * 2^n). Cant figure out how it is possible.

class Solution {

    private List<List<Integer>> output = new ArrayList();
    private int n;
    private int runStatus=0;

    public void backtrack(int first, ArrayList<Integer> curr, int[] nums) {
        // Add the current subset to the output
        output.add(new ArrayList(curr));
        // Generate subsets starting from the current index
        for (int i = first; i < n; ++i) {
            curr.add(nums[i]);
            System.out.println("runstatus is : "+(runStatus++));
            backtrack(i + 1, curr, nums);
            curr.remove(curr.size() - 1);
        }
    }

    public List<List<Integer>> subsets(int[] nums) {
        n = nums.length;
        ArrayList<Integer> currCombo = new ArrayList<Integer>();
        backtrack(0, currCombo, nums); // One call generates all subsets
        return output;
    }
}

Now, if you track how many times the "System.out.println("runstatus is : "+(runStatus++));" has run, it will always be O(2^n). Please throw some light on this, what I am interpreting incorrectly?

Share Improve this question edited Jan 19 at 16:03 dariosicily 4,5272 gold badges15 silver badges18 bronze badges asked Jan 19 at 8:25 OnkiOnki 2,0957 gold badges40 silver badges68 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The factor

发布评论

评论列表(0)

  1. 暂无评论