forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '7oSkaaa:main' into main
- Loading branch information
Showing
11 changed files
with
453 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...5- Smallest Number in Infinite Set/25- Smallest Number in Infinite Set (Ahmed Hossam).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class SmallestInfiniteSet { | ||
public: | ||
// Define a set variable named Mex | ||
set < int > Mex; | ||
|
||
// Define a constant integer N with a value of 1000 | ||
static constexpr int N = 1000; | ||
|
||
// Constructor function for the SmallestInfiniteSet class | ||
SmallestInfiniteSet() { | ||
// Loop through integers from 1 to N and insert them into the set variable Mex | ||
for(int i = 1; i <= N; i++) | ||
Mex.insert(i); | ||
} | ||
|
||
// Function to remove and return the smallest integer from the set variable Mex | ||
int popSmallest() { | ||
// Store the smallest integer in the set variable Mex | ||
int mex = *Mex.begin(); | ||
|
||
// Remove the smallest integer from the set variable Mex | ||
Mex.erase(Mex.begin()); | ||
|
||
// Return the smallest integer | ||
return mex; | ||
} | ||
|
||
// Function to add an integer back to the set variable Mex | ||
void addBack(int num) { | ||
// Insert the integer into the set variable Mex | ||
Mex.insert(num); | ||
} | ||
}; |
40 changes: 40 additions & 0 deletions
40
... Smallest Number in Infinite Set/25- Smallest Number in Infinite Set (Ibrahim Khalid).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// author : ibrahim khalid | ||
class SmallestInfiniteSet { | ||
public: | ||
// create set to store number 1 from 1000 | ||
set<int>s; | ||
// create value increase by 1 | ||
int n; | ||
SmallestInfiniteSet() { | ||
// Initial value equal 1 | ||
n=1; | ||
} | ||
|
||
int popSmallest() { | ||
SmallestInfiniteSet(); | ||
// add n | ||
s.insert(n); | ||
// increae to store after pop small element | ||
n++; | ||
// x equal smallest element | ||
int x=* s.begin(); | ||
// pop | ||
s.erase(x); | ||
return x; | ||
|
||
} | ||
|
||
void addBack(int num) { | ||
// if num less than or equal to n --> store in set | ||
if(num<=n){ | ||
s.insert(num); | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Your SmallestInfiniteSet object will be instantiated and called as such: | ||
* SmallestInfiniteSet* obj = new SmallestInfiniteSet(); | ||
* int param_1 = obj->popSmallest(); | ||
* obj->addBack(num); | ||
*/ |
48 changes: 48 additions & 0 deletions
48
...allest Number in Infinite Set/25- Smallest Number in Infinite Set (Mahmoud Aboelsoud).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class SmallestInfiniteSet { | ||
public: | ||
// we need to find the smallest number in the set and remove it | ||
// we start the set with all numbers from 1 to 1000 | ||
// we can use a vector to represent the set | ||
// we can use a variable to keep track of the smallest number in the set | ||
// when we pop the smallest number we go to the next number in the set | ||
// when we add a number back to the set we check if it is smaller than the smallest number in the set | ||
// if it is we update the smallest number in the set | ||
|
||
// vis: vector to represent the set | ||
// vis[i] = 1 means that the number i is in the set | ||
// vis[i] = 0 means that the number i is not in the set | ||
vector<int> vis; | ||
// idx: the smallest number in the set | ||
int idx; | ||
SmallestInfiniteSet() { | ||
// initialize the set with all numbers from 1 to 1000 | ||
vis.assign(1001, 1); | ||
// initialize the smallest number in the set to 1 | ||
idx = 1; | ||
} | ||
|
||
int popSmallest() { | ||
// find the smallest number in the set | ||
while(!vis[idx]) idx++; | ||
// remove the smallest number from the set | ||
vis[idx] = 0; | ||
// return the smallest number | ||
return idx++; | ||
} | ||
|
||
void addBack(int num) { | ||
// add the number to the set | ||
vis[num] = 1; | ||
// update the smallest number in the set | ||
idx = min(idx, num); | ||
} | ||
}; | ||
|
||
/** | ||
* Your SmallestInfiniteSet object will be instantiated and called as such: | ||
* SmallestInfiniteSet* obj = new SmallestInfiniteSet(); | ||
* int param_1 = obj->popSmallest(); | ||
* obj->addBack(num); | ||
*/ |
32 changes: 32 additions & 0 deletions
32
... Smallest Number in Infinite Set/25- Smallest Number in Infinite Set (Noura Algohary).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//Author: Noura Algohary | ||
|
||
class SmallestInfiniteSet { | ||
public: | ||
// set is used to store UNIQUE values in ascending order by default | ||
set<int>st; | ||
SmallestInfiniteSet() { | ||
st.clear(); | ||
|
||
for(int i = 1; i<=1000;i++) | ||
st.insert(i); | ||
} | ||
|
||
int popSmallest() { | ||
// smallest element is at the beginning of the set | ||
int removed = *st.begin(); | ||
st.erase(st.begin()); | ||
|
||
return removed; | ||
} | ||
|
||
void addBack(int num) { | ||
st.insert(num); | ||
} | ||
}; | ||
|
||
/** | ||
* Your SmallestInfiniteSet object will be instantiated and called as such: | ||
* SmallestInfiniteSet* obj = new SmallestInfiniteSet(); | ||
* int param_1 = obj->popSmallest(); | ||
* obj->addBack(num); | ||
*/ |
31 changes: 31 additions & 0 deletions
31
...- Smallest Number in Infinite Set/25- Smallest Number in Infinite Set (Noura Algohary).py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Author: Noura Algohary | ||
class SmallestInfiniteSet: | ||
# use list as a set | ||
st = [] | ||
|
||
def __init__(self): | ||
self.st.clear() | ||
|
||
for i in range(1, 1001): | ||
self.st.append(i) | ||
|
||
|
||
def popSmallest(self) -> int: | ||
# sort to return the first element(smallest element) | ||
self.st.sort() | ||
|
||
removed = self.st.pop(0) | ||
|
||
return removed | ||
|
||
def addBack(self, num: int) -> None: | ||
# make sure the number is not presented in the set | ||
if num not in self.st: | ||
self.st.insert(0, num) | ||
|
||
|
||
|
||
# Your SmallestInfiniteSet object will be instantiated and called as such: | ||
# obj = SmallestInfiniteSet() | ||
# param_1 = obj.popSmallest() | ||
# obj.addBack(num) |
45 changes: 45 additions & 0 deletions
45
...l/25- Smallest Number in Infinite Set/25- Smallest Number in Infinite Set (Omar Wael).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Author : Omar Wael | ||
// leetcode account : https://leetcode.com/OmarWael1/ | ||
|
||
class SmallestInfiniteSet { | ||
public: | ||
int ans=1; | ||
priority_queue<int,vector<int>,greater<int>>pq; | ||
int vis[1005]; | ||
SmallestInfiniteSet() { | ||
// min number is 1 | ||
ans=1; | ||
// visit all numbers | ||
for(int i=1;i<=1004;i++){ | ||
vis[i]=1; | ||
} | ||
} | ||
int popSmallest() { | ||
// if there is number less than the ans | ||
// then unvisit it and return it | ||
if(!pq.empty() && pq.top()<ans){ | ||
int cur=pq.top(); | ||
pq.pop(); | ||
vis[cur]=0; | ||
return cur; | ||
} | ||
// otherwise unvisit the answer and return it | ||
vis[ans]=0; | ||
ans++; | ||
return ans-1; | ||
} | ||
void addBack(int num) { | ||
// if current number is unvisited push it | ||
if(!vis[num]){ | ||
pq.push(num); | ||
vis[num]=1; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Your SmallestInfiniteSet object will be instantiated and called as such: | ||
* SmallestInfiniteSet* obj = new SmallestInfiniteSet(); | ||
* int param_1 = obj->popSmallest(); | ||
* obj->addBack(num); | ||
*/ |
47 changes: 47 additions & 0 deletions
47
...5- Smallest Number in Infinite Set/25- Smallest Number in Infinite Set (Osama Ayman).java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Author: Osama Ayman | ||
// Time: O((m+n) log(n)) | ||
// Space: O(n) | ||
class SmallestInfiniteSet { | ||
// use a priority queue to keep track of the added integers in increasing order | ||
// we will only add numbers that are less than the current number | ||
PriorityQueue<Integer> addedIntegers; | ||
// set to tell if a number is already in the pq, so we don't add it again to the pq | ||
Set<Integer> set; | ||
// represents the lowest number in the infinte set | ||
int cur; | ||
public SmallestInfiniteSet() { | ||
addedIntegers = new PriorityQueue<>(); | ||
set = new HashSet<>(); | ||
// initially set to 1 | ||
cur = 1; | ||
} | ||
|
||
public int popSmallest() { | ||
// if pq is empty then the lowest number is the cur | ||
if(addedIntegers.isEmpty()){ | ||
// return it, then increment it by 1 | ||
return cur++; | ||
} | ||
// else if pq not empty, then the top number in it is lower than the cur | ||
int pop = addedIntegers.poll(); | ||
// remove it from set as we removed it from pq | ||
set.remove(pop); | ||
return pop; | ||
} | ||
|
||
public void addBack(int num) { | ||
// if the pq already contains the num or the num is greater than or equal to | ||
// cur, dont add it | ||
if(set.contains(num) || cur <= num) return; | ||
// else add it | ||
set.add(num); | ||
addedIntegers.add(num); | ||
} | ||
} | ||
|
||
/** | ||
* Your SmallestInfiniteSet object will be instantiated and called as such: | ||
* SmallestInfiniteSet obj = new SmallestInfiniteSet(); | ||
* int param_1 = obj.popSmallest(); | ||
* obj.addBack(num); | ||
*/ |
29 changes: 29 additions & 0 deletions
29
04- April/26- Add Digits/26- Add Digits (Ahmed Hossam).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
|
||
// A function to find the sum of digits of a given number | ||
int sumDigits(int num){ | ||
// Initialize the sum variable to zero | ||
int sum = 0; | ||
|
||
// Convert the number to a string and iterate over each character | ||
for(auto& c : to_string(num)) | ||
// Subtract the ASCII value of '0' from the character and add it to the sum | ||
sum += c - '0'; | ||
|
||
// Return the sum of digits | ||
return sum; | ||
} | ||
|
||
int addDigits(int num) { | ||
// Repeat the process until the number is a single digit | ||
while(num > 9) | ||
// Sum the digits of the number | ||
num = sumDigits(num); | ||
|
||
// Return the resulting single digit | ||
return num; | ||
} | ||
}; |
29 changes: 29 additions & 0 deletions
29
04- April/26- Add Digits/26- Add Digits (Mahmoud Aboelsoud).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
// in this problem we have to add the digits of a number until we get a single digit | ||
// we can do this by using a while loop and a nested while loop | ||
// the outer loop will run until the number is less than 9 | ||
// the inner loop will run until the number is 0 to add the digits | ||
|
||
int addDigits(int num) { | ||
// loop until the number is less than or equal 9 (single digit) | ||
while(num > 9){ | ||
// x: the sum of the digits | ||
int x = 0; | ||
// loop until the number is 0 to add the digits | ||
while(num){ | ||
// add the last digit to x | ||
x += num % 10; | ||
// remove the last digit from the number | ||
num /= 10; | ||
} | ||
// make the number equal to x (the sum of the digits) | ||
num = x; | ||
} | ||
|
||
// return the number | ||
return num; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
04- April/26- Add Digits/26- Add Digits (Osama Ayman).java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Author: Osama Ayman | ||
class Solution { | ||
public int addDigits(int num) { | ||
// if it is a single digit return it | ||
if(num <= 9) return num; | ||
// else calculate the sum of digits | ||
int res = 0; | ||
while(num > 0){ | ||
// get the last digit of num, add it to res | ||
res += num%10; | ||
// remove last digit of num | ||
num /= 10; | ||
} | ||
// do it recursively | ||
return addDigits(res); | ||
} | ||
} |
Oops, something went wrong.