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
22 changes: 22 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time Complexity :O(n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :no

/*
Approach
idea here is to buy only if the next day the price goes up
single pass as we are only deal this the next ele to i
*/

class Solution {
public int maxProfit(int[] prices) {
int total = 0;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i] < prices[i + 1]) {
total += prices[i + 1] - prices[i];
}
}
return total;
}
}
51 changes: 51 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time Complexity :O(1)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :no

/*
Approach
we are using a native iterator to implement this iterator,
you native iterator is alway one ele ahead of the peeking iterator

when the peeking iterator is initialised we store the first ele

if peek it called we just return the value of the stored ele

if next is called, as we alreay have the next value stored in ele we return that value but
before we return we check if there is a next ele if yes we save that

if hasnext it checks if nextEl is null or not, and returns accordingly
*/

class PeekingIterator implements Iterator<Integer> {
Iterator<Integer> iter;
Integer nextEl;

public PeekingIterator(Iterator<Integer> iterator) {
this.iter = iterator;
this.nextEl = iter.next();
}

public Integer peek() {
return nextEl;
}

@Override
public Integer next() {
Integer temp = nextEl;
nextEl = null;
if (iter.hasNext()) {
nextEl = iter.next();
}
return temp;
}

@Override
public boolean hasNext() {
if (nextEl != null) {
return true;
}
return false;
}
}