From 81c413b6f014bb896469f279265b80d54bbafef8 Mon Sep 17 00:00:00 2001 From: aboelsooud Date: Mon, 15 May 2023 02:14:58 +0300 Subject: [PATCH 01/11] add the 14'th day problem solution --- ...After N Operations (Mahmoud Aboelsoud).cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 05- May/14- Maximize Score After N Operations/14- Maximize Score After N Operations (Mahmoud Aboelsoud).cpp diff --git a/05- May/14- Maximize Score After N Operations/14- Maximize Score After N Operations (Mahmoud Aboelsoud).cpp b/05- May/14- Maximize Score After N Operations/14- Maximize Score After N Operations (Mahmoud Aboelsoud).cpp new file mode 100644 index 000000000..5d308860c --- /dev/null +++ b/05- May/14- Maximize Score After N Operations/14- Maximize Score After N Operations (Mahmoud Aboelsoud).cpp @@ -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> dp; + // nums: vector of numbers + vector 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& nums) { + // initialize the variables + this -> n = nums.size(); + this -> nums = nums; + + // initialize dp with -1 + dp.assign(1 << n, vector (n + 1, -1)); + + // return the maximum score + return get_max(0, n, 1); + } +}; From 18b1f2c79d1402bac1632b6385ced39fed82a535 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Mon, 15 May 2023 00:58:11 +0000 Subject: [PATCH 02/11] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/15- Swapping Nodes in a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 0986d48cb..52af9e62f 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -543,6 +543,10 @@ { "day": "14", "title": "Maximize Score After N Operations" + }, + { + "day": "15", + "title": "Swapping Nodes in a Linked List" } ] } \ No newline at end of file diff --git a/05- May/15- Swapping Nodes in a Linked List/.gitkeep b/05- May/15- Swapping Nodes in a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From f526ca8117776020c45e2ba5b92bea30abf91990 Mon Sep 17 00:00:00 2001 From: aboelsooud Date: Mon, 15 May 2023 04:13:44 +0300 Subject: [PATCH 03/11] add the 15'th day problem solution --- ...s in a Linked List (Mahmoud Aboelsoud).cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Mahmoud Aboelsoud).cpp diff --git a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Mahmoud Aboelsoud).cpp b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Mahmoud Aboelsoud).cpp new file mode 100644 index 000000000..572e839dc --- /dev/null +++ b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List (Mahmoud Aboelsoud).cpp @@ -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 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; + + } +}; From 89dab97543f290e4e221f066e3976c3333fc939d Mon Sep 17 00:00:00 2001 From: lamasalah32 Date: Mon, 15 May 2023 23:28:27 +0300 Subject: [PATCH 04/11] add 15- Swapping Nodes in a Linked List ( Lama Salah ).cpp --- ... Nodes in a Linked List ( Lama Salah ).cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Lama Salah ).cpp diff --git a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Lama Salah ).cpp b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Lama Salah ).cpp new file mode 100644 index 000000000..01428105f --- /dev/null +++ b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Lama Salah ).cpp @@ -0,0 +1,40 @@ +/** + * 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; + } +}; \ No newline at end of file From 655b3e41efaa9594d50fa70539d1c2d46cd472ed Mon Sep 17 00:00:00 2001 From: lamasalah32 Date: Mon, 15 May 2023 23:31:34 +0300 Subject: [PATCH 05/11] update 15- Swapping Nodes in a Linked List ( Lama Salah ).cpp --- .../15- Swapping Nodes in a Linked List ( Lama Salah ).cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Lama Salah ).cpp b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Lama Salah ).cpp index 01428105f..e16fe39cf 100644 --- a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Lama Salah ).cpp +++ b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Lama Salah ).cpp @@ -1,3 +1,5 @@ +// Author: Lama Salah + /** * Definition for singly-linked list. * struct ListNode { From b6b19bd8974aa3d37cadc9f740f4a0a461cc49c6 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 16 May 2023 00:00:14 +0000 Subject: [PATCH 06/11] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/16- Swap Nodes in Pairs/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 52af9e62f..2464a5b42 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -547,6 +547,10 @@ { "day": "15", "title": "Swapping Nodes in a Linked List" + }, + { + "day": "16", + "title": "Swap Nodes in Pairs" } ] } \ No newline at end of file diff --git a/05- May/16- Swap Nodes in Pairs/.gitkeep b/05- May/16- Swap Nodes in Pairs/.gitkeep new file mode 100644 index 000000000..e69de29bb From 49587a71b42c55751c2c5ab23a526db3ec4113eb Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Tue, 16 May 2023 18:12:08 +0300 Subject: [PATCH 07/11] Create 16- Swap Nodes in Pairs (Ahmed Hossam).cpp --- ...16- Swap Nodes in Pairs (Ahmed Hossam).cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Ahmed Hossam).cpp diff --git a/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Ahmed Hossam).cpp b/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Ahmed Hossam).cpp new file mode 100644 index 000000000..d2522ed4e --- /dev/null +++ b/05- May/16- Swap Nodes in Pairs/16- Swap Nodes in Pairs (Ahmed Hossam).cpp @@ -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; + } +}; From d4b0868f73f37e2a5b39e1d8e4296f3011a29aa4 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Tue, 16 May 2023 15:12:53 +0000 Subject: [PATCH 08/11] Add new daily problem to README --- 05- May/README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/05- May/README.md b/05- May/README.md index 25bc9decd..ce7c58f48 100644 --- a/05- May/README.md +++ b/05- May/README.md @@ -36,6 +36,7 @@ 1. **[Count Ways To Build Good Strings](#13--count-ways-to-build-good-strings)** 1. **[Maximize Score After N Operations](#14--maximize-score-after-n-operations)** 1. **[Swapping Nodes in a Linked List](#15--swapping-nodes-in-a-linked-list)** +1. **[Swap Nodes in Pairs](#16--swap-nodes-in-pairs)**


@@ -843,4 +844,41 @@ public: }; ``` + +
+

+ +## 16) [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Linked List` `Recursion` + +### Code + + +```cpp +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; + } +}; +``` \ No newline at end of file From f7504c4996a0d3810fd08f54c148ba98999081b2 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 17 May 2023 00:00:14 +0000 Subject: [PATCH 09/11] Add new daily problem --- .github/data/problems.json | 4 ++++ 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep | 0 2 files changed, 4 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/.gitkeep diff --git a/.github/data/problems.json b/.github/data/problems.json index 2464a5b42..ec8dc0827 100644 --- a/.github/data/problems.json +++ b/.github/data/problems.json @@ -551,6 +551,10 @@ { "day": "16", "title": "Swap Nodes in Pairs" + }, + { + "day": "17", + "title": "Maximum Twin Sum of a Linked List" } ] } \ No newline at end of file diff --git a/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep b/05- May/17- Maximum Twin Sum of a Linked List/.gitkeep new file mode 100644 index 000000000..e69de29bb From 7e3c390ae583582001363b844e247377fb297b7c Mon Sep 17 00:00:00 2001 From: Ahmed Hossam <63050133+7oSkaaa@users.noreply.github.com> Date: Wed, 17 May 2023 19:35:31 +0300 Subject: [PATCH 10/11] Create 17- Maximum Twin Sum of a Linked List (Ahmed Hossam).cpp --- ...in Sum of a Linked List (Ahmed Hossam).cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Ahmed Hossam).cpp diff --git a/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Ahmed Hossam).cpp b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Ahmed Hossam).cpp new file mode 100644 index 000000000..c38965c4f --- /dev/null +++ b/05- May/17- Maximum Twin Sum of a Linked List/17- Maximum Twin Sum of a Linked List (Ahmed Hossam).cpp @@ -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; + } + +}; From 1abf5268b15416df7294d882db9ba74a4106fbe1 Mon Sep 17 00:00:00 2001 From: 7oSkaaa Date: Wed, 17 May 2023 16:36:22 +0000 Subject: [PATCH 11/11] Add new daily problem to README --- 05- May/README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/05- May/README.md b/05- May/README.md index ce7c58f48..f7d3b3f40 100644 --- a/05- May/README.md +++ b/05- May/README.md @@ -37,6 +37,7 @@ 1. **[Maximize Score After N Operations](#14--maximize-score-after-n-operations)** 1. **[Swapping Nodes in a Linked List](#15--swapping-nodes-in-a-linked-list)** 1. **[Swap Nodes in Pairs](#16--swap-nodes-in-pairs)** +1. **[Maximum Twin Sum of a Linked List](#17--maximum-twin-sum-of-a-linked-list)**


@@ -881,4 +882,49 @@ public: } }; ``` + +
+

+ +## 17) [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) + +### Difficulty + +![](https://img.shields.io/badge/Medium-orange?style=for-the-badge) + +### Related Topic + +`Linked List` `Two Pointers` `Stack` + +### Code + + +```cpp +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; + } + +}; +``` \ No newline at end of file