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
6 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...ze Score After N Operations/14- Maximize Score After N Operations (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,56 @@ | ||
// Author: Mahmoud Aboelsoud | ||
|
||
class Solution { | ||
public: | ||
// we need to find the maximum score we can get after n operations | ||
// we can do that using dp and bitmasking | ||
// we need to find the way to rearrange the numbers to get the maximum score | ||
// we can use bitmasking to represent the numbers we have used | ||
// we can use dp[mask][prev] to be the maximum score we can get if we have used the numbers in mask and the last number we have used is prev | ||
// we can use a recursive function to get the maximum score | ||
|
||
|
||
// dp: dp[mask][prev] -> maximum score we can get if we have used the numbers in mask and the last number we have used is prev | ||
vector<vector<int>> dp; | ||
// nums: vector of numbers | ||
vector<int> nums; | ||
// n: number of elements | ||
int n; | ||
|
||
// function to get the maximum score | ||
int get_max(int mask, int prev, int idx){ | ||
// if we have used all numbers return 0 | ||
if((1 << n) - 1 == mask) return 0; | ||
|
||
// if we have already calculated the answer for this state return it | ||
if(dp[mask][prev] != -1) return dp[mask][prev]; | ||
|
||
// ans: maximum score we can get | ||
int ans = 0; | ||
|
||
// iterate over all numbers | ||
for(int i = 0; i < n; i++){ | ||
// if the number is not used | ||
if(!((1 << i) & mask)){ | ||
// calculate the maximum score, if there is no previous number we add 0 to the score and if there is a previous number we add the gcd of the two numbers multiplied by idx | ||
ans = max(ans, (prev != n ? gcd(nums[i], nums[prev]) * idx : 0) + get_max(mask | (1 << i), (prev == n ? i : n), idx + (prev != n))); | ||
} | ||
} | ||
|
||
// return the answer | ||
return dp[mask][prev] = ans; | ||
} | ||
|
||
|
||
int maxScore(vector<int>& nums) { | ||
// initialize the variables | ||
this -> n = nums.size(); | ||
this -> nums = nums; | ||
|
||
// initialize dp with -1 | ||
dp.assign(1 << n, vector<int> (n + 1, -1)); | ||
|
||
// return the maximum score | ||
return get_max(0, n, 1); | ||
} | ||
}; |
42 changes: 42 additions & 0 deletions
42
...5- Swapping Nodes in a Linked List/15- Swapping Nodes in 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,42 @@ | ||
// 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 { | ||
|
||
// TWO POINTERS SOLUTION | ||
|
||
public: | ||
ListNode* swapNodes(ListNode* head, int k) { | ||
// Initialize two pointers p1 and p2 to the head of the linked list. | ||
ListNode *p1 = head, *p2 = head, *beginning; | ||
|
||
// Move the pointer p2 k-1 nodes (This puts p2 at the kth node from the beginning of the list). | ||
for (int i = 0; i < k-1; i++) | ||
p2 = p2 -> next; | ||
|
||
// Initialize a pointer beginning to the node pointed to by p2. | ||
beginning = p2; | ||
|
||
// Move both pointers p1 and p2 simultaneously until p2 reaches the end of the linked list. | ||
// At this point, p1 will be pointing to the kth node from the end of the list. | ||
while (p2 -> next){ | ||
p1 = p1 -> next; | ||
p2 = p2 -> next; | ||
} | ||
|
||
// Swap values of kth node from beginning and kth node from end. | ||
swap(beginning -> val, p1 -> val); | ||
|
||
// Return the head of the linked list. | ||
return head; | ||
} | ||
}; |
48 changes: 48 additions & 0 deletions
48
...apping Nodes in a Linked List/15- Swapping Nodes in a Linked List (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 | ||
|
||
/** | ||
* 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: | ||
ListNode* swapNodes(ListNode* head, int k) { | ||
// we need to swap the kth node from the beginning with the kth node from the end | ||
// we can use a vector to store the values of all nodes | ||
// then swap the values of the kth node from the beginning with the kth node from the end | ||
// then update the values of the nodes with the new values | ||
|
||
// v: vector to store the values of all nodes | ||
vector<int> v; | ||
|
||
// cur: pointer to traverse the linked list | ||
ListNode* cur = head; | ||
|
||
// loop over the linked list to store the values of all nodes | ||
while(cur != NULL){ | ||
v.emplace_back(cur -> val); | ||
cur = cur -> next; | ||
} | ||
|
||
// swap the values of the kth node from the beginning with the kth node from the end | ||
swap(v[k - 1], v[v.size() - k]); | ||
|
||
cur = head; | ||
|
||
// loop over the linked list to update the values of the nodes with the new values | ||
for(auto&i: v){ | ||
cur -> val = i; | ||
cur = cur -> next; | ||
} | ||
|
||
// return the head of the linked list | ||
return head; | ||
|
||
} | ||
}; |
20 changes: 20 additions & 0 deletions
20
05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (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,20 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
ListNode* swapPairs(ListNode* head) { | ||
// If the linked list is empty or has only one node | ||
if(head == NULL) return NULL; | ||
if(head -> next == NULL) return head; | ||
|
||
// Store the next node in a variable | ||
ListNode* next = head -> next; | ||
|
||
// Recursively swap the pairs of nodes | ||
head -> next = swapPairs(next -> next); | ||
next -> next = head; | ||
|
||
// Return the new head of the swapped linked list | ||
return next; | ||
} | ||
}; |
28 changes: 28 additions & 0 deletions
28
...aximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (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,28 @@ | ||
// Author: Ahmed Hossam | ||
|
||
class Solution { | ||
public: | ||
|
||
int pairSum(ListNode* head) { | ||
// Create a vector to store the values of the linked list nodes | ||
vector < int > nums; | ||
ListNode* curr = head; | ||
|
||
// Traverse the linked list and store the values in the vector | ||
while (curr != nullptr) { | ||
nums.push_back(curr -> val); | ||
curr = curr -> next; | ||
} | ||
|
||
// Initialize left and right pointers, and the maximum sum | ||
int l = 0, r = nums.size() - 1, mx_sum = INT_MIN; | ||
|
||
// Find the maximum sum of pairs | ||
while (l < r) | ||
mx_sum = max(mx_sum, nums[l++] + nums[r--]); | ||
|
||
// Return the maximum sum | ||
return mx_sum; | ||
} | ||
|
||
}; |
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