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
Making sense of question from all that deck crap is more difficult here than the solution
7:53-7:58 correction: [4 -> 5, 6 -> 7] index -> value in array. Nonetheless, thank you for the stellar explanation.
Today's problem was tricky to understand from the given examples.
This is a creative approach to this problem
Great explanation as always. Thank you
Although the run of the loop is linear time, the initial sorting makes the run of the program nLogn
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.
Very helpful! Your explanation is much more easier to understand than leetcode solution.
I didn't understand a question at first. Thanks for sharing the solution!
very tricky problem) only queue of all indexes help and poping from start and appending to the end of queue
it was hard to understand the question thankyou for posting.
He said its linear time but the first thing he does is sort it
I hate these simulation questions.
deque for deck
what is your profile name on leetcode ??
How can ppl figure it out in 45 mins?
no neetcode video updates in 5 days i wonder what happened... :(
An easier way to look at this problem is to look at the output in reverse order
how so?
@@salimzukari9335 look at the example output in reverse you will find a pattern
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
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
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?
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.
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.
I.. didnt solve this one 😔
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;
}
};
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!
where do i find the spoon
Is this a leetcode hard
That looks easier than this question
i did this question 4 years back
Thanks, that helps a lot.
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.
you wrote 11 instead of 5
This question feels like it was pulled right out of somebody's @ss
This problem is a piece of s**t.