Minimum Lines to Represent a Line Chart

You are given a 2D integer array stockPrices where stockPrices[i] = [dayi, pricei] indicates the price of the stock on day dayi is pricei.

A line chart is created from the array by plotting the points on an XY plane with the X-axis representing the day and the Y-axis representing the price, then connecting adjacent points.

Return the minimum number of lines needed to represent the line chart.

Example 1
InputstockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]
Output3
The line chart can be represented by 3 lines: one through days 1 to 4, one from day 4 to day 5, and one through days 5 to 8; it is not possible to use fewer than 3 lines.
Example 2
InputstockPrices = [[3,4],[1,2],[7,8],[2,3]]
Output1
The points all lie on the same line after ordering by day, so the line chart can be represented with a single line.

Constraints

  • 1 <= stockPrices.length <= 10^5
  • stockPrices[i].length == 2
  • 1 <= dayi, pricei <= 10^9
  • All dayi are distinct.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate