Found 10476 Articles for Python

Program to find island count by adding blocks into grid one by one in Python

Arnab Chakraborty
Updated on 26-Dec-2020 11:16:42

159 Views

Suppose we have one infinite grid of water. We can add blocks of land to that grid one by one. We have a list of coordinates called land_requests where each coordinate is in the form [r, c] where r is for row and c is for column. We have to find a list where each element represents the number of islands that exist after adding each block of land from land_requests.So, if the input is like land_requests = [[1, 1], [2, 4], [1, 2], [1, 4], [1, 3]], then the output will be [1, 2, 2, 2, 1] asTo solve ... Read More

Program to find sum of rectangle whose sum at most k in Python

Arnab Chakraborty
Updated on 26-Dec-2020 11:12:30

315 Views

Suppose we have a 2d matrix and another value k, we have to find the largest sum of a rectangle where sum ≤ k.So, if the input is like5−2710and k = 15, then the output will be 12, as we can take the rectangle [5, 7] to get sum of 12 less than 15.To solve this, we will follow these steps −n := row count of am := column count of aans := inffor i1 in range 0 to n, dorow := a list of size m and fill with 0for i2 in range i1 to n, dofor j in ... Read More

Program to find maximum difference of any number and its next smaller number in Python

Arnab Chakraborty
Updated on 26-Dec-2020 11:09:09

298 Views

Suppose we have a list of numbers called nums, we have to find the maximum difference that exists between any number and the next smaller number. Our goal is to solve this in linear time.So, if the input is like nums = [14, 2, 6, 35, 12], then the output will be 21, as 35 and 14 have the largest difference of 21.To solve this, we will follow these steps −max_val := maximum of nums, min_val := minimum of numsif max_val is same as min_val, thenreturn 0delta := (max_val − min_val) / (size of nums − 1)min_map := an empty ... Read More

Program to find largest island after changing one water cell to land cell in Python

Arnab Chakraborty
Updated on 26-Dec-2020 11:02:39

190 Views

Suppose we have a binary matrix where 1 represents land and 0 represents water. And an island is a group of 1s that are surrounded by water. We have to find the size of the largest island. We are allowed to change at most one water cell to land cell.So, if the input is like101000110111then the output will be 7, as we can turn one water cells to land to connect the two islands. So final matrix is like −101000110111To solve this, we will follow these steps −R := row count of mat, C := column count of matmass := ... Read More

Program to find largest average of sublist whose size at least k in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:59:53

307 Views

Suppose we have a list of numbers called nums and another value k, we have to find the largest average value of any sublist of the list whose length is at least k.So, if the input is like nums = [2, 10, -50, 4, 6, 6] k = 3, then the output will be 5.33333333, as sublist [4, 6, 6] has the largest average valueTo solve this, we will follow these steps −left := minimum of nums, right := maximum of numss := sum of all numbers in nums from index 0 to k − 1largest_avg := s / kwhile left

Program to find kth lexicographic sequence from 1 to n of size k Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:57:54

205 Views

Suppose we have two values n and k. Now consider a list of numbers in range 1 through n [1, 2, ..., n] and generating every permutation of this list in lexicographic sequence. For example, if n = 4 we have [1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321]. We have to find the kth value of this permutation sequence as a string.So, if the input is like n = 4 k = 5, then the output will be "1432"To solve this, we will ... Read More

Program to find number of distinct island shapes from a given matrix in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:52:44

304 Views

Suppose we have a 2d binary matrix, we have to find the number of distinct islands in the given matrix. Here 1 represents land and 0 represents water, so an island is a set of 1s that are close and whose perimeter is surrounded by water. Here two islands are unique if their shapes are different.So, if the input is like100001010101101001001000011011then the output will be 4 (distinct islands are in different color).To solve this, we will follow these steps −Define a function dfs() . This will take i, j, k, lmat[i, j] := 0insert pair (i − k, j − ... Read More

Program to count number of ways we can make a list of values by splitting numeric string in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:50:41

112 Views

Suppose we have a strings s. The s is containing digits from 0 - 9 and we also have another number k. We have to find the number of different ways that s can be represented as a list of numbers from [1, k]. If the answer is very large then return result mod 10^9 + 7.So, if the input is like s = "3456" k = 500, then the output will be 7, as we can represent s like [3, 4, 5, 6], [34, 5, 6], [3, 4, 56], [3, 45, 6], [34, 56], [345, 6], [3, 456]To solve ... Read More

Program to find number of coins we can pick from topleft to bottom-right cell and return in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:48:38

165 Views

Suppose we have a 2D matrix with 3 possible values −0 for an empty cell.1 for a coin.−1 for a wall.We have to find the maximum number of coins we can take by starting from the top−left cell and reaching the bottom−right cell by moving only right or down direction. Then come back to the top−left cell by only moving up or left direction. When we pick up a coin, the cell value becomes 0. If we cannot reach the bottom−right cell, then return 0.So, if the input is like011111−111011then the output will be 8.To solve this, we will follow ... Read More

Program to check person 1 can win the candy game by taking maximum score or not in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:44:27

327 Views

Suppose two players are playing a game. Where several candies placed on a line, and person 1 is given a list of numbers called nums that is representing the point value of each candy. On each person's turn, they can pick 1, 2, or 3 candies from the front of the line, then delete them from list, and get the sum of their points added to their score. This game will end when all the candies are deleted and the person with the higher score will be the winner. We have to check person 1 can win this game or ... Read More

Advertisements