Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions BestTimetoBuyandSellStock.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
51 changes: 51 additions & 0 deletions PeekingIterator.java
Original file line number Diff line number Diff line change
@@ -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<Integer> {

private Iterator<Integer> iterator;
private Integer nextElement;

public PeekingIterator(Iterator<Integer> 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;
}
}