Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 and connecting adjacent points. One such example is shown below:
Return the minimum number of lines needed to represent the line chart.
Example 1:
Input: stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]] Output: 3 Explanation: The diagram above represents the input, with the X-axis representing the day and Y-axis representing the price. The following 3 lines can be drawn to represent the line chart: - Line 1 (in red) from (1,7) to (4,4) passing through (1,7), (2,6), (3,5), and (4,4). - Line 2 (in blue) from (4,4) to (5,4). - Line 3 (in green) from (5,4) to (8,1) passing through (5,4), (6,3), (7,2), and (8,1). It can be shown that it is not possible to represent the line chart using less than 3 lines.
Example 2:
Input: stockPrices = [[3,4],[1,2],[7,8],[2,3]] Output: 1 Explanation: As shown in the diagram above, the line chart can be represented with a single line.
Constraints:
1 <= stockPrices.length <= 105stockPrices[i].length == 21 <= dayi, pricei <= 109dayi are distinct.Problem summary: 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 and connecting adjacent points. One such example is shown below: Return the minimum number of lines needed to represent the line chart.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]
[[3,4],[1,2],[7,8],[2,3]]
max-points-on-a-line)minimum-number-of-lines-to-cover-points)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2280: Minimum Lines to Represent a Line Chart
class Solution {
public int minimumLines(int[][] stockPrices) {
Arrays.sort(stockPrices, (a, b) -> a[0] - b[0]);
int dx = 0, dy = 1;
int ans = 0;
for (int i = 1; i < stockPrices.length; ++i) {
int x = stockPrices[i - 1][0], y = stockPrices[i - 1][1];
int x1 = stockPrices[i][0], y1 = stockPrices[i][1];
int dx1 = x1 - x, dy1 = y1 - y;
if (dy * dx1 != dx * dy1) {
++ans;
}
dx = dx1;
dy = dy1;
}
return ans;
}
}
// Accepted solution for LeetCode #2280: Minimum Lines to Represent a Line Chart
func minimumLines(stockPrices [][]int) int {
ans := 0
sort.Slice(stockPrices, func(i, j int) bool { return stockPrices[i][0] < stockPrices[j][0] })
for i, dx, dy := 1, 0, 1; i < len(stockPrices); i++ {
x, y := stockPrices[i-1][0], stockPrices[i-1][1]
x1, y1 := stockPrices[i][0], stockPrices[i][1]
dx1, dy1 := x1-x, y1-y
if dy*dx1 != dx*dy1 {
ans++
}
dx, dy = dx1, dy1
}
return ans
}
# Accepted solution for LeetCode #2280: Minimum Lines to Represent a Line Chart
class Solution:
def minimumLines(self, stockPrices: List[List[int]]) -> int:
stockPrices.sort()
dx, dy = 0, 1
ans = 0
for (x, y), (x1, y1) in pairwise(stockPrices):
dx1, dy1 = x1 - x, y1 - y
if dy * dx1 != dx * dy1:
ans += 1
dx, dy = dx1, dy1
return ans
// Accepted solution for LeetCode #2280: Minimum Lines to Represent a Line Chart
/**
* [2280] 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 and connecting adjacent points. One such example is shown below:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/03/30/1920px-pushkin_population_historysvg.png" style="width: 500px; height: 313px;" />
* Return the minimum number of lines needed to represent the line chart.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/03/30/ex0.png" style="width: 400px; height: 400px;" />
* Input: stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]
* Output: 3
* Explanation:
* The diagram above represents the input, with the X-axis representing the day and Y-axis representing the price.
* The following 3 lines can be drawn to represent the line chart:
* - Line 1 (in red) from (1,7) to (4,4) passing through (1,7), (2,6), (3,5), and (4,4).
* - Line 2 (in blue) from (4,4) to (5,4).
* - Line 3 (in green) from (5,4) to (8,1) passing through (5,4), (6,3), (7,2), and (8,1).
* It can be shown that it is not possible to represent the line chart using less than 3 lines.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/03/30/ex1.png" style="width: 325px; height: 325px;" />
* Input: stockPrices = [[3,4],[1,2],[7,8],[2,3]]
* Output: 1
* Explanation:
* As shown in the diagram above, 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.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/
// discuss: https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn minimum_lines(stock_prices: Vec<Vec<i32>>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2280_example_1() {
let stock_prices = vec![
vec![1, 7],
vec![2, 6],
vec![3, 5],
vec![4, 4],
vec![5, 4],
vec![6, 3],
vec![7, 2],
vec![8, 1],
];
let result = 3;
assert_eq!(Solution::minimum_lines(stock_prices), result);
}
#[test]
#[ignore]
fn test_2280_example_2() {
let stock_prices = vec![vec![3, 4], vec![1, 2], vec![7, 8], vec![2, 3]];
let result = 1;
assert_eq!(Solution::minimum_lines(stock_prices), result);
}
}
// Accepted solution for LeetCode #2280: Minimum Lines to Represent a Line Chart
function minimumLines(stockPrices: number[][]): number {
const n = stockPrices.length;
stockPrices.sort((a, b) => a[0] - b[0]);
let ans = 0;
let pre = [BigInt(0), BigInt(0)];
for (let i = 1; i < n; i++) {
const [x1, y1] = stockPrices[i - 1];
const [x2, y2] = stockPrices[i];
const dx = BigInt(x2 - x1),
dy = BigInt(y2 - y1);
if (i == 1 || dx * pre[1] !== dy * pre[0]) ans++;
pre = [dx, dy];
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.