Skip to content

Commit

Permalink
Add best-time-to-buy-and-sell-stock solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehay28 committed Jan 6, 2025
1 parent 935f123 commit 181a129
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions best-time-to-buy-and-sell-stock/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {number[]} prices
* @return {number}
*/

// TC : O(n)
// SC : O(1)

var maxProfit = function (prices) {
if (prices.length === 1) {
return 0;
}

// Two variables (profitMax and priceMin) are used to store the maximum profit and minimum price seen, which require O(1) space.
let profitMax = 0;
let priceMin = prices[0];

for (const price of prices) {
const profit = price - priceMin;
profitMax = Math.max(profit, profitMax);
priceMin = Math.min(price, priceMin);
}

return profitMax;
};

// Why Constants Are Ignored in Big-O
// In Big-O notation, O(2) is simplified to O(1) because constants are irrelevant in asymptotic analysis.
// Big-O focuses on how resource usage scales with input size, not fixed values.

// Using 2 variables: O(1)
// Using 10 variables: O(1)
// Using 100 variables: O(1)

// What Space Complexity Looks Like for Larger Growth
// O(n): Memory grows linearly with the input size (e.g., storing an array of n elements).
// O(n^2): Memory grows quadratically (e.g., a 2D matrix with n*n elements).
// 𝑂(log 𝑛): Memory grows logarithmically (e.g., recursive calls in binary search).
// O(1): Fixed memory usage, regardless of input size (e.g., using a fixed number of variables).


0 comments on commit 181a129

Please sign in to comment.