
- 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 lexicographically smallest subsequence of size k in Python
Suppose we have a list of numbers called nums and another value k, we have to find the lexicographically smallest subsequence of size k.
So, if the input is like nums = [2, 3, 1, 10, 3, 4] k = 3, then the output will be [1, 3, 4]
To solve this, we will follow these steps −
- l := size of nums, r := k - 1
- out := a new list
- for j in range 0 to k, do
- mn := nums[complement of r]
- for i in range r to l, do
- if mn >= nums[complement of i], then
- mn := nums[complement of i]
- l := i
- if mn >= nums[complement of i], then
- r := r - 1
- insert mn at the end of out
- return out
Example (Python)
Let us see the following implementation to get better understanding −
class Solution: def solve(self, nums, k): l, r = len(nums), k - 1 out = [] for j in range(k): mn = nums[~r] for i in range(r, l): if mn >= nums[~i]: mn = nums[~i] l = i r -= 1 out.append(mn) return out ob = Solution() nums = [2, 3, 1, 10, 3, 4] k = 3 print(ob.solve(nums, k))
Input
[2, 3, 1, 10, 3, 4], 3
Output
[1, 3, 4]
- Related Articles
- Program to find lexicographically smallest lowercase string of length k and distance n in Python
- Program to find kth smallest n length lexicographically smallest string in python
- Program to find lexicographically smallest non-palindromic string in Python
- Program to find Lexicographically Smallest String With One Swap in Python
- Program to find lexicographically smallest string after applying operations in Python
- JavaScript Program to Find Lexicographically smallest rotated sequence
- Program to find lexicographically smallest string to move from start to destination in Python
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Program to find smallest value of K for K-Similar Strings in Python
- Program to Find Out the Largest K-Divisible Subsequence Sum in Python
- Maximum product of subsequence of size k in C++
- Program to find number of increasing subsequences of size k in Python
- Program to find max values of sublists of size k in Python
- Subsequence of size k with maximum possible GCD
- Smallest Subsequence of Distinct Characters in Python

Advertisements