Found 10784 Articles for Python

Decrease Elements To Make Array Zigzag in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:43:20

309 Views

Suppose we have an array nums of integers, a move operation is actually choosing any element and decreasing it by 1. An array A is a zigzag array if either 1 or 2 is satisfied −Every even-indexed element is greater than adjacent elements, So. A[0] > A[1] < A[2] > A[3] < A[4] > ... and so on.Every odd-indexed element is greater than adjacent elements, So. A[0] < A[1] > A[2] < A[3] > A[4] < ... and so on.We have to find the minimum number of moves to transform the given array nums into a zigzag array.So if the ... Read More

Connecting Cities With Minimum Cost in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:17:17

309 Views

Suppose there are N cities numbered from 1 to N. We have connections, where each connection [i] is [city1, city2, cost] this represents the cost to connect city1 and city2 together. We have to find the minimum cost so that for every pair of cities, there exists a path of connections (possibly of length 1) that connects those two cities together. The cost is the sum of the connection costs used. If the task is impossible, return -1.So if the graph is like −Then the output will be 6, Choosing any two cities will connect all cities, so we choose ... Read More

Shortest Path with Alternating Colors in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:16:31

556 Views

Suppose we have directed graph, with nodes labelled 0, 1, ..., n-1. In this graph, each edge is colored with either red or blue colors, and there could be self-edges or parallel edges. Each [i, j] in red_edges indicates a red directed edge from node i to node j. Similarly, each [i, j] in blue_edges indicates a blue directed edge from node i to node j. We have to find an array answer of length n, where each answer[X] is the length of the shortest path from node 0 to node X such that the edge colors alternate along the ... Read More

Lowest Common Ancestor of Deepest Leaves in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:15:36

107 Views

Suppose we have a rooted binary tree, we have to return the lowest common ancestor of its deepest leaves. We have to keep in mind that −The node of a binary tree is a leaf node if and only if it has no childrenThe depth of the root of the tree is 0, and when the depth of a node is d, the depth of each of its children is d+1.The lowest common ancestor of a set S of nodes in the node A with the largest depth such that every node in S is in the subtree with root ... Read More

Maximum Average Subtree in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:14:40

130 Views

Suppose we have the root of a binary tree; we have to find the maximum average value of any subtree of that tree. So if the tree is like −The output will be 6, this is because, for node 5, it will be (5 + 6 + 1)/ 3 = 4, then for node 6 it will be 6 / 1 = 6, and for node 1, it will be 1 / 1 = 1, so the max is 6.To solve this, we will follow these steps −res := 0Define a method called solve(), this will take rootif root is ... Read More

Path In Zigzag Labelled Binary Tree in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:00:19

132 Views

Suppose in an infinite binary tree where every node has two children, the nodes are labelled in row order. Now in the odd numbered rows (the first, third, fifth, ...), the labelling is left to right, and in the even numbered rows (second, fourth, sixth, ...), the labelling is right to left. So the tree will be like −So we have given the label of a node in this tree, we have to find the labels in the path from the root of the tree to the node with that label. So if the input is label = 14, then ... Read More

The Earliest Moment When Everyone Become Friends in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:56:38

85 Views

Suppose in a social group, there are N different people, with unique integer ids from 0 to N-1. Here we have a list of logs, where each logs[i] = [time, id_A, id_B] contains a non-negative integer timestamp, and the ids of two different people. Each log is showing the time in which two different people became friends. If A is friends with B, then B is friends with A. Let's say that person A is acquainted with person B if A is friends with B, or A is a friend of someone acquainted with B. We have to find the ... Read More

Broken Calculator in Python

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

238 Views

Suppose we have a broken calculator that has a number showing on its display, we can perform only two operations −Double − This will multiply the number on the display by 2, or;Decrement − This will decrease the number that is showing, by 1, Initially, the calculator is displaying the number X. We have to find the minimum number of operations needed to display the number Y.So if the input is like X = 5 and Y is 8, then the output will be 2, as decrement, it once, then double itTo solve this, we will follow these steps −res ... Read More

Insert Delete GetRandom O(1) in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:34:50

446 Views

Suppose we have a data structure that supports all following operations in average O(1) time.insert(val) − This will Insert an item val to the set if that is not already present.remove(val) − This will remove an item val from the set if it presents.getRandom − This will return a random element from current set of elements. Each element must have the same probability of being returned.To solve this, we will follow these steps −For the initialization, define a parent map, and elements arrayFor the insert() function, it will take val as inputif val is not present in parent map, or ... Read More

Flatten Nested List Iterator in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:33:50

357 Views

Suppose we have a nested list of integers; we have to implement an iterator to flatten it. Each element is either an integer, or a list. The elements of that list may also be integers or other lists. So if the input is like [[1, 1], 2, [1, 1]], then the output will be [1, 1, 2, 1, 1]To solve this, we will follow these steps −In the initializing section, it will take the nested list, this will work as follows −set res as empty list, index := 0, call getVal(nestedList)The getVal() will take nestedIntegers, this will work as −for ... Read More

Advertisements