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 341 of 855
Program 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 MoreProgram to determine if two strings are close in Python
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 ...
Read MoreProgram to find out the vertical area between two points where no point lies and is the widest in Python
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. ...
Read MoreProgram to find out the inheritance order in a family in Python
In family inheritance systems, we need to track the order of succession when the head of the family dies. This program implements a family inheritance tracker that manages births, deaths, and determines the inheritance order using a depth-first search approach. Problem Overview The inheritance follows these rules: The eldest living member is the head of the family When the head dies, their direct descendants inherit in order Children inherit before siblings The inheritance order follows a depth-first traversal of the family tree ...
Read More