Longest Increasing Price Subsequence
Updated
Problem statement
You are given an integer array prices containing financial values in chronological order. The values can represent recorded prices or consecutive monthly net profits; net profits may be negative or zero. Return the maximum number of values you can select so that the selected values are strictly increasing.
The selected values must keep their original order, but they do not need to be adjacent. In other words, find the length of the longest strictly increasing subsequence of prices.
- You may skip any number of positions.
- Every selected value after the first must be greater than the previously selected value. Equal values cannot extend the subsequence.
- Return only the length of the subsequence.
Function
lengthOfLIS(prices: int[]) → intExamples
Example 1
prices = [10,9,2,5,3,7,101,18]return = 4One longest strictly increasing subsequence is [2, 3, 7, 18]. Its elements occur in their original order, and its length is 4.
Example 2
prices = [4,10,4,3,8,9]return = 3Select [4, 8, 9], skipping the intervening prices. No strictly increasing subsequence of length 4 exists, so the answer is 3.
Example 3
prices = [7,7,7]return = 1All prices are equal. A strictly increasing subsequence can select only one of them.
Example 4
prices = [-1,9,0,8,-5,6,-24]return = 3These are monthly net profits. Select [-1, 0, 6] in chronological order, skipping intervening months. Its length is 3, and no strictly increasing subsequence of length 4 exists.
Constraints
- For this exercise, assume
1 <= prices.length <= 2500. - For this exercise, assume every value is an integer and
-10^9 <= prices[i] <= 10^9.
More OpenAI Online Assessment questions
- Count Routes Through Four Shop TypesOA · Seen Oct 2026
- Digit-Sum Sequence Join PointOA · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
- Password Clues and the Next CommandOA · Seen Oct 2026
- Macaron Order PriceOA · Seen Oct 2026
- Shortest Atom Action PathOA · Seen Oct 2026
- Simplify a Quadratic ExpressionOA · Seen Oct 2026
- Minimum-Cost Color SequenceOA · PHONE SCREEN · Seen Oct 2026
- Largest Microorganism After ConsumptionOA · Seen Oct 2026
- Invert or Blur an ImageOA · Seen Sep 2026
Similar questions at other companies
- Minimum Cost to Attend Required CoursesVisa · OAVisa interview questions
- Fill a Time Window with SongsMicrosoft · OAMicrosoft interview questions
- Maximum Taxi EarningsWells Fargo · OAWells Fargo interview questions
- Longest Increasing Subsequence from Two ArraysSquarepoint Capital · OASquarepoint Capital interview questions
- Split Array Largest SumLinkedIn · OALinkedIn interview questions
- Longest Increasing SubsequenceAtlassian · OAAtlassian interview questions
- Maximum Profit from an Increasing Price TripletOdoo · OAOdoo interview questions
- Longest Increasing Subsequence LengthApple · OAApple interview questions
- Beautiful RowNutanix · OANutanix interview questions
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions