
- 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 find minimum amplitude after deleting KLength sublist in Python
Suppose we have a list of numbers called nums and a value k. First we shall remove a sublist of size k, then find the minimum of (maximum of nums - minimum of nums).
So, if the input is like nums = [2, 3, 10, 9, 8, 4] k = 3, then the output will be 2, If we remove [10, 9, 8] we get [2, 3, 4] and 4 - 2 = 2
To solve this, we will follow these steps −
N := size of nums
copy nums into lmin and lmax
also copy nums into rmin and rmax
for i in range 1 to N - 1, do
lmin[i] := minimum of lmin[i] and lmin[i - 1]
lmax[i] := maximum of lmax[i] and lmax[i - 1]
for i in range N - 2 to 0, decrease by 1, do
rmin[i] := minimum of rmin[i] and rmin[i + 1]
rmax[i] := maximum of rmax[i] and rmax[i + 1]
ans := minimum of (rmax[k] - rmin[k]), (lmax[complement of k] - lmin[complement of k])
for i in range 0 to N - k - 2, do
cand := (maximum of lmax[i] and rmax[i + k + 1]) - (minimum of lmin[i] and rmin[i + k + 1])
ans := minimum of ans and cand
return ans
Example
Let us see the following implementation to get better understanding
def solve(nums, k): N = len(nums) lmin, lmax = nums[:], nums[:] rmin, rmax = nums[:], nums[:] for i in range(1, N): lmin[i] = min(lmin[i], lmin[i - 1]) lmax[i] = max(lmax[i], lmax[i - 1]) for i in range(N - 2, -1, -1): rmin[i] = min(rmin[i], rmin[i + 1]) rmax[i] = max(rmax[i], rmax[i + 1]) ans = min(rmax[k] - rmin[k], lmax[~k] - lmin[~k]) for i in range(N - k - 1): cand = max(lmax[i], rmax[i + k + 1]) - min(lmin[i], rmin[i + k + 1]) ans = min(ans, cand) return ans nums = [2, 3, 10, 9, 8, 4] k = 3 print(solve(nums, k))
Input
[2, 3, 10, 9, 8, 4], 3
Output
2
- Related Articles
- Program to find minimum amplitude after deleting K elements in Python
- Program to find minimum length of string after deleting similar ends in Python
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Program to find longest equivalent sublist after K increments in Python
- Program to find string after deleting k consecutive duplicate characters in python
- Program to find length of longest contiguously strictly increasing sublist after removal in Python
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Program to find longest subarray of 1s after deleting one element using Python
- Program to find minimum number colors remain after merging in Python
- Program to find shortest sublist so after sorting that entire list will be sorted in Python
- Program to find minimum possible maximum value after k operations in python
- Find all possible substrings after deleting k characters in Python
- Python program to find Sum of a sublist
- Program to find length of longest distinct sublist in Python
- Program to get indices of a list after deleting elements in ascending order in Python
