Skip to content

Commit

Permalink
POTD 11-Sep-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
UdaySharmaGitHub committed Sep 11, 2024
1 parent ed2a420 commit 1fde7b3
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
2220. Minimum Bit Flips to Convert Number
A bit flip of a number x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0.
For example, for x = 7, the binary representation is 111 and we may choose any bit (including any eading zeros not shown) and flip it. We can flip the first bit from the right to get 110, flip the second bit from the right to get 101, flip the fifth bit from the right (a leading zero) to get 10111, etc.
Given two integers start and goal, return the minimum number of bit flips to convert start to goal.
Example 1:
Input: start = 10, goal = 7
Output: 3
Explanation: The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
- Flip the first bit from the right: 1010 -> 1011.
- Flip the third bit from the right: 1011 -> 1111.
- Flip the fourth bit from the right: 1111 -> 0111.
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
Example 2:
Input: start = 3, goal = 4
Output: 3
Explanation: The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
- Flip the first bit from the right: 011 -> 010.
- Flip the second bit from the right: 010 -> 000.
- Flip the third bit from the right: 000 -> 100.
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
Constraints:
0 <= start, goal <= 109
*/
class Solution {
public:
int minBitFlips(int start, int goal) {
int count = 0 ;
while(start || goal){
int ld1 = start&1 , ld2 = goal&1;
count+= (ld1 != ld2);
start>>=1;
goal>>=1;
}
return count ;
}
};
47 changes: 47 additions & 0 deletions 14_Queue/Problems/Easy/Minimum_Cost_of_ropes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Minimum Cost of ropes
Given an array arr containing the lengths of the different ropes, we need to connect these ropes to form one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.
Examples:
Input: arr[] = [4, 3, 2, 6]
Output: 29
Explanation: We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Which makes the array [4, 5, 6]. Cost of this operation 2 + 3 = 5.
2) Now connect ropes of lengths 4 and 5. Which makes the array [9, 6]. Cost of this operation 4 + 5 = 9.
3) Finally connect the two ropes and all ropes have connected. Cost of this operation 9 + 6 =15
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes.
Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three rope of 3, 2 and 10), then connect 10 and 3 (we gettwo rope of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38.
Input: arr[] = [4, 2, 7, 6, 9]
Output: 62
Explanation: First, connect ropes 4 and 2, which makes the array [6, 7, 6, 9]. Cost of this operation 4 + 2 = 6.
Next, add ropes 6 and 6, which results in [12, 7, 9]. Cost of this operation 6 + 6 = 12.
Then, add 7 and 9, which makes the array [12,16]. Cost of this operation 7 + 9 = 16. And
finally, add these two which gives [28]. Hence, the total cost is 6 + 12 + 16 + 28 = 62.
Expected Time Complexity: O(n logn)
Expected Auxilliary Space: O(n)
Constraints:
1 ≤ arr.size() ≤ 205
1 ≤ arr[i] ≤ 106
*/
class Solution {
public:
// Function to return the minimum cost of connecting the ropes.
long long minCost(vector<long long>& arr) {
// Your code here
long long n= arr.size();
// Your code here
priority_queue<long long,vector<long long>,greater<long long>> pq= {arr.begin(),arr.end()};
long long ans=0;

for(int i=0;i<n-1;i++) {
int x= pq.top();
pq.pop();
int y= pq.top();
pq.pop();

pq.push(x+y);
ans+= (x+y);
}
return ans;
}
};
103 changes: 103 additions & 0 deletions 15_Tree/Problems/Medium/Flatten_BST_to_sorted_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Flatten BST to sorted list
You are given a Binary Search Tree (BST) with n nodes, each node has a distinct value assigned to it. The goal is to flatten the tree such that, the left child of each element points to nothing (NULL), and the right child points to the next element in the sorted list of elements of the BST (look at the examples for clarity). You must accomplish this without using any extra storage, except for recursive calls, which are allowed.
Note: If your BST does have a left child, then the system will print a -1 and will skip it, resulting in an incorrect solution.
Example 1:
Input:
5
/ \
3 7
/ \ / \
2 4 6 8
Output: 2 3 4 5 6 7 8
Explanation:
After flattening, the tree looks
like this
2
\
3
\
4
\
5
\
6
\
7
\
8
Here, left of each node points
to NULL and right contains the
next node.
Example 2:
Input:
5
\
8
/ \
7 9
Output: 5 7 8 9
Explanation:
After flattening, the tree looks like this:
5
\
7
\
8
\
9
Your Task:
You don't need to read input or print anything. Your task is to complete the function flattenBST() which takes root node of the BST as input parameter and returns the root node after transforming the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= Number of nodes <= 103
1 <= Data of a node <= 105
*/
//User function Template for C++
/* Node of the binary search tree
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
*/

class Solution
{
public:
vector<Node*> dfs(Node* root){
vector<Node*> ans;
if (root == NULL) return ans;
vector<Node*> left = dfs(root->left);
ans.insert(ans.end(), left.begin(), left.end());
ans.push_back(root);
vector<Node*> right = dfs(root->right);
ans.insert(ans.end(), right.begin(), right.end());
return ans;
}
Node *flattenBST(Node *root)
{
// code here
vector<Node*> inorder = dfs(root);
root= inorder[0];
Node* temp = root;
for(int i=1;i<inorder.size();i++){
temp->right = inorder[i];
temp->left = nullptr;
temp =inorder[i];
}
temp->left = nullptr;temp->right = nullptr;

return root;
}
};
47 changes: 47 additions & 0 deletions GFG/Easy Problems/Minimum_Cost_of_ropes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Minimum Cost of ropes
Given an array arr containing the lengths of the different ropes, we need to connect these ropes to form one rope. The cost to connect two ropes is equal to sum of their lengths. The task is to connect the ropes with minimum cost.
Examples:
Input: arr[] = [4, 3, 2, 6]
Output: 29
Explanation: We can connect the ropes in following ways.
1) First connect ropes of lengths 2 and 3. Which makes the array [4, 5, 6]. Cost of this operation 2 + 3 = 5.
2) Now connect ropes of lengths 4 and 5. Which makes the array [9, 6]. Cost of this operation 4 + 5 = 9.
3) Finally connect the two ropes and all ropes have connected. Cost of this operation 9 + 6 =15
Total cost for connecting all ropes is 5 + 9 + 15 = 29. This is the optimized cost for connecting ropes.
Other ways of connecting ropes would always have same or more cost. For example, if we connect 4 and 6 first (we get three rope of 3, 2 and 10), then connect 10 and 3 (we gettwo rope of 13 and 2). Finally we connect 13 and 2. Total cost in this way is 10 + 13 + 15 = 38.
Input: arr[] = [4, 2, 7, 6, 9]
Output: 62
Explanation: First, connect ropes 4 and 2, which makes the array [6, 7, 6, 9]. Cost of this operation 4 + 2 = 6.
Next, add ropes 6 and 6, which results in [12, 7, 9]. Cost of this operation 6 + 6 = 12.
Then, add 7 and 9, which makes the array [12,16]. Cost of this operation 7 + 9 = 16. And
finally, add these two which gives [28]. Hence, the total cost is 6 + 12 + 16 + 28 = 62.
Expected Time Complexity: O(n logn)
Expected Auxilliary Space: O(n)
Constraints:
1 ≤ arr.size() ≤ 205
1 ≤ arr[i] ≤ 106
*/
class Solution {
public:
// Function to return the minimum cost of connecting the ropes.
long long minCost(vector<long long>& arr) {
// Your code here
long long n= arr.size();
// Your code here
priority_queue<long long,vector<long long>,greater<long long>> pq= {arr.begin(),arr.end()};
long long ans=0;

for(int i=0;i<n-1;i++) {
int x= pq.top();
pq.pop();
int y= pq.top();
pq.pop();

pq.push(x+y);
ans+= (x+y);
}
return ans;
}
};
103 changes: 103 additions & 0 deletions GFG/Medium Problems/Flatten_BST_to_sorted_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Flatten BST to sorted list
You are given a Binary Search Tree (BST) with n nodes, each node has a distinct value assigned to it. The goal is to flatten the tree such that, the left child of each element points to nothing (NULL), and the right child points to the next element in the sorted list of elements of the BST (look at the examples for clarity). You must accomplish this without using any extra storage, except for recursive calls, which are allowed.
Note: If your BST does have a left child, then the system will print a -1 and will skip it, resulting in an incorrect solution.
Example 1:
Input:
5
/ \
3 7
/ \ / \
2 4 6 8
Output: 2 3 4 5 6 7 8
Explanation:
After flattening, the tree looks
like this
2
\
3
\
4
\
5
\
6
\
7
\
8
Here, left of each node points
to NULL and right contains the
next node.
Example 2:
Input:
5
\
8
/ \
7 9
Output: 5 7 8 9
Explanation:
After flattening, the tree looks like this:
5
\
7
\
8
\
9
Your Task:
You don't need to read input or print anything. Your task is to complete the function flattenBST() which takes root node of the BST as input parameter and returns the root node after transforming the tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= Number of nodes <= 103
1 <= Data of a node <= 105
*/
//User function Template for C++
/* Node of the binary search tree
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
*/

class Solution
{
public:
vector<Node*> dfs(Node* root){
vector<Node*> ans;
if (root == NULL) return ans;
vector<Node*> left = dfs(root->left);
ans.insert(ans.end(), left.begin(), left.end());
ans.push_back(root);
vector<Node*> right = dfs(root->right);
ans.insert(ans.end(), right.begin(), right.end());
return ans;
}
Node *flattenBST(Node *root)
{
// code here
vector<Node*> inorder = dfs(root);
root= inorder[0];
Node* temp = root;
for(int i=1;i<inorder.size();i++){
temp->right = inorder[i];
temp->left = nullptr;
temp =inorder[i];
}
temp->left = nullptr;temp->right = nullptr;

return root;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
2220. Minimum Bit Flips to Convert Number
A bit flip of a number x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0.
For example, for x = 7, the binary representation is 111 and we may choose any bit (including any eading zeros not shown) and flip it. We can flip the first bit from the right to get 110, flip the second bit from the right to get 101, flip the fifth bit from the right (a leading zero) to get 10111, etc.
Given two integers start and goal, return the minimum number of bit flips to convert start to goal.
Example 1:
Input: start = 10, goal = 7
Output: 3
Explanation: The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
- Flip the first bit from the right: 1010 -> 1011.
- Flip the third bit from the right: 1011 -> 1111.
- Flip the fourth bit from the right: 1111 -> 0111.
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
Example 2:
Input: start = 3, goal = 4
Output: 3
Explanation: The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
- Flip the first bit from the right: 011 -> 010.
- Flip the second bit from the right: 010 -> 000.
- Flip the third bit from the right: 000 -> 100.
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
Constraints:
0 <= start, goal <= 109
*/
class Solution {
public:
int minBitFlips(int start, int goal) {
int count = 0 ;
while(start || goal){
int ld1 = start&1 , ld2 = goal&1;
count+= (ld1 != ld2);
start>>=1;
goal>>=1;
}
return count ;
}
};

0 comments on commit 1fde7b3

Please sign in to comment.