Reveal Cards In Increasing Order - Leetcode 950 - Python

Поделиться
HTML-код
  • Опубликовано: 28 июн 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
    🐦 Twitter: / neetcode1
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    Problem Link: leetcode.com/problems/reveal-...
    0:00 - Read the problem
    0:20 - Drawing Explanation
    8:48 - Coding Explanation
    leetcode 950
    #neetcode #leetcode #python

Комментарии • 41

  • @vinit.khandelwal
    @vinit.khandelwal 2 месяца назад +27

    Making sense of question from all that deck crap is more difficult here than the solution

  • @kabeersingh459
    @kabeersingh459 2 месяца назад +11

    7:53-7:58 correction: [4 -> 5, 6 -> 7] index -> value in array. Nonetheless, thank you for the stellar explanation.

  • @DebopriyoBasu
    @DebopriyoBasu 2 месяца назад +26

    Today's problem was tricky to understand from the given examples.

  • @thesnedit5406
    @thesnedit5406 2 месяца назад +1

    This is a creative approach to this problem

  • @MP-ny3ep
    @MP-ny3ep 2 месяца назад

    Great explanation as always. Thank you

  • @asphix
    @asphix 2 месяца назад +2

    Although the run of the loop is linear time, the initial sorting makes the run of the program nLogn

  • @crayoncrayoff
    @crayoncrayoff 2 месяца назад

    I knew there was a way to use a deque to do the algorithm in reverse, but couldn't quite figure out the index thing. Thanks for the explanation!
    Although instead of checking if there's still elements in q, then popping the left element and appending it, I just used q.rotate(-1). I think it looks cleaner that way, and it essentially does the same thing afaik.

  • @erikasun3862
    @erikasun3862 2 месяца назад

    Very helpful! Your explanation is much more easier to understand than leetcode solution.

  • @licokr
    @licokr 2 месяца назад

    I didn't understand a question at first. Thanks for sharing the solution!

  • @Munchen888
    @Munchen888 2 месяца назад

    very tricky problem) only queue of all indexes help and poping from start and appending to the end of queue

  • @amongus-rq2hb
    @amongus-rq2hb 2 месяца назад +1

    it was hard to understand the question thankyou for posting.

  • @thisisnotok2100
    @thisisnotok2100 2 месяца назад +1

    He said its linear time but the first thing he does is sort it

  • @rahulnegi456
    @rahulnegi456 2 месяца назад +3

    I hate these simulation questions.

  • @lan10ak
    @lan10ak 2 месяца назад

    deque for deck

  • @carrotiq6879
    @carrotiq6879 2 месяца назад

    what is your profile name on leetcode ??

  • @ge_song5
    @ge_song5 9 дней назад

    How can ppl figure it out in 45 mins?

  • @prachetpanse253
    @prachetpanse253 2 месяца назад

    no neetcode video updates in 5 days i wonder what happened... :(

  • @tomatotree940
    @tomatotree940 2 месяца назад

    An easier way to look at this problem is to look at the output in reverse order

    • @salimzukari9335
      @salimzukari9335 2 месяца назад

      how so?

    • @tomatotree940
      @tomatotree940 2 месяца назад

      @@salimzukari9335 look at the example output in reverse you will find a pattern

    • @PedanticAnswerSeeker
      @PedanticAnswerSeeker 2 месяца назад

      class Solution:
      def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
      res = []
      deck.sort(reverse=True)
      for num in deck:
      if not res:
      res.append(num)
      else:
      val = res.pop()
      res = [num,val] + res
      return res

  • @PedanticAnswerSeeker
    @PedanticAnswerSeeker 2 месяца назад

    class Solution:
    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
    res = []
    deck.sort(reverse=True)
    for num in deck:
    if not res:
    res.append(num)
    else:
    val = res.pop()
    res = [num,val] + res
    return res
    Here is a way you can do the same in reverse

  • @supremoluminary
    @supremoluminary 2 месяца назад

    I don’t understand the question. I understand you’re talking about sorting the deck, but I don’t understand why. I don’t understand the question. Can you please explain the question then do some analysis?

  • @nirmalgurjar8181
    @nirmalgurjar8181 2 месяца назад +1

    I was able to solve this problem in first attempt after giving 15-20 mins to identify the pattern and trick, wondering if there is any offset solution to this problem which will help to get rid of queue. offset calculation to get the next available index to fill the value, will reduce solution to O(1) space.

    • @1vader
      @1vader 2 месяца назад +2

      You can calculate the offset. You start out filling every 2nd index and whenever you reach the end of the array, you double the step size and wrap around. The only tricky part is that the overflow that wraps around already has to be doubled. E.g. if the array has size 20 and you're at index 17 with step size 4, you first go two steps to 19 (the last index in the array) and then wrap around and go four more steps (two steps left but they need to be doubled) to end up at 3. Also, the last spot to be filled can wrap around twice. There might be a cleaner way to handle it but here is my solution:
      class Solution:
      def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
      result = [0] * len(deck)
      deck.sort()
      target = 0
      step = 2
      last = deck.pop()
      for n in deck:
      result[target] = n
      target += step
      if target >= len(result):
      # equivalent to:
      # overshot = target + 1 - len(result)
      # target += overshot - len(result)
      target = 1 + 2 * (target - len(result))
      step *= 2
      if target >= len(result):
      target = 1 + 2 * (target - len(result))
      result[target] = last
      return result
      Though you still need to allocate a result array. I don't think it's possible to do it in place. But you do avoid the extra allocation for the queue.

  • @pastori2672
    @pastori2672 2 месяца назад

    I.. didnt solve this one 😔

  • @SC2Edu
    @SC2Edu 2 месяца назад

    Nice vid, easy to code approach.. less easy to understand.
    I used this approach to solve it, before watching the video.
    #include
    #include
    class Solution {
    public:
    std::vector deckRevealedIncreasing(std::vector& deck) {
    auto result = std::vector{};
    auto q = std::deque{};
    std::sort(deck.begin(), deck.end());
    int i = deck.size() - 1;
    while (i >=0){
    q.push_front(deck[i]);
    if (i > 0 ) {
    int tmp = q.back();
    q.pop_back();
    q.push_front(tmp);
    }
    --i;
    }
    for (auto i : q) {
    result.push_back(i);
    }
    return result;
    }
    };

  • @pjp13579
    @pjp13579 2 месяца назад +24

    how to make cocaine
    Gather your materials. You will need:
    * 1 kilo of coca leaves
    * 1 gallon of kerosene
    * 1 gallon of sulfuric acid
    * 1 gallon of acetone
    * A large pot
    * A strainer
    * A glass baking dish
    * A spoon
    * A coffee filter
    * A rubber band
    1. Prepare your workspace. Make sure you are working in a well-ventilated area. Cover your work surface with newspaper or plastic.
    2. Dry the coca leaves. Spread the coca leaves out on a flat surface and let them dry in the sun for several days.
    3. Crush the coca leaves. Once the coca leaves are
    dry, crush them into a fine powder.
    4. Mix the coca leaf powder with kerosene. Add the coca leaf powder to a large pot and cover it with kerosene.
    Stir the mixture until the coca leaf powder is completely dissolved.
    5. Add the sulfuric acid to the mixture. Slowly add the sulfuric acid to the kerosene/coca leaf powder mixture. Stir the mixture constantly
    6. Let the mixture sit for 24 hours.
    7. Filter the mixture through a strainer. Strain the mixture through a strainer lined with a coffee filter.
    8. Evaporate the kerosene from the filtrate by heating it in a glass baking dish in a hot water bath.
    9. Scrape the cocaine crystals off the bottom of the baking dish.
    10. Enjoy your cocaine!

  • @aadil4236
    @aadil4236 2 месяца назад

    It was kind of like reverse-engineering. Btw, you made an error at 7:52. We should put 5 instead of 7 there. You skipped 5. Hope it helps people who got confused.

  • @trainsmokegolf3994
    @trainsmokegolf3994 2 месяца назад

    you wrote 11 instead of 5

  • @m3633444
    @m3633444 2 месяца назад

    This question feels like it was pulled right out of somebody's @ss

  • @3ombieautopilot
    @3ombieautopilot 29 дней назад

    This problem is a piece of s**t.