Found 10476 Articles for Python

Program to find path with minimum effort in Python

Arnab Chakraborty
Updated on 05-Oct-2021 11:51:58

415 Views

Suppose we have 2D matrix of order m x n called height. The heights[i][j] represents the height of cell (i, j). If we are at (0, 0) cell we want to travel to the bottom-right cell, (m-1, n-1). We can move up, down, left, or right, and we wish to find a route that requires the minimum effort. In this problem the roots effort is the maximum absolute difference in heights between two consecutive cells of the route. So finally, we need to find minimum efforts needed to travel to the destination.So, if the input is like234495646then the output will ... Read More

Program to find best team with no conflicts in Python

Arnab Chakraborty
Updated on 05-Oct-2021 11:40:57

527 Views

Suppose we have two lists called scores and ages, where scores[i] and ages[i] represents the score and age of the ith player in a basketball game. We want to select the team with the highest overall score. Here the score of the team is the total sum of scores of all the players in the team. But we do not allow conflicts in the game. Here a conflict exists if a younger player has a strictly higher score than an older player.So, if the input is like scores = [5, 7, 9, 14, 19], ages = [5, 6, 7, 8, ... Read More

Program to find lexicographically smallest string after applying operations in Python

Arnab Chakraborty
Updated on 05-Oct-2021 11:25:54

299 Views

Suppose we have a string s with only numeric digits and also have two values a and b. We can apply any one of the following two operations any number of times and in any order on s −Add 'a' to all odd positioned items of s(0-indexed). If digit is 9, then by adding something with it will be cycled back to 0.Rotate 's' to the right by b positions.So we have to find the lexicographically smallest string we can get by applying the above operations any number of times on s.So, if the input is like s = "5323" ... Read More

Program to find number of sets of k-non-overlapping line segments in Python

Arnab Chakraborty
Updated on 05-Oct-2021 11:16:36

324 Views

Suppose we have n points on a line, where the ith point (from 0 to n-1) is at position x = i, we have to find the number of ways we can draw exactly k different non-overlapping line segments such that each segment covers two or more points. The endpoints of each line segment must have integral coordinates. The k line segments do not have to cover all given n points, and they can share endpoints. If the answer is too large, then return result mod 10^9+7.So, if the input is like n = 4 k = 2, then the ... Read More

Program to find maximal network rank in Python

Arnab Chakraborty
Updated on 05-Oct-2021 10:54:28

438 Views

Suppose there are n cities and there are some roads connecting these cities. Each roads[i] = [u, v] indicates that there is a two-way road between cities u and v. Now consider network rank is the total number of directly connected roads to either city. When a road is directly connected to both cities, it is only counted once. And the maximal network rank of the network is the maximum network rank of all pairs of different cities. So, if we have different roads, we have to find the maximal network rank of the entire network.So, if the input is ... Read More

Program to find valid matrix given row and column sums in Python

Arnab Chakraborty
Updated on 04-Oct-2021 13:15:42

340 Views

Suppose we have two arrays rowSum and colSum with non-negative values where rowSum[i] has the sum of the elements in the ith row and colSum[j] has the sum of the elements in the jth column of a 2D matrix. We have to find any matrix with non-negative values of size (rowSum size x colSum size) that satisfies the given rowSum and colSum values.So, if the input is like rowSum = [13, 14, 12] colSum = [9, 13, 17], then the output will be9400950012To solve this, we will follow these steps −matrix := create empty matrixvisited := a new setDefine a ... Read More

Program to find maximum non negative product in a matrix in Python

Arnab Chakraborty
Updated on 04-Oct-2021 11:53:09

180 Views

Suppose we have a matrix of order m x n. Initially we are at the top-left corner cell (0, 0), and in each step, we can only move right or down in the matrix. Now among all possible paths from the top-left corner cell (0, 0) to bottom-right corner cell(m-1, n-1), we have to find the path with the maximum non-negative product. If the answer is too large, then return the maximum non-negative product modulo 10^9+7.So, if the input is like2-422-424-82then the output will be 256 because the path is the colored one, 2-422-424-82so product is [2 * 2 * ... Read More

Program to find split a string into the max number of unique substrings in Python

Arnab Chakraborty
Updated on 04-Oct-2021 10:07:49

376 Views

Suppose we have a string s, we have to find the maximum number of unique substrings that the given string can be split into. We can split string s into any list of non-empty substrings such that the concatenation of the substrings forms the original string. But we must split the substrings such that all of them are same.So, if the input is like s = "pqpqrrr", then the output will be 5 because we can split it like ['p', 'q', 'pq', 'r', 'rr']. If we split like ['p', 'q', 'p', 'q', 'r', 'rr'] is not valid because here 'p' ... Read More

Program to make sum divisible by P in Python

Arnab Chakraborty
Updated on 04-Oct-2021 10:00:07

401 Views

Suppose we have an array nums and another value p, we remove the smallest subarray (not the whole array) such that the sum of the remaining values is divisible by p. We have to find the length of the smallest subarray that we need to remove, if there is no such subarray then return -1.So, if the input is like nums = [8, 2, 6, 5, 3] p = 7, then the output will be 1 because if we remove 3, then total sum will be 21 and that is divisible by 7.To solve this, we will follow these steps ... Read More

Program to find maximum sum obtained of any permutation in Python

Arnab Chakraborty
Updated on 04-Oct-2021 09:44:48

475 Views

Suppose we have an array nums, and another array called requests where requests[i] = [start_i, end_i], this represents the ith request asks for the sum of nums[start_i] + nums[start_i+1] + ... + nums[end_i-1] + nums[end_i]. We have to find the maximum total sum of all requests among all permutations of nums. The answer may be very large, so return it modulo 10^9+7.So, if the input is like nums = [10, 20, 30, 40, 50] requests = [[1, 3], [0, 1]], then the output will be 190, because if we arrange like [30, 50, 40, 20, 10] we get: from requests[0] ... Read More

Advertisements