Problem Statement:
The system should identify a user's region based on their IP address.
IPv4 addresses are written as four numbers separated by periods, like "127.10.20.30". Each number is called an octet. Based on the first octet, addresses fall into one of five regions:
You are given a list of IP addresses. For each one, check if it is valid. If it is valid, return the region index (1 through 5). If it is not valid, return -1.
Problem Statement:
Given a tree with tree_nodes nodes, where each node i has an associated weight weight[i] (1-based indexing). The tree is rooted at node 1.
A subset of nodes is called "good" if there is no pair of nodes where one node is an ancestor of the other.
Find the maximum sum of weights from nodes that form a good subset.
Note: A node u is an ancestor of node v if it lies on the direct path from the root to node v.
The objective is to validate a given list of IPv4 address strings and classify them into one of five geographic regions based on their first octet. A valid IPv4 address must consist of exactly four numeric octets separated by periods (.), and each octet must be an integer between 0 and 255 (inclusive) with no invalid leading zeros.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Helper function to validate a single octet
bool isValidOctet(const string& s) {
if (s.empty() || s.length() > 3) return false;
// Prevent invalid leading zeros (e.g., "012")
if (s.length() > 1 && s[0] == '0') return false;
for (char c : s) {
if (!isdigit(c)) return false;
}
int val = stoi(s);
return val >= 0 && val <= 255;
}
// Function to process a single IP address
int getRegion(const string& ip) {
vector<string> octets;
string current = "";
for (char c : ip) {
if (c == '.') {
octets.push_back(current);
current = "";
} else {
current += c;
}
}
octets.push_back(current);
if (octets.size() != 4) return -1;
for (const string& octet : octets) {
if (!isValidOctet(octet)) return -1;
}
int firstOctet = stoi(octets[0]);
if (firstOctet >= 0 && firstOctet <= 127) return 1;
if (firstOctet >= 128 && firstOctet <= 191) return 2;
if (firstOctet >= 192 && firstOctet <= 223) return 3;
if (firstOctet >= 224 && firstOctet <= 239) return 4;
if (firstOctet >= 240 && firstOctet <= 255) return 5;
return -1;
}
vector<int> detectLocations(const vector<string>& ips) {
vector<int> results;
for (const string& ip : ips) {
results.push_back(getRegion(ip));
}
return results;
}
We are looking for a "good subset" of nodes in a rooted tree where no selected node is an ancestor of another selected node. To maximize the sum of weights, we have to make a binary choice at every node:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Helper DFS function for Tree DP
long long dfs(int u, int parent, const vector<vector<int>>& adj, const vector<int>& weight) {
long long sum_children = 0;
// Traverse all children of the current node
for (int v : adj[u]) {
if (v != parent) {
sum_children += dfs(v, u, adj, weight);
}
}
long long current_node_weight = weight[u - 1];
return max(current_node_weight, sum_children);
}
long long maxGoodSubsetWeight(int tree_nodes, vector<int> weight, vector<int> tree_from, vector<int> tree_to) {
vector<vector<int>> adj(tree_nodes + 1);
// Build the adjacency list for the tree
for (size_t i = 0; i < tree_from.size(); i++) {
adj[tree_from[i]].push_back(tree_to[i]);
adj[tree_to[i]].push_back(tree_from[i]);
}
// Start DFS from root (Node 1) with a dummy parent (0)
return dfs(1, 0, adj, weight);
}