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 598 of 855
Program to find maximum difference of adjacent values after deleting k numbers in python
Given a sorted list of numbers, we need to delete k values to minimize the maximum difference between adjacent elements. This problem uses dynamic programming to find the optimal solution. Problem Understanding If we have nums = [15, 20, 30, 400, 1500] and k = 2, removing [400, 1500] leaves [15, 20, 30] with maximum adjacent difference of 10 (between 20 and 30). Algorithm Steps The approach follows these steps − Calculate differences between consecutive elements Use dynamic programming to try removing elements from both ends Find the minimum possible maximum difference ...
Read MoreProgram to find maximum credit we can get by finishing some assignments in python
Suppose we have two lists of the same size representing course assignments: deadlines and credits. Here deadlines[i] shows the deadline day for assignment i and credits[i] represents the amount of credits we get for assignment i. We have one day to complete an assignment, and it can be completed before or on the deadline day. We cannot do multiple assignments at the same time. We have to find the maximum credit we can gain by finishing some subset of assignments. So, if the input is like deadlines = [1, 2, 2, 2] and credits = [4, 5, 6, 7], ...
Read MoreProgram to check number of ways we can move k times and return back to first place in python
Suppose we are at position 0 of an n-length array, and on each step, we can move right one place, left one place (not exceeding bounds), or stay at the same position. We need to find how many unique walks of exactly k steps return us back to index 0. If the answer is very large, return it modulo 10^9 + 7. So, if the input is like n = 7, k = 4, then the output will be 9. The possible action sequences are ? [Right, Right, Left, Left] [Right, Left, Right, Left] [Stay, Stay, Stay, ...
Read MoreProgram to find number of steps to solve 8-puzzle in python
The 8-puzzle is a classic sliding puzzle game where you have a 3x3 grid with numbers 0-8, and you need to arrange them in order by sliding tiles into the empty space (represented by 0). This problem asks us to find the minimum number of steps to solve the puzzle using a breadth-first search approach. Understanding the Problem Given a 3x3 board with numbers 0-8 (no duplicates), we can swap the 0 with any of its adjacent neighbors. The goal is to arrange the numbers in sequential order from 0 to 8. For example, if we have ...
Read MoreProgram to check whether we can form 24 by placing operators in python
Suppose we have a list of four numbers, each number is in range 1 to 9, in a fixed order. We need to place operators (+, -, *, /) between the numbers and group them with brackets to check whether it is possible to get the value 24 or not. So, if the input is like nums = [5, 3, 6, 8, 7], then the output will be True, as (5 * 3) - 6 + (8 + 7) = 24. Approach We will use a recursive approach to generate all possible results by splitting the array ...
Read MoreProgram to find number of days it will take to burn all trees in python
Suppose we have a 2D matrix that represents a forest where there are three types of cells: 0 (empty cell), 1 (tree cell), and 2 (tree on fire cell). Every day, a tree catches fire when there is an adjacent (top, down, left, right, not diagonal) tree on fire. We have to find the number of days it would take for every tree to be on fire. If that is not possible, return -1. Problem Example If the input matrix is: 1 2 1 1 0 1 1 1 1 ...
Read MoreProgram to count number of paths whose sum is k in python
In this problem, we need to find the number of paths in a binary tree that sum to a target value k. The paths can start and end at any nodes, not necessarily from root to leaf. So, if the input is like: 3 2 4 1 2 ...
Read MoreProgram to check whether we can color a tree where no adjacent nodes have the same color or not in python
Suppose we have a binary tree where the value of each node represents its color. There are at most 2 colors in a tree. We have to check whether it is possible to swap the colors of the nodes any number of times so that no two connected nodes have the same color. So, if the input is like: .node { fill: lightblue; stroke: black; stroke-width: 2; } .text { font-family: Arial; font-size: 14px; text-anchor: middle; } .line { stroke: black; stroke-width: ...
Read MoreProgram to find the final ranking of teams in order from highest to lowest rank in python
When we have a list of voting strings where each string represents votes on candidates in order from highest to lowest preference, we need to rank candidates based on their voting positions. The ranking follows a specific priority: first by highest preference votes, then by second preference votes if tied, and finally alphabetically if still tied. So, if the input is like votes = ["zyx", "zxy", "xyz"], then the output will be "zxy". Here's why: 'z' received 2 votes for highest preference (position 0), 'x' received 1 vote for highest preference, and 'y' received 0 votes for highest preference. ...
Read MoreProgram to count number of surrounded islands in the matrix in python
In this problem, we have a binary matrix where 1 represents land and 0 represents water. An island is a group of connected 1s, and we need to find islands that are completely surrounded by water (not touching the matrix boundaries). So, if the input matrix is like: ...
Read More