
- 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 max values of sublists of size k in Python
Suppose we have a list nums and another value k, we have to find the maximum values of each sublist of size k.
So, if the input is like nums = [12, 7, 3, 9, 10, 9] k = 3, then the output will be [12, 9, 10, 10]
To solve this, we will follow these steps −
if k > size of nums, then
return a blank list
res := a new list
temp := nums[0]
temp := npoint := 0
for i in range 0 to k − 1, do
if nums[i] > temp, then
temp := nums[i]
point := i
insert temp at the end of res
for i in range k to size of nums, do
if nums[i] < temp and (i − point) < k, then
temp := nums[point]
otherwise when nums[i] < temp and (i − point) >= k, then
point := i − k + 1
for j in range i − k + 1 to i, do
if nums[j] > nums[point], then
point := j
temp := nums[point]
otherwise,
temp := nums[i]
point := i
insert temp at the end of res
return res
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums, k): if k > len(nums): return [] res = [] temp = nums[0] point = 0 for i in range(k): if nums[i] > temp: temp = nums[i] point = i res.append(temp) for i in range(k, len(nums)): if nums[i] < temp and (i − point) < k: temp = nums[point] elif nums[i] < temp and (i − point) >= k: point = i − k + 1 for j in range(i − k + 1, i + 1): if nums[j] > nums[point]: point = j temp = nums[point] else: temp = nums[i] point = i res.append(temp) return res ob = Solution() nums = [12, 7, 3, 9, 10, 9] k = 3 print(ob.solve(nums, k))
Input
[12, 7, 3, 9, 10, 9], 3
Output
[12, 9, 10, 10]
- Related Articles
- Program to split lists into strictly increasing sublists of size greater than k in Python
- Program to find max number of K-sum pairs in Python
- Program to find number of sublists that contains exactly k different words in Python
- Program to find minimum largest sum of k sublists in C++
- Program to find number of sublists with sum k in a binary list in Python
- Program to find number of increasing subsequences of size k in Python
- Program to find lexicographically smallest subsequence of size k in Python
- Program to count number of sublists with exactly k unique elements in Python
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find largest average of sublist whose size at least k in Python
- Program to find k sublists with largest sums and return sums in ascending order in Python
- Program to find sum of the sum of all contiguous sublists in Python
- Program to maximize the minimum value after increasing K sublists in Python
- Program to find maximum sum of two non-overlapping sublists in Python
