Professional Documents
Culture Documents
Description: Given an array nums with n objects colored red, white, or blue, sort them in-
place so that objects of the same color are adjacent, with the colors in the order red, white,
and blue.
Solution Code:
void sortColors(vector<int>& nums) {
int low = 0, mid = 0, high = nums.size() - 1;
while (mid <= high) {
if (nums[mid] == 0) {
swap(nums[low++], nums[mid++]);
} else if (nums[mid] == 1) {
++mid;
} else {
swap(nums[mid], nums[high--]);
}
}
}
Problem: Wiggle Sort II
Description: Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2]
< nums[3]....
Solution Code:
void wiggleSort(vector<int>& nums) {
vector<int> sorted(nums);
sort(sorted.begin(), sorted.end());
int n = nums.size(), j = n - 1;
for (int i = 1; i < n; i += 2)
nums[i] = sorted[j--];
for (int i = 0; i < n; i += 2)
nums[i] = sorted[j--];
}
Description: Given a sorted array of integers and a target value, implement binary search to
determine if the target is present in the array. If it is present, return the index of the target,
otherwise return -1.
Solution Code:
int binarySearch(vector<int>& nums, int target) {
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
struct Job {
char id;
int deadline;
int profit;
};
int main() {
Job arr[] = {{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27},
{'d', 1, 25}, {'e', 3, 15}};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum profit sequence of jobs: ";
printJobScheduling(arr, n);
return 0;
}
struct Job {
char id;
int deadline;
int profit;
};
int main() {
Job arr[] = {{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27},
{'d', 1, 25}, {'e', 3, 15}};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum profit sequence of jobs: ";
printJobScheduling(arr, n);
return 0;
}
Description: Given a set of jobs each with a deadline, profit, and penalty for missing the
deadline, select the jobs to maximize profit while meeting all deadlines or minimizing
penalties.
Solution Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Job {
int id, deadline, profit, penalty;
};
int main() {
Job arr[] = {{1, 2, 100, 5}, {2, 1, 19, 2}, {3, 2, 27, 3},
{4, 1, 25, 4}, {5, 3, 15, 1}};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum profit sequence of jobs: ";
jobSequencing(arr, n);
return 0;
}
Description: Given a set of jobs each with a deadline and profit, select the jobs to minimize
the total completion time while meeting all deadlines.
Solution Code:
#include <iostream>
#include <algorithm>
using namespace std;
struct Job {
char id;
int deadline;
int profit;
};
int main() {
Job arr[] = {{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27},
{'d', 1, 25}, {'e', 3, 15}};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum profit sequence of jobs: ";
printJobScheduling(arr, n);
return 0;
}
Description: Given a set of jobs each with a start time, end time, and profit, select the jobs to
maximize profit while not overlapping in time.
Solution Code:
#include <iostream>
#include <algorithm>
using namespace std;
struct Job {
int start;
int end;
int profit;
};
int main() {
Job arr[] = {{1, 2, 50}, {3, 5, 20}, {6, 19, 100}, {2, 100, 200}};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Maximum profit sequence of jobs: " << findMaxProfit(arr, n) << endl;
return 0;
}
KRUSKAL’S ALGORITHM MST(MINIMUM SPANNING TREE) PROBLEMS
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
vector<Edge> edges;
};
struct Subset {
int parent, rank;
};
int e = 0;
int i = 0;
while (e < V - 1 && i < graph.E) {
Edge next_edge = graph.edges[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
int main() {
Graph graph = {4, 5, {{0, 1, 10}, {0, 2, 6}, {0, 3, 5}, {1, 3, 15}, {2, 3, 4}}};
KruskalMST(graph);
return 0;
}
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
vector<Edge> edges;
};
struct Subset {
int parent, rank;
};
int e = 0;
int i = 0;
int minCost = 0;
while (e < V - 1 && i < graph.E) {
Edge next_edge = graph.edges[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
minCost += next_edge.weight;
}
}
return minCost;
}
int main() {
Graph graph = {4, 5, {{0, 1, 10}, {0, 2, 6}, {0, 3, 5}, {1, 3, 15}, {2, 3, 4}}};
int minCost = kruskalMST(graph);
cout << "Minimum cost to connect all cities: " << minCost << endl;
return 0;
}
Description: Given a weighted undirected graph, find the maximum cost minimum spanning
tree using Kruskal's algorithm.
Solution Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
vector<Edge> edges;
};
struct Subset {
int parent, rank;
};
int e = 0;
int i = 0;
int maxCost = 0;
while (e < V - 1 && i < graph.E) {
Edge next_edge = graph.edges[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
maxCost = max(maxCost, next_edge.weight);
}
}
return maxCost;
}
int main() {
Graph graph = {4, 5, {{0, 1, 10}, {0, 2, 6}, {0, 3, 5}, {1, 3, 15}, {2, 3, 4}}};
int maxCost = kruskalMST(graph);
cout << "Maximum cost of MST: " << maxCost << endl;
return 0;
}
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
vector<Edge> edges;
};
struct Subset {
int parent, rank;
};
int e = 0;
int i = 0;
int totalCost = 0;
while (e < V - 1 && i < graph.E) {
Edge next_edge = graph.edges[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
totalCost += next_edge.weight;
}
}
return totalCost;
}
int main() {
Graph graph = {4, 5, {{0, 1, 10}, {0, 2, 6}, {0, 3, 5}, {1, 3, 15}, {2, 3, 4}}};
int minCost = kruskalMST(graph);
cout << "Minimum cost to build a communication network: " << minCost << endl;
return 0;
}
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
vector<Edge> edges;
};
struct Subset {
int parent, rank;
};
int find(Subset subsets[], int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
int e = 0;
int i = 0;
vector<int> minCosts(V, 0); // Stores the minimum cost for each connected component
vector<int> mstEdges;
while (e < V - 1 && i < graph.E) {
Edge next_edge = graph.edges[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
minCost += next_edge.weight;
mstEdges.push_back(i - 1); // Index of the edge in the original sorted edge list
minCosts[x] = next_edge.weight; // Update minCosts for the component
minCosts[y] = next_edge.weight; // Update minCosts for the component
}
}
// If there are remaining vertices not connected, they form separate trees
for (int j = i; j < graph.E; ++j) {
Edge edge = graph.edges[j];
int x = find(subsets, edge.src);
int y = find(subsets, edge.dest);
if (x != y) {
mstEdges.push_back(j); // Index of the edge in the original sorted edge list
minCosts[x] = edge.weight; // Update minCosts for the component
minCosts[y] = edge.weight; // Update minCosts for the component
}
}
delete[] subsets;
return minCosts;
}
int main() {
Graph graph = {4, 6, {{0, 1, 10}, {0, 2, 6}, {0, 3, 5}, {1, 3, 15}, {2, 3, 4}, {4, 5, 20}}};
int minCost = 0;
vector<int> minCosts = kruskalMST(graph, minCost);
cout << "Minimum cost of Minimum Spanning Forest: " << minCost << endl;
cout << "Minimum cost for each connected component:" << endl;
for (int i = 0; i < minCosts.size(); ++i) {
if (minCosts[i] != 0)
cout << "Component " << i << ": " << minCosts[i] << endl;
}
return 0;
}