From 72c0fd14ef4fb7de27b75b7a6458846f6acc9d49 Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Mon, 15 Jun 2026 16:02:51 -0700 Subject: [PATCH] Competitive Coding 10 --- BestTimetoBuyandSellStock.java | 27 ++++++++++++++++++ PeekingIterator.java | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 BestTimetoBuyandSellStock.java create mode 100644 PeekingIterator.java diff --git a/BestTimetoBuyandSellStock.java b/BestTimetoBuyandSellStock.java new file mode 100644 index 0000000..f0ae71e --- /dev/null +++ b/BestTimetoBuyandSellStock.java @@ -0,0 +1,27 @@ +// Time Complexity : O(N) where N is the length of the input array, as we need to iterate through the array once to calculate the total profit. +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach +// The idea is to iterate through the array of stock prices and calculate the profit for each pair of consecutive days. If the price on the next day is higher than the price on the current day, we can make a profit by buying on the current day and selling on the next day. We add this profit to our total profit. By doing this for all pairs of consecutive days, we can calculate the maximum profit that can be achieved by making multiple transactions (buying and selling multiple times). +// The time complexity of this approach is O(N) where N is the length of the input array, and the space complexity is O(1) since we are using only a constant amount of extra space to store the total profit. +// The maxProfit method takes an array of stock prices as input and returns the maximum profit that can be achieved by making multiple transactions. +// We initialize a variable totalProfit to 0, and then iterate through the array of prices. For each pair of consecutive days, we check if the price on the next day is higher than the price on the current day. If it is, we calculate the profit by subtracting the current day's price from the next day's price and add it to totalProfit. Finally, we return totalProfit as the result. +class Solution { + + public int maxProfit(int[] prices) { + + int totalProfit = 0; + + for (int i = 0; i < prices.length - 1; i++) { + + if (prices[i] < prices[i + 1]) { + totalProfit += prices[i + 1] - prices[i]; + } + } + + return totalProfit; + } +} \ No newline at end of file diff --git a/PeekingIterator.java b/PeekingIterator.java new file mode 100644 index 0000000..0f34287 --- /dev/null +++ b/PeekingIterator.java @@ -0,0 +1,51 @@ +// Time Complexity : O(1) for peek(), next() and hasNext() methods, as we are only performing a constant number of operations in each method. +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +// Brute Force Approach : We can use an additional variable to store the next element in the iterator. The peek() method will return this variable, while the next() method will return the current next element and update it to the next element in the iterator. The hasNext() method will check if there is a next element available by checking if the next element variable is not null. +//Your code here along with comments explaining your approach +// The PeekingIterator class implements the Iterator interface and provides an additional peek() method that allows us to look at the next element without advancing the iterator. We maintain a reference to the next element in the iterator, which is updated whenever we call next(). The hasNext() method checks if there is a next element available by checking if the nextElement variable is not null. +// The constructor initializes the iterator and sets the nextElement to the first element of the iterator if it exists. The peek() method simply returns the nextElement without advancing the iterator. The next() method returns the current nextElement and then updates it to the next element in the iterator, or null if there are no more elements. The hasNext() method checks if there is a next element by checking if nextElement is not null. +// Your PeekingIterator object will be instantiated and called as such: +// PeekingIterator obj = new PeekingIterator(iter); +// Integer param_1 = obj.peek(); +// Boolean param_2 = obj.hasNext(); +// Time Complexity : O(1) for peek(), next() and hasNext() methods, as we are only performing a constant number of operations in each method. + +import java.util.Iterator; + +class PeekingIterator implements Iterator { + + private Iterator iterator; + private Integer nextElement; + + public PeekingIterator(Iterator iterator) { + this.iterator = iterator; + + if (iterator.hasNext()) { + nextElement = iterator.next(); + } + } + + public Integer peek() { + return nextElement; + } + + @Override + public Integer next() { + + Integer result = nextElement; + + nextElement = iterator.hasNext() + ? iterator.next() + : null; + + return result; + } + + @Override + public boolean hasNext() { + return nextElement != null; + } +} \ No newline at end of file