Found 10476 Articles for Python

Program to find minimum number of cells it will take to reach bottom right corner in Python

Arnab Chakraborty
Updated on 21-Oct-2020 12:04:30

223 Views

Suppose we have a 2D grid representing a maze where 0 is for empty space, and 1 is the wall. We will start at grid[0, 0], we have to find the minimum number of squares it would take to get to bottom right corner of the grid. If we cannot reach, return −1.So, if the input is like000100100then the output will be 5To solve this, we will follow these steps −R := row count of grid, C := column count of gridq := [0, 0, 1] when A[0, 0] is 1 otherwise a new listA[0, 0] := 1for each (r, ... Read More

Program to schedule tasks to take smallest amount of time in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:59:05

233 Views

Suppose we have a list of values called tasks where each different value represents a different task type, and we also have a non-negative integer k. Each task wants one minute to complete, but we must wait k minutes between doing two tasks of the same type. At any time, we can be doing a task or waiting. We have to find the smallest amount of time it takes to complete all the tasks.So, if the input is like nums = [2, 2, 2, 3, 3, 2], k = 1, then the output will be 7, as the optimal ordering ... Read More

Program to find number of given operations required to reach Target in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:57:08

392 Views

Suppose we have two values start and end, we have to find the minimum number of operations needed to convert start to end by using these operations −Decrement by 1Multiply by 2So, if the input is like start = 2, end = 7, then the output will be 3, as we can multiply 2 to get 4, then multiply 2 to get 8 and then subtract 1 to get 7.To solve this, we will follow these steps −ans := 0Do the following infinitely, doif end

Program to check whether given tree is symmetric tree or not in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:54:58

288 Views

Suppose we have one binary tree. We have to check whether the tree is symmetric tree or not. A tree will be said to be symmetric if it is same when we take the mirror image of it. From these two trees, the first one is symmetric, but second one is not.To solve this, we will follow these steps.We will call following steps recursively. The function will be solve(root, root)if the node1 and node2 are empty, then return trueif either node1 or node2 is empty, then return falsereturn true when node1.val = node2.val and solve(node1.left, node2.right) and solve(node1.right, node2.left)Let us ... Read More

Program to check whether two trees can be formed by swapping nodes or not in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:50:02

169 Views

Suppose we have two trees, we have to check whether we can transform first tree into second one by swapping any node's left and right subtrees any number of times.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −que1 := a queue with root0 initiallyque2 := a queue with root1 initiallywhile que1 and que2 are not empty, dotemp1 := a new list, temp2 := a new listvalues1 := a new list, values2 := a new listif que1 and que2 are containing different number of elements, thenreturn Falsefor i in range 0 ... Read More

Program to check whether each node value except leaves is sum of its children value or not in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:46:21

343 Views

Suppose we have a binary tree, we have to check whether for every node in the tree except leaves, its value is same as the sum of its left child's value and its right child's value or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs() . This will take rootif root is null, thenreturn Trueif left of root is null and right of root is null, thenreturn Trueleft := 0if left of root is not null, thenleft := value of left of rootright := 0if right ... Read More

Program to find three unique elements from list whose sum is closest to k Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:43:13

236 Views

Suppose we have a list of numbers called nums and another value k, we have to find three unique entries in nums (a, b, c) such that |a + b + c − k| is minimized and return the absolute difference.So, if the input is like nums = [2, 5, 25, 6] k = 14, then the output will be 1 as if we take [2, 5, 6] will get us closest to 14 and the absolute difference is |13 − 14| = 1.To solve this, we will follow these steps −sort the list numsans := 1^9for i in range ... Read More

Program to check number of triplets from an array whose sum is less than target or not Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:40:48

191 Views

Suppose we have a list of numbers called nums and another value target, we have to find the number of triples (i < j < k) that exist such that nums[i] + nums[j] + nums[k] < target.So, if the input is like nums = [−2, 6, 4, 3, 8], target = 12, then the output will be 5, as the triplets are: [−2, 6, 4], [−2, 6, 3], [−2, 4, 3], [−2, 4, 8], [−2, 3, 8]To solve this, we will follow these steps −sort the list numsans := 0n := size of numsfor i in range 0 to n−1, ... Read More

Program to check we can find three unique elements ose sum is same as k or not Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:38:38

104 Views

Suppose we have a list of numbers called nums and another value k, we have to check whether we can find three unique elements in the list whose sum is k.So, if the input is like nums = [11, 4, 6, 10, 5, 1] k = 20, then the output will be True, as we have numbers [4, 6, 10] whose sum is 20.To solve this, we will follow these steps −sort the list numsl := 0, r := size of nums − 1while l < r − 1, dot := k − nums[l] − nums[r]if nums[r − 1] < ... Read More

Program to check we can find four elements whose sum is same as k or not in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:31:22

123 Views

Suppose we have a list of numbers called nums and a value k, we have to check whether there are four unique elements in the list that add up to k.So, if the input is like nums = [11, 4, 6, 10, 5, 1] k = 25, then the output will be True, as we have [4, 6, 10, 5] whose sum is 25.To solve this, we will follow these steps −sort the list numsn := size of numsfor i in range 0 to n − 4, dofor j in range i + 1 to n − 3, dol := ... Read More

Advertisements