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 337 of 855
Program to find maximum average pass ratio in Python
Suppose we have a list of classes where classes[i] = [pass_i, total_i] represents the number of students who passed and the total number of students in the ith class respectively. We also have extra brilliant students guaranteed to pass any exam they're assigned to. We need to assign these extra students to maximize the average pass ratio across all classes. The pass ratio of a class is passed_students / total_students. The average pass ratio is the sum of all class ratios divided by the number of classes. Algorithm Approach We use a greedy approach with a max-heap ...
Read MoreProgram to find average waiting time in Python
Suppose we have an array customers, where customers[i] = [arrival_i, time_i]. Here arrival_i is the arrival time of the ith customer and time_i is the time needed to prepare the order. The cook processes orders sequentially and can only prepare one order at a time. We need to find the average waiting time of all customers. The waiting time for each customer is the total time from arrival until their order is completed. Problem Understanding Let's understand with an example. If the input is customers = [[7, 2], [8, 4], [10, 3], [20, 1]], then ? ...
Read MoreProgram to find center of star graph in Python
A star graph is an undirected graph with n nodes where one central node connects to all other n-1 nodes. In this tutorial, we'll learn how to identify the center node of a star graph represented as a list of edges. Understanding the Problem Given a star graph with n nodes labeled from 1 to n, we need to find the center node. The center node appears in every edge since it connects to all other nodes. ...
Read MoreProgram to find number of restricted paths from first to last node in Python
Suppose we have an undirected weighted connected graph with n nodes labeled from 1 to n. A restricted path is a special path from node 1 to node n where the distance to the destination decreases at each step. Specifically, for a path [z0, z1, z2, ..., zk], we need dist(zi) > dist(zi+1) where dist(x) is the shortest distance from node x to node n. We need to find the number of restricted paths from node 1 to node n, returning the result modulo 10^9 + 7. For example, if we have this graph: ...
Read MoreProgram to find maximum score we can get in jump game in Python
Suppose we have an array called nums and another value k. We are at index 0. In one move, we can jump at most k steps right without going outside the boundaries of the array. We want to reach the final index of the array. For jumping we get score, that is the sum of all nums[j] for each index j we visited in the array. We have to find the maximum score we can get. So, if the input is like nums = [1, -2, -5, 7, -6, 4] and k = 2, then the output will be ...
Read MoreProgram to find minimum elements to add to form a given sum in Python
When working with arrays that have element constraints, we often need to find the minimum number of elements to add to achieve a target sum. This problem involves an array where each element's absolute value is bounded by a limit, and we need to reach a specific goal sum. Problem Statement Given an array nums and two values limit and goal, where |nums[i]| ≤ limit for all elements, we need to find the minimum number of elements to insert so that the array sum equals the goal. The inserted elements must also satisfy the limit constraint. Example ...
Read MoreProgram to find maximum erasure value in Python
Suppose we have an array called nums (with positive values only) and we want to erase a subarray containing unique elements. We will get a score that is the sum of subarray elements. We have to find the maximum score we can get by erasing exactly one subarray. So, if the input is like nums = [6, 3, 2, 3, 6, 3, 2, 3, 6], then the output will be 11, because the optimal subarray is either [6, 3, 2] or [2, 3, 6], so the sum is 11. Algorithm To solve this, we will follow these ...
Read MoreProgram to find sum of beauty of all substrings in Python
Given a string, we need to find the sum of beauty of all its substrings. The beauty of a string is the difference between the frequencies of the most frequent and least frequent characters. For example, if the string is "abaacc", the frequency of 'a' is 3 and 'b' is 1, so beauty = 3 - 1 = 2. Problem Example If the input string is "xxyzy", the substrings with non-zero beauty are: "xxy" → beauty = 2 - 1 = 1 "xxyz" → beauty = 2 - 1 = 1 "xxyzy" → beauty = ...
Read MoreProgram to find minimum difference of stone games score in Python
Suppose we have an array called stones where stones[i] represents the value of the ith stone from the left. Two friends Amal and Bimal are playing a turn-based game with these stones and Amal starts first always. There are n stones arranged in a row. Each player can remove either the leftmost stone or the rightmost stone from the row and get points equal to the sum of the remaining stones' values in the row. The player with the higher score wins. Bimal found that he will always lose this game, so he decided to minimize the score's difference. ...
Read MoreProgram to check whether number is a sum of powers of three in Python
Suppose we have a number n, we have to check whether it is possible to represent n as the sum of distinct powers of three or not. An integer y is said to be power of three if there exists an integer x such that y = 3^x. So, if the input is like n = 117, then the output will be True because 117 = 3^4 + 3^3 + 3^2 = 81 + 27 + 9. Algorithm To solve this, we will follow these steps ? for i in range 16 to 0, decrease ...
Read More