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.
- Loading branch information
Showing
9 changed files
with
326 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs ( 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,32 @@ | ||
// Author : Ibrahim Khalid | ||
class Solution { | ||
public: | ||
ListNode* swapPairs(ListNode* head) { | ||
// to store values | ||
vector<int>v; | ||
|
||
ListNode* temp=head; | ||
// move between values in linkedlist and store it in vector | ||
while(temp!=NULL){ | ||
v.push_back(temp->val); | ||
temp=temp->next; | ||
} | ||
int n=v.size(); | ||
// swap every two adjacent elements in vector | ||
for(int i=0;i<n;i+=2){ | ||
if(i+1<n){ | ||
swap(v[i],v[i+1]); | ||
} | ||
} | ||
// reorder linkedlist by modified vector | ||
ListNode* tmp=head; | ||
int in=0; | ||
while(tmp!=NULL){ | ||
tmp->val=v[in]; | ||
tmp=tmp->next; | ||
in++; | ||
} | ||
|
||
return head; | ||
} | ||
}; |
62 changes: 62 additions & 0 deletions
62
05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Omar Sanad).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,62 @@ | ||
// author : Omar Sanad | ||
|
||
/* | ||
i j | ||
[1]->[2]->[3]->[4]->[5]->[6] | ||
Suppose we have this list | ||
we have to swap i and j ... | ||
how to do so? | ||
j->next = i | ||
i->next = the first node of the remaining of the list after doing the swaps | ||
j i | ||
[2]->[1]->[the first node of the remaining of the list after doing the swaps] | ||
in this step I will return j | ||
*/ | ||
class Solution { | ||
public: | ||
|
||
// recursion function | ||
ListNode* rec(ListNode* i) { | ||
|
||
// if this node is null or if it's the last node in the list, | ||
// then we cannot do any swaps, so we return | ||
if (i == NULL or i->next == NULL) | ||
return i; | ||
|
||
// we declare two nodes | ||
// the node adjacent to i, (Which we will swap it with i) | ||
ListNode *j = i->next; | ||
// the first node of the remaining of the list | ||
ListNode *to_go = j->next; | ||
|
||
// swap j and i | ||
j->next = i; | ||
|
||
// mark the next to i as the first node of the remaining of the list after doing the swaps | ||
i->next = rec(to_go); | ||
|
||
// return the node j, as now its directly before i | ||
return j; | ||
} | ||
ListNode* swapPairs(ListNode* head) { | ||
return rec(head); | ||
} | ||
}; | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ |
39 changes: 39 additions & 0 deletions
39
05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (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,39 @@ | ||
// Author: Osama Ayman | ||
// Time: O(n) | ||
// Space: O(1) | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode swapPairs(ListNode head) { | ||
// if there the list is empty of if there is only 1 node, return it | ||
if(head==null || head.next == null) return head; | ||
ListNode pp = null, prev = head, cur = head.next, next = null; | ||
ListNode newHead = cur; | ||
while(cur!=null){ | ||
// pp is the last node from prev iteration | ||
if(pp!=null) pp.next = cur; | ||
// save the next, so we don't lose reference to it | ||
next = cur.next; | ||
// swap | ||
cur.next = prev; | ||
prev.next = next; | ||
// advance the pp | ||
pp = prev; | ||
// advance the prev | ||
prev = next; | ||
// if there is no next node or only 1 node is left, break | ||
if(next == null || next.next == null) break; | ||
// advance the cur | ||
cur = next.next; | ||
} | ||
return newHead; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...um Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List ( 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,25 @@ | ||
// Author : Ibrahim Khalid | ||
class Solution { | ||
public: | ||
int pairSum(ListNode* head) { | ||
// store value | ||
vector<int>v; | ||
ListNode* temp=head; | ||
while(temp!=NULL){ | ||
v.push_back(temp->val); | ||
temp=temp->next; | ||
} | ||
int n=v.size(); | ||
// two pointers | ||
int l=0,r=n-1; | ||
// return the maximum twin sum of the linked list | ||
int mx=0; | ||
while(l<r){ | ||
// compare between twin sum and old max | ||
mx=max((v[l]+v[r]),mx); | ||
r--; | ||
l++; | ||
} | ||
return mx; | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
... Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Lama Salah).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,36 @@ | ||
// Author: Lama Salah | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int pairSum(ListNode* head) { | ||
// Create a vector to store the values of the linked list nodes. | ||
vector <int> v; | ||
|
||
// Iterate through the linked list and add each node's value to the vector. | ||
while (head){ | ||
v.emplace_back(head -> val); | ||
head = head -> next; | ||
} | ||
|
||
int l = 0, r = v.size()-1; // Initialize pointers l and r to the start and end of the vector. | ||
int ans = 0; // Initialize a variable to store the maximum twin sum. | ||
|
||
while (l < r) | ||
// Calculate the sum of current twin and update the maximum twin sum. | ||
ans = max(ans, v[l++] + v[r--]); | ||
|
||
// Return the maximum twin sum. | ||
return ans; | ||
} | ||
}; |
47 changes: 47 additions & 0 deletions
47
... Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Omar Sanad).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,47 @@ | ||
// author : Omar Sanad | ||
|
||
class Solution { | ||
public: | ||
int pairSum(ListNode* head) { | ||
// declare a stack to store the nodes in a reversed order | ||
stack < ListNode* > st; | ||
|
||
// declare a variable cur as a pointer to iterate over the list | ||
ListNode *cur = head; | ||
|
||
// iterate over the list and push the nodes in the stack | ||
while (cur){ | ||
st.push(cur); | ||
cur = cur->next; | ||
} | ||
|
||
// declare a variable to store the answer and initialize it to 0 | ||
int maxSUM = 0; | ||
|
||
// declare a variable node as a pointer to iterate over the list | ||
ListNode *node = head; | ||
|
||
// iterate over the list from the front using the variable node, | ||
// and from the back using the stack | ||
// and maximizing the answer | ||
while (st.size()) { | ||
maxSUM = max(maxSUM, node->val + st.top()->val); | ||
st.pop(); | ||
node = node->next; | ||
} | ||
|
||
// return the answer | ||
return maxSUM; | ||
} | ||
}; | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ |
30 changes: 30 additions & 0 deletions
30
...aximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (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,30 @@ | ||
// Author: Osama Ayman | ||
// Time: O(n) | ||
// Space: O(n) | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public int pairSum(ListNode head) { | ||
// convert to array list, so we can use the 2-pointers technique | ||
List<Integer> ls = new ArrayList<>(); | ||
ListNode cur = head; | ||
while(cur!=null){ | ||
ls.add(cur.val); | ||
cur = cur.next; | ||
} | ||
// 2-ptrs | ||
int lo=0, hi=ls.size()-1, max = 0; | ||
while(lo<hi){ | ||
max = Math.max(max, ls.get(lo++) + ls.get(hi--)); | ||
} | ||
return max; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...s to Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (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: | ||
vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) { | ||
// Create two arrays to store the count of outgoing and incoming edges for each vertex | ||
vector<int> from(n), to(n); | ||
|
||
// Count the number of outgoing and incoming edges for each vertex | ||
for (auto& vec : edges) { | ||
from[vec[0]]++; // Increment the outgoing edge count for the source vertex | ||
to[vec[1]]++; // Increment the incoming edge count for the destination vertex | ||
} | ||
|
||
// Create a vector to store the result | ||
vector < int > res; | ||
|
||
// Iterate over all vertices | ||
for (int i = 0; i < n; i++) { | ||
// Check if the vertex has outgoing edges but no incoming edges | ||
if (from[i] && !to[i]) | ||
res.push_back(i); // Add the vertex to the result | ||
} | ||
|
||
// Return the result vector | ||
return res; | ||
} | ||
|
||
}; |
26 changes: 26 additions & 0 deletions
26
...Reach All Nodes/18- Minimum Number of Vertices to Reach All Nodes (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,26 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
vector<int> findSmallestSetOfVertices(int n, vector<vector<int>>& edges) { | ||
// to find the minimum number of vertices to reach all nodes we need to find the nodes with zero in degree | ||
// because we can reach all nodes from the nodes with zero in degree | ||
// we can do that by counting the in degree of each node | ||
// then loop over the nodes and add the nodes with zero in degree to the answer | ||
|
||
// in_degree: in_degree[i] -> the in degree of node i | ||
// ans: the answer | ||
vector<int> in_degree(n), ans; | ||
|
||
// count the in degree of each node | ||
for(auto&i: edges) in_degree[i[1]]++; | ||
|
||
// loop over the nodes and add the nodes with zero in degree to the answer | ||
for(int i = 0; i < n; i++){ | ||
if(!in_degree[i]) ans.emplace_back(i); | ||
} | ||
|
||
// return the answer | ||
return ans; | ||
} | ||
}; |