Smallest Number in Infinite Set
Автор: Deepti Talesra
Загружено: 2025-02-09
Просмотров: 2012
Описание:
Explaining Smallest Number in Infinite Set from leetcode in Python!
ahhhhh I accidentally deleted the part in which I do a walkthrough of the code with an example (like we do in every video!) so i'm writing it below in case you are interested:)
Input example:
["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest"]
[[], [4], [], [], [1], [], []]
Outputs after each operation:
1. Initialize for the call to SmallestInfiniteSet
self.smallest = 1
self.removed = set()
Now we call: 2. addBack with 4
However going in the first if condition we see the condition is false - 4 is not in self.removed as it is currently empty. So we don't go in the if and since there are no more lines of code in the function we exit.
Current state: self.smallest = 1; self.removed = set()
3. popSmallest()
Current state: self.smallest = 1; self.removed = set()
Going in the function, we set ret to be 1 (line 9). Then we add 1 to the self.removed set (line 10). self.removed = set(1) Increase self.smallest to be 2 and go in the while loop (line 11). While self.smallest (2) in self.removed, that is false 2 is not in there, so we exit and return our ret variable of 1.
Current state: self.smallest = 2; self.removed = set(1)
4. popSmallest()
Going in the function, we set ret to be self.smallest so ret = 2 (line 9). Then we add 2 to the self.removed set (line 10). self.removed = set(1,2) Increase self.smallest by one - to be 3 and go in the while loop (line 11). While self.smallest (3) in self.removed, that is false 3 is not in there, so we exit and return our ret variable of 2.
Current state: self.smallest = 3; self.removed = set(1,2)
5. addBack with 1
If num (1) in self.removed: (this is true), so we go into the if condition and remove 1 from the set of self.removed. *self.removed = set(2)*. We check if 1 is less than self.smallest (3) this is true, so we set self.smallest to be 1.
Current state: self.smallest = 1; self.removed = set(2)
6. popSmallest()
Going in the function, we set ret = 1 (line 9). Then we add 1 to the self.removed set (line 10). self.removed = set(1,2) Increase self.smallest to be 2 and go in the while loop (line 11). While self.smallest (2) in self.removed, that is true 2 is in there, so we increase self.smallest to be 3 and go back into the while loop. *self.smallest = 3*. We check: While self.smallest (3) in self.removed, that is not true 3 is not in there, so we exit and return our ret variable of 1.
Current state: self.smallest = 3; self.removed = set(1,2)
And that's it!! Lemme know if you have any questions:))))
LeetCode 2336
Code: https://github.com/deepti-talesra/Lee...
@0:53 - Example + Explanation
@3:33 - Code
@6:01 - Space and Time Complexity
Music: Bensound
Lemme know if you have any comments or questions!:)))
Socials:
/ deeptitalesra
/ deeptitalesra
Playlists:
• Graph
• Binary
• Matrix
• Linked List
• Dynamic Programming
• String
• Tree
• Array's
• BLIND 75 - Interview Questions
• LeetCode Coding Interview Questions - Python
• Top Interview 150
If you found this helpful, check out my channel for even **MORE VIDEOS**!!:)) https://www.youtube.com/c/DEEPTITALES...
Give it a shot yourself - https://leetcode.com/problems/smalles...
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: