Skip to content

Commit

Permalink
Merge branch '7oSkaaa:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 committed May 17, 2023
2 parents 4bde588 + 1abf526 commit b21a881
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 0 deletions.
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);
}
};
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;
}
};
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;

}
};
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;
}
};
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;
}

};
84 changes: 84 additions & 0 deletions 05- May/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
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)**
1. **[Maximum Twin Sum of a Linked List](#17--maximum-twin-sum-of-a-linked-list)**

<hr>
<br><br>
Expand Down Expand Up @@ -843,4 +845,86 @@ public:

};
```

<hr>
<br><br>

## 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;
}
};
```

<hr>
<br><br>

## 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;
}

};
```

0 comments on commit b21a881

Please sign in to comment.