C++ Articles - Page 427 of 719

Minimum Cost to Connect Sticks in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:27:42

404 Views

Suppose we have some sticks with positive integer lengths. We can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y. This will be performed until there is one stick remaining. We have to find the minimum cost of connecting all the given sticks into one stick in this way. So if the stack is [2, 4, 3], then the output will be 14.To solve this, we will follow these steps −Define a max heap priority queue pqinsert all elements of s into pqans := 0while pq has more than ... Read More

Maximum Level Sum of a Binary Tree in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:14:36

290 Views

Suppose we have the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. We have to return the smallest level X such that the sum of all the values of nodes at level X is maximal. So if the tree is like −Then the output will be 2, The level 1 sum = 1, level 2 sum is 7 + 0 = 7, level 2 sum is 7 + (-8) = -1, so max is of level 2, so output is 2.To solve this, we will follow ... Read More

Complete Binary Tree Inserter in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:50:52

1K+ Views

As we know a complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. We have to write a data structure CBTInserter that is initialized with a complete binary tree and it supports the following operations−CBTInserter(TreeNode root) this initializes the data structure on a given tree with head node root;CBTInserter.insert(int v) will used to insert a TreeNode into the tree with a value node.val = v so that the tree remains complete, and returns the value of the parent of the inserted TreeNode;CBTInserter.get_root() ... Read More

Sum of Subarray Minimums in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:35:55

277 Views

Suppose we have an array of integers A. We have to find the sum of min(B), where B ranges over every (contiguous) subarray of A. Since the answer may be very large, then return the answer in modulo 10^9 + 7. So if the input is like [3, 1, 2, 4], then the output will be 17, because the subarrays are [3], [1], [2], [4], [3, 1], [1, 2], [2, 4], [3, 1, 2], [1, 2, 4], [3, 1, 2, 4], so minimums are [3, 1, 2, 4, 1, 1, 2, 1, 1, 1], and the sum is 17.To solve ... Read More

All Possible Full Binary Trees in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:34:58

348 Views

Suppose a full binary tree is a binary tree where each node has exactly 0 or 2 children. So we have to find a list of all possible full binary trees with N nodes. Each node of each tree in the answer must have node.val = 0. The returned trees can be in any order. So if the input is 7, then the trees are −To solve this, we will follow these steps −Define a map m of integer type key and tree type value.define a method called allPossibleFBT(), this will take N as inputis N is 1, then create ... Read More

Smallest Subtree with all the Deepest Nodes in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:33:30

171 Views

Suppose we have a binary tree rooted at root, the depth of each node is the shortest distance to the root. Here a node is deepest if it has the largest depth possible among any node in the entire tree. The subtree of a node is that node, plus the set of all descendants of that node. We have to find the node with the largest depth such that it contains all the deepest nodes in its subtree. So if the tree is like −Then the deepest subtree will be −To solve this, we will follow these steps −Define a ... Read More

Task Scheduler n C++

Arnab Chakraborty
Updated on 30-Apr-2020 08:47:54

4K+ Views

Suppose we have a char array representing tasks CPU need to do. This contains uppercase letters A to Z where different letters represent different tasks. The tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one job or just be idle. However, there is a non-negative cooling interval called n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. We have to find the least number of intervals the CPU will take to finish ... Read More

Find All Anagrams in a String in C++

Arnab Chakraborty
Updated on 29-Apr-2020 13:07:56

2K+ Views

Suppose we have a string s and a non-empty string p, we have to find all the start indices of p's anagrams in s. The strings consist of lowercase letters only and the length of both strings s and p will not be larger than 20 and 100. So for example, if s: "cbaebabacd" p: "abc", then the output will be [0, 6], at index 0, it is “cba”, and another is “bac”, these are the anagrams of “abc”.To solve this, we will follow these steps −Define a map m, n := size of s, set left := 0, right ... Read More

House Robber III in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:32:55

449 Views

Suppose one thief has found himself a new place for his thievery again. There is only one entrance to this area, the entrance is called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief felt that "all houses in this place forms a binary tree". And It will automatically contact the police if two directly-linked houses were broken into on the same night. We have to find the maximum amount of money the thief can rob tonight without alerting the police. So if the tree is like −Then the ... Read More

Super Ugly Number in C++

Arnab Chakraborty
Updated on 29-Apr-2020 12:30:42

353 Views

We have to create one function to find the nth super ugly number. The super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. So if the n is 12 and primes are [2, 7, 13, 19], then the output will be 32, this is because [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of 12 super ugly numbers.To solve this, we will follow these steps −Create a data structure triplet, with num, prime and idxif n is 1, then return 1, create ... Read More

Advertisements