Skip to content

Instantly share code, notes, and snippets.

@ratmcu
Created April 30, 2025 01:57
Show Gist options
  • Save ratmcu/46d2c1f315330bad58fd5171b907bc3d to your computer and use it in GitHub Desktop.
Save ratmcu/46d2c1f315330bad58fd5171b907bc3d to your computer and use it in GitHub Desktop.
cpp nested vector and depth weighted sum
#include <vector>
#include <functional>
#include <iostream>
#include <string>
// Forward declaration of the interface that will be used in the functions.
class NestedInteger {
public:
// Return true if this NestedInteger holds a single integer, rather than a nested list.
bool isInteger() const
{
// Assuming this function is implemented to return the boolean value.
return isIntegerFlag;
}
// Return the single integer that this NestedInteger holds, if it holds a single integer.
// The result is undefined if this NestedInteger holds a nested list.
int getInteger() const
{
// Assuming this function is implemented to return the integer value.
return value;
}
// Return the nested list that this NestedInteger holds, if it holds a nested list.
// The result is undefined if this NestedInteger holds a single integer.
const std::vector<NestedInteger> &getList() const
{
// Assuming this function is implemented to return the nested list.
return list;
}
// Constructor initializes a single integer.
explicit NestedInteger(int value) : value(value), isIntegerFlag(true) {
}
explicit NestedInteger(std::vector<NestedInteger> values) : isIntegerFlag(false) {
for (const NestedInteger& _value : values) {
if(_value.isInteger()) {
list.push_back(NestedInteger(_value.value));
} else {
list.push_back(NestedInteger(_value.list));
}
}
}
NestedInteger(const NestedInteger& value) {
if (value.isIntegerFlag) {
this->value = value.value;
isIntegerFlag = true;
} else {
isIntegerFlag = false;
list = value.list;
}
}
// define move constructor
// NestedInteger(NestedInteger&& other) : isIntegerFlag(other.isIntegerFlag), list(std::move(other.list)), value(other.value) {
// std::cout << "NestedInteger(NestedInteger&& other)" << std::endl;
// }
// define copy constructor
// NestedInteger(const NestedInteger& other) {
// std::cout << "NestedInteger(const NestedInteger& other)" << std::endl;
// if(other.isIntegerFlag) {
// value = other.value;
// isIntegerFlag = true;
// } else {
// isIntegerFlag = false;
// list = other.list; // Copy the nested list.
// std::cout << "list size: " << list.size() << std::endl;
// }
// }
private:
int value; // Holds the integer value if isIntegerFlag is true.
bool isIntegerFlag; // Flag to indicate if this is an integer or a nested list.
std::vector<NestedInteger> list; // Holds the nested list if isIntegerFlag is false.
};
/**
* This function calculates the sum of values in a nested list structure,
* where each element is multiplied by its depth in the nested structure.
*
* @param nestedList A list of NestedInteger.
* @return The depth sum of the nested list.
*/
int depthSum(const std::vector<NestedInteger>& nestedList) {
// Helper function to perform a depth-first search on the nested list.
std::function<int(const std::vector<NestedInteger>&, int)> dfs = [&](const std::vector<NestedInteger>& list, int depth) -> int {
int currentDepthSum = 0;
// Iterate through each element in the nested list.
for (const auto& item : list) {
if (item.isInteger()) { // Check if the item is an integer.
// Item is an integer, so add its value multiplied by its depth to the sum.
currentDepthSum += item.getInteger() * depth;
} else {
// Item is not an integer (it's a nested list), so call dfs recursively.
currentDepthSum += dfs(item.getList(), depth + 1);
}
}
// Return the sum for the current depth.
return currentDepthSum;
};
// Call the dfs helper function with the initial depth of 1 for the outermost list.
return dfs(nestedList, 1);
}
int main()
{
// Example usage
std::vector<NestedInteger> nestedList0 = {
NestedInteger(std::vector<NestedInteger>{
NestedInteger(std::vector<NestedInteger>{
NestedInteger{std::vector<NestedInteger>{
NestedInteger(1), NestedInteger(1)
}
}
}
)})
};
int result = depthSum(nestedList0);
std::cout << "Depth Sum: " << result << std::endl; // Output: 1*1 + 2*2 + 3*2 + (4+5)*3 = 1 + 4 + 6 + 27 = 38
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment