2280. Minimum Lines to Represent a Line Chart LeetCode Solution
In this guide, you will get 2280. Minimum Lines to Represent a Line Chart LeetCode Solution with the best time and space complexity. The solution to Minimum Lines to Represent a Line Chart problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Lines to Represent a Line Chart solution in C++
- Minimum Lines to Represent a Line Chart solution in Java
- Minimum Lines to Represent a Line Chart solution in Python
- Additional Resources
Problem Statement of 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:
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 <= 105
stockPrices[i].length == 2
1 <= dayi, pricei <= 109
All dayi are distinct.
Complexity Analysis
- Time Complexity: O(\texttt{sort})
- Space Complexity: O(\texttt{sort})
2280. Minimum Lines to Represent a Line Chart LeetCode Solution in C++
class Solution {
public:
int minimumLines(vector<vector<int>>& stockPrices) {
int ans = 0;
ranges::sort(stockPrices);
for (int i = 2; i < stockPrices.size(); ++i) {
const pair<int, int> a = getSlope(stockPrices[i - 2], stockPrices[i - 1]);
const pair<int, int> b = getSlope(stockPrices[i - 1], stockPrices[i]);
if (a != b)
++ans;
}
return ans + (stockPrices.size() > 1);
}
private:
pair<int, int> getSlope(const vector<int>& p, const vector<int>& q) {
const int dx = p[0] - q[0];
const int dy = p[1] - q[1];
if (dx == 0)
return {0, p[0]};
if (dy == 0)
return {p[1], 0};
const int d = __gcd(dx, dy);
return {dx / d, dy / d};
}
};
/* code provided by PROGIEZ */
2280. Minimum Lines to Represent a Line Chart LeetCode Solution in Java
class Solution {
public int minimumLines(int[][] stockPrices) {
int ans = 0;
Arrays.sort(stockPrices, (a, b) -> Integer.compare(a[0], b[0]));
for (int i = 2; i < stockPrices.length; ++i) {
Pair<Integer, Integer> a = getSlope(stockPrices[i - 2], stockPrices[i - 1]);
Pair<Integer, Integer> b = getSlope(stockPrices[i - 1], stockPrices[i]);
if (a.getKey() != b.getKey() || a.getValue() != b.getValue())
++ans;
}
return ans + (stockPrices.length > 1 ? 1 : 0);
}
private Pair<Integer, Integer> getSlope(int[] p, int[] q) {
final int dx = p[0] - q[0];
final int dy = p[1] - q[1];
if (dx == 0)
return new Pair<>(0, p[0]);
if (dy == 0)
return new Pair<>(p[1], 0);
final int d = gcd(dx, dy);
return new Pair<>(dx / d, dy / d);
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// code provided by PROGIEZ
2280. Minimum Lines to Represent a Line Chart LeetCode Solution in Python
class Solution:
def minimumLines(self, stockPrices: list[list[int]]) -> int:
ans = 0
stockPrices.sort()
def getSlope(p: list[int], q: list[int]) -> tuple[int, int]:
dx = p[0] - q[0]
dy = p[1] - q[1]
if dx == 0:
return (0, p[0])
if dy == 0:
return (p[1], 0)
d = gcd(dx, dy)
return (dx // d, dy // d)
for i in range(2, len(stockPrices)):
a = getSlope(stockPrices[i - 2], stockPrices[i - 1])
b = getSlope(stockPrices[i - 1], stockPrices[i])
if a != b:
ans += 1
return ans + (len(stockPrices) > 1)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.