Sort by

recency

|

27 Discussions

|

  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Cut Tree Problem Solution

  • + 0 comments

    Here is Cut Tree problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-cut-tree-problem-solution.html

  • + 0 comments

    This challenge can be solved in O(n*K*K) time complexity and linear space complexity in my solution, I can't optimize further. My solution to this challenge https://blog.csdn.net/JetHsu1/article/details/131125934

  • + 0 comments

    Here is my solution without any dynamic programming. Only single dfs suffice. I challenge hackerrank team to understand it 😜.

    def cutTree(n, k, edges):
        g = [[] for _ in range(n)]
        for i in edges:
            g[i[0]-1].append(i[1]-1)
            g[i[1]-1].append(i[0]-1)
        global ans
        ans = 1
        def multiply(x, y):
            ans = defaultdict(lambda: 0)
            for k, v in x.items():
                for k1, v1 in y.items(): ans[k+k1-1] += v*v1
            for k, v in ans.items():
                if k in x: x[k] += v
                else: x[k] = v
        def dfs(i,p):
            global ans
            if g[i] == [p]:
                ans += 1
                return {0:1}
            x = 1 if i else 0
            res = {len(g[i])-x : 1}
            for nxt in g[i]:
                if nxt != p: multiply(res, dfs(nxt, i))
            ans += sum(((v if i+x <= k else 0) for i, v in res.items()))
            return res
        dfs(0,-1)
        return ans
    
  • + 0 comments

    Whats wrong in this understadning of solution. https://github.com/PrakashJetty/Solutions/blob/main/Sol10.java

    ** The idea is to calculate for each node atleast n-k conncted subtrees ** Approach :

    1. for each branch calculate connected nodes as we move up the branch. accumulate nodes at each level so far computed.
    2. For first branch connected nodes as move up will be calculates subtrees
    3. Second branch onwards , repeat step 1 and caculate subtrees possible as mentioned above using combinations at each level of already visited or computed tree.
    4. 4.