
- 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
Divide Array Into Increasing Sequences in Python
Suppose we have a non-decreasing array of positive integers called nums and an integer K, we have to find out if this array can be divided into one or more number of disjoint increasing subsequences of length at least K.
So, if the input is like nums = [1,2,2,3,3,4,4], K = 3, then the output will be true, as this array can be divided into the two subsequences like [1,2,3,4] and [2,3,4] with lengths at least 3 each.
To solve this, we will follow these steps −
d := a new map
req := 0
for each i in nums, do
if i not in d is non-zero, then
d[i]:= 1
otherwise,
d[i] := d[i] + 1
req := maximum of req, d[i]
return true when req*K <= size of nums
Let us see the following implementation to get better understanding −
Example
class Solution(object): def canDivideIntoSubsequences(self, nums, K): d = {} req = 0 for i in nums: if i not in d: d[i]=1 else: d[i]+=1 req = max(req,d[i]) return req*K<=len(nums) ob = Solution() print(ob.canDivideIntoSubsequences([1,2,2,3,3,4,4],3))
Input
[1,2,2,3,3,4,4]. 3
Output
True
- Related Articles
- Making two sequences increasing in JavaScript
- Converting array into increasing sequence in JavaScript
- Minimum Swaps To Make Sequences Increasing in C++
- Total number of longest increasing sequences in JavaScript
- How to divide an array into half in java?
- Program to find number of strictly increasing colorful candle sequences are there in Python
- All ways to divide array of strings into parts in JavaScript
- Divide a scalar value into every element of a masked Array in NumPy
- Print all increasing sequences of length k from first n natural numbers in C++
- Removing least number of elements to convert array into increasing sequence using JavaScript
- Program to sort array by increasing frequency of elements in Python
- How to generate sequences in Python?
- Divide a scalar value into every element of a masked Array with __truediv__() in NumPy
- Divide every element of a masked Array into a scalar value with __rtruediv__() in NumPy
- Make Array Strictly Increasing in C++

Advertisements