
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to maximize the minimum value after increasing K sublists in Python
Suppose we have a list of numbers called nums and two values, size and k. Now suppose there is an operation where we take a contiguous sublist of length size and increment every element by one. We can perform this operation k times, we have to find the largest minimum value possible in nums.
So, if the input is like nums = [2, 5, 2, 2, 7], size = 3, k = 2, then the output will be 3, as we can increase [2, 5, 2] to get [3, 6, 3, 2, 7] and then increment [6, 3, 2] to get [3, 7, 4, 3, 7], minimum is 3
To solve this, we will follow these steps −
- Define a function possible() . This will take target
- events := A list of size N, and fill with 0
- moves := 0, s := 0
- for i in range 0 to N, do
- s := s + events[i]
- delta := target -(A[i] + s)
- if delta > 0, then
- moves := moves + delta
- s := s + delta
- if i + size < N, then
- events[i + size] := events[i + size] - delta
- return true when moves <= K
- From the main method, do the following
- N := size of A
- left := 0, right := 10^10
- while left < right, do
- mid :=(left + right + 1) / 2
- if possible(mid), then
- left := mid
- otherwise,
- right := mid - 1
- return left
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, A, size, K): N = len(A) def possible(target): events = [0] * N moves = s = 0 for i in range(N): s += events[i] delta = target - (A[i] + s) if delta > 0: moves += delta s += delta if i + size < N: events[i + size] -= delta return moves <= K left, right = 0, 10 ** 10 while left < right: mid = (left + right + 1)//2 if possible(mid): left = mid else: right = mid - 1 return left ob = Solution() nums = [2, 5, 2, 2, 7] size = 3 k = 2 print(ob.solve(nums, size, k))
Input
[2, 5, 2, 2, 7], 3, 2
Output
3
- Related Articles
- Program to find minimum possible maximum value after k operations in python
- Program to split lists into strictly increasing sublists of size greater than k in Python
- Program to find minimum largest sum of k sublists in C++
- Program to find minimum amplitude after deleting K elements in Python
- Maximize Sum Of Array After K Negations in Python
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Program to check whether list can be split into sublists of k increasing elements in C++
- Program to find maximize score after n operations in Python
- Program to find max values of sublists of size k in Python
- Adding value to sublists in Python
- Program to maximize the number of equivalent pairs after swapping in Python
- Maximize array sum after K negation in C++
- Program to maximize the minimum force between balls in a bucket using Python
- Program to count number of sublists with exactly k unique elements in Python
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python

Advertisements