Skip to content

Practice Minimum Lines to Represent a Line Chart

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2280
LeetCode Problem

Minimum Lines to Represent a Line Chart

Problem Statement

Each stockPrices entry [day,price] is a point. Order points by day and connect consecutive points to form a line chart. Return the minimum number of straight line segments needed to represent it, combining consecutive connections with the same slope. A single point needs no line.

Source: Minimum Lines to Represent a Line Chart on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]
Output
3

# Example 2

Input
stockPrices = [[3,4],[1,2],[7,8],[2,3]]
Output
1

Constraints

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

Related LeetCode Problems

Practice more problems similar to Minimum Lines to Represent a Line Chart.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.