Skip to content

Problem1 added - #472

Open
megharaykar wants to merge 1 commit into
super30admin:masterfrom
megharaykar:master
Open

Problem1 added#472
megharaykar wants to merge 1 commit into
super30admin:masterfrom
megharaykar:master

Conversation

@megharaykar

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Interview Problem : Buy and Sell Stock (Problem1.py)

Your solution is correct and efficient. You have implemented the greedy approach which is optimal for this problem. However, note the following:

  • You should not define two classes with the same name in the same file. Choose one implementation. The for loop version is preferable because it is more concise and avoids the redundant increment.
  • In the for loop solution, the line i += 1 is unnecessary because the for loop iterates over range(1, len(prices)) and i is automatically incremented each iteration. You can remove that line.
  • It's good practice to add comments explaining the approach. For example, you could mention that you are capturing every positive daily price change.

Here is a cleaned-up version of your code:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profit = 0
        for i in range(1, len(prices)):
            if prices[i] > prices[i-1]:
                profit += prices[i] - prices[i-1]
        return profit

VERDICT: PASS


Interview Problem: Peeking Iterator

It seems there has been a mix-up in your submission. You have provided a solution for "Best Time to Buy and Sell Stock II" instead of the "Peeking Iterator" problem.

For the PeekingIterator problem, you need to design a class that wraps an existing iterator and provides a peek() method that returns the next element without advancing the iterator. The key challenge is to maintain the next element in a variable so that peek() can return it without consuming it.

Here are some tips for implementing PeekingIterator:

  • You should store the underlying iterator and the next element (if available).
  • In the constructor, initialize the next element by calling next() on the underlying iterator if it has elements.
  • The peek() method simply returns the stored next element.
  • The next() method returns the stored next element and then updates it by calling next() on the underlying iterator if available.
  • The hasNext() method checks if the stored next element is not null.

Please review the problem statement again and try to implement the correct solution. If you need further clarification, feel free to ask.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants