Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 137 of 855
Program to find minimum length of string after deleting similar ends in Python
Suppose we have a string s with only three characters 'a', 'b', and 'c'. We can apply the following algorithm on the string any number of times to minimize its length: Select a non-empty prefix from s where all characters in the prefix are the same. Select a non-empty suffix from s where all characters in the suffix are the same. The prefix and suffix must be disjoint (non-overlapping). The characters from the prefix and suffix must be the same. Remove both the prefix and the suffix from s. Our goal is to find the minimum ...
Read MoreProgram to count minimum semesters to cover all different courses in Python
Suppose we have a number n, indicating that there are n different courses labeled from 1 to n. We also have an array called relations where relations[i] contains a pair (prevCourse_i, nextCourse_i), representing a prerequisite relationship between the course prevCourse_i and the course nextCourse_i: so course prevCourse_i has to be taken before the course nextCourse_i. The last parameter we have is k. In one semester, we can take at most k number of courses as long as we have taken all the prerequisites in the previous semester for the courses we are taking. We have to find the minimum number ...
Read MoreProgram to find maximum absolute sum of any subarray in Python
Sometimes we need to find the maximum absolute sum of any subarray within an array. This problem extends Kadane's algorithm by considering both maximum and minimum subarray sums, as the absolute value of a negative sum might be larger than the positive maximum. Problem Understanding Given an array nums, we need to find the maximum absolute sum of any contiguous subarray. The absolute sum of a subarray [nums_l, nums_l+1, ..., nums_r] is |nums_l + nums_l+1 + ... + nums_r|. For example, if nums = [2, -4, -3, 2, -6], the subarray [2, -4, -3, 2] has sum ...
Read MoreProgram to check whether we can eat favorite candy on our favorite day in Python
Suppose we have an array of positive values candiesCount where candiesCount[i] denotes the number of candies of the ith type we have. We also have another array called queries where queries[i] has three parameters [favoriteType_i, favoriteDay_i, dailyCap_i]. We have some rules: We start eating candies on day 0. We cannot eat any candy of type i unless we have eaten all candies of previous i-1 types. We must eat at least one candy per day until we have eaten all of them. Following these rules, we have ...
Read MoreProgram to find out the critical and pseudo-critical edges in a graph in Python
A Minimum Spanning Tree (MST) contains the minimum weight edges that connect all vertices in a graph. In this problem, we need to identify critical edges (edges whose removal increases MST weight) and pseudo-critical edges (edges that can appear in some MSTs but not all). Problem Understanding Given an undirected weighted graph with n vertices (0 to n-1), we need to find: Critical edges: Removing them increases the MST weight Pseudo-critical edges: Can be part of an MST but aren't required ...
Read MoreProgram to find Kth ancestor of a tree node in Python
Suppose we have a tree with n nodes that are numbered from 0 to n-1. The tree is given by a parent array, where parent[i] is the parent of node i. The root of the tree is node 0. We have to find the kth ancestor of a given node. If the ancestor is not present, then return -1. So, if the input is like ? 0 1 2 ...
Read MoreProgram to find minimum total distance between house and nearest mailbox in Python
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] and 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. Algorithm To solve this problem, we ...
Read MoreProgram to find minimum cost for painting houses in Python
Suppose there is an array of size m representing m houses in a small city. Each house must be painted with one of the n colors (labeled from 1 to n). Some houses are already painted, so no need to paint them again. Houses colored with the same adjacent color form a neighborhood. We have the array houses, where houses[i] represents the color of house i. If the color value is 0, the house is not colored yet. We have another array called cost, which is a 2D array where cost[i][j] represents the cost to color house i with ...
Read MoreProgram to find where the ball lands in a grid box in Python
Suppose we are given an 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. A ball is dropped from each top cell, and we need to determine where each ball lands at the bottom of the box. The grid is represented as a matrix where 1 means the diagonal board spans from top-left to bottom-right, and -1 means it spans from top-right to bottom-left. If a ball gets stuck or goes out of bounds, we return -1 for that position. ...
Read MoreProgram to implement a queue that can push or pop from the front, middle, and back in Python
A flexible queue is a data structure that allows insertion and removal of elements from the front, middle, and back positions. This is useful when you need dynamic access to different parts of your queue. Understanding the Queue Operations Our queue supports six main operations ? push_from_front() − Insert element at the beginning push_from_middle() − Insert element at the middle position push_from_back() − Insert element at the end pop_from_front() − Remove and return first element pop_from_middle() ...
Read More