Find Minimum Total Distance Between House and Nearest Mailbox in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:50:51

1K+ Views

Suppose we have an array called houses and have another value k. Here houses[i] represents the location of the ith house along a street, we have to allocate k mailboxes in the street, and find the minimum total distance between each house and its nearest mailbox.So, if the input is like houses = [6, 7, 9, 16, 22] k = 2, then the output will be 9 because if we place mailbox at 7 and 18, then minimum total distance from each house is |6-7|+|7-7|+|9-7|+|16- 18|+|22-18| = 1+0+2+2+4 = 9.To solve this, we will follow these steps −sort the list ... Read More

Find Minimum Cost for Painting Houses in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:46:26

516 Views

Suppose there is an array of size m, represents m houses in a small city, each house must be painted with one of the n colors (the colors are labeled from 1 to n), And some houses are already painted, so no need to paint again. Those houses which are colored with same color are called neighborhood. We have the array houses, where houses[i] represents the color of the house, if the color value is 0, then it represents the house is not colored yet. We have another array called costs, this is a 2D array where costs[i, j] represents ... Read More

Find Where the Ball Lands in a Grid Box in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:43:34

211 Views

Suppose, we are given a m x n grid box, where each cell has a board that is positioned either from the top-right to bottom-left, or from the top-left to the bottom-right. Now from the top cells, a ball is put into the box and we have to check if that ball reaches the bottom of the box. The grid is given as a matrix. If a cell is marked 1 the diagonal board spans from the top-left to the bottom-right; if it is marked -1 it spans from the top-right to the bottom-left corner. If n balls are put ... Read More

Determine if Two Strings are Close in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:40:53

486 Views

Suppose we have two strings, s and t, we have to check whether s and t are close or not. We can say two strings are close if we can attain one from the other using the following operations −Exchange any two existing characters. (like abcde to aecdb)Change every occurrence of one existing character into another existing character, and do the same with the other characters also. (like aacabb -> bbcbaa (here all a's are converted to b's, and vice versa))We can use the operations on either string as many times as we want.So, if the input is like s ... Read More

Implement Queue with Front, Middle, and Back Operations in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:39:42

556 Views

Suppose, we are asked to implement a queue that can push and pop values at the front, middle, and back.We have to implement a pair of functions to push and pop for all three cases. We have to implement another function that shows the full queue at a given time.So, if the input is likepush_from_back(10)push_from_back(20)push_from_front(30)push_from_middle(40)push_from_front(50)show_queue()pop_from_back()show_queue()pop_from_front()show_queue()pop_from_middle()show_queue(), then the output will be [50, 30, 40, 10, 20[50, 30, 40, 10][30, 40, 10][30, 10]To solve this, we will follow these steps −array := array representation of the queueDefine a function push_from_front() . This will take valueinsert value into array at position 0Define a ... Read More

Find Vertical Area Between Two Points in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:37:22

180 Views

Suppose, we are given n number of points as (x, y). A vertical area is an area that is extended infinitely along the y-axis. We have to find out the vertical area between two points such that no other point is inside the area and is the widest.So, if the input is like pts = [[10, 9], [11, 11], [9, 6], [11, 9]], then the output will be 1.The areas in red and blue are optimal and there are no points inside them.To solve this, we will follow these steps −sort the list ptsfor i in range 1 to size ... Read More

Find Minimum Jumps to Reach Home in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:36:01

449 Views

Suppose there is an array called forbidden, where forbidden[i] indicates that the bug cannot jump to the position forbidden[i], and we also have three values a, b, and x. A bug's home is at position x on the number line. It is at position 0 initially. it can jump by following rules −Bug can jump exactly a positions to the right.Bug can jump exactly b positions to the left.Bug cannot jump backward twice in a row.Bug cannot jump to any forbidden positions given in the array.Bug can jump forward beyond its home, but it cannot jump to positions numbered with ... Read More

Find Inheritance Order in a Family Using Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:34:03

2K+ Views

Suppose, there is a family that consists of members from different generations. Such as the family has a father, his children, and their grandmother. But births and deaths happen in each family.The eldest member of the family is considered the head. So, when the 'head' member dies, their direct successor or their children becomes the head. We implement three functions, the first one is used when a child is born into the family. The function takes the parent's name and child's name as input and adds them to the record.The second function is used when there is a death. It ... Read More

Minimum Rotations to Maximize Profit from Ferris Wheel in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:30:39

218 Views

Suppose there is a Ferris wheel with four cabins and each cabin can contain four passengers. The wheel rotates counter-clockwise, and for each rotation, it costs 'run' amount of money. We now have an array 'cust' that contains n items and each item i signifies the number of people waiting to get into the Ferris wheel before the i-th rotation. To board the wheel, each customer has to pay an amount of money 'board', and that much money is for one anti-clockwise rotation of the wheel. The people waiting in line should not wait if there is any vacant seat ... Read More

Find Minimum Deletions to Make String Balanced in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:28:25

596 Views

Suppose we have a string s with only two characters 's' and 't'. We can delete any number of characters of s to make the string balanced. We can say, s is balanced when there is no pair of indices (i, j) such that i < j and s[i] = 't' and s[j]= 's'. We have to find the minimum number of deletions needed to make s balanced.So, if the input is like s = "sststtst", then the output will be 2 because we can either, remove the characters at indices 2 and 6 ("sststtst" to "sssttt"), or remove the ... Read More

Advertisements