id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) \n {\n if(s.length()!=t.length()) return false;\n int n=s.length();\n int m1[256]={0},m2[256]={0};\n for(int i=0;i<n;i++)\n {\n if(m1[s[i]]!=m2[t[i]]){\n return false;\n ...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n map<char, char> mpp;\n vector<int> si(200, -1);\n vector<int> ti(200, -1);\n\n int n = s.size();\n int m = t.size();\n\n if(n != m) return false;\n\n for (int i=0; i<n; i++) {\n ...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int map_s_to_t[256] = {0};\n int map_t_to_s[256] = {0};\n\n for (int i = 0; i < s.length(); i++) {\n char char_s = s[i];\n char char_t = t[i];\n\n if (map_s_to_t[char_s] == 0 && ma...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n if (s.size() != t.size()) return false; // Check if strings are of equal length\n\n map<char, char> translate;\n map<char, char> secondWay;\n\n for (int i = 0; i < s.size(); i++) {\n char charS = s[i];\n char...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\nprivate:\n bool ismapped(string s,string q){\n vector<pair<char,char>> mappin;\n for(int i=0;i<s.length();i++){\n int cnt =0;\n\n for(int j=0;j<mappin.size();j++){\n if(mappin[j].first == s[i]){\n cnt = 1;\n ...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n vector<int> spos(1000, -1),tpos(1000, -1);\n for(int i=0;i<s.size();i++){\n if(spos[(int)s[i]] != tpos[(int)t[i]]){\n return false;\n }\n spos[(int)s[i]] = i;\n ...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n\nvoid mapping(string &s){\n vector<char>mp(300,'0');\n char start='a';\n for(int i=0;i<s.size();i++){\n int idx=(int)s[i];\n if(mp[idx]=='0'){\n mp[idx]=start;\n start++;\n }\n }\n string ans=\"\";\n for(int i=0;i<s.size();i++...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n string mapping(string str, char* mapping,char& start){\n // mappedString to store \n string mappedString;\n\n for(auto ch : str){\n if(mapping[ch] == 0){\n mapping[ch] = start;\n start++;\n }\n\n ...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n string mapping(string str){\n // mappedString to store \n string mappedString;\n\n // create mapping\n char mapping[256] = {0};\n\n // start mapping from 'a'\n char start = 'a';\n\n for(auto ch : str){\n if(mapping[c...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int arr1[1230] = {0};\n int arr2[1230] = {0};\n \n if(s.size() != t.size())\n return 0;\n\n for(int i=0;i<s.size();i++)\n {\n if(arr1[s[i]] != arr2[t[i]])\n return...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n bool present(unordered_map<char,char>m,char ch){\n for(auto x:m){\n if(x.second==ch){\n return true;\n }\n }\n return false;\n }\n bool isIsomorphic(string s, string t) {\n unordered_map<char,char>m;\n ...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
1
{ "code": "class Solution {\npublic:\n bool present(unordered_map<char,char>m,char ch){\n for(auto x:m){\n if(x.second==ch){\n return true;\n }\n }\n return false;\n }\n bool isIsomorphic(string s, string t) {\n unordered_map<char,char>m;\n ...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
2
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int n1= s.length();\n // int n2= t.length();\n\n map<char, char> m1, m2;\n for(int i=0;i<n1;i++){\n if(m1[s[i]] == 0 && m2[t[i]] == 0){\n m1[s[i]] = t[i];\n m2[t[i]]...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
2
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n int n1= s.length();\n // int n2= t.length();\n\n map<char, char> m1, m2;\n for(int i=0;i<n1;i++){\n if(m1[s[i]] == 0 && m2[t[i]] == 0){\n m1[s[i]] = t[i];\n m2[t[i]]...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
3
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n map <char,int>m1,m2;\n if (s.size()!= t.size()) {\n return false;\n }\n for (int i = 0; i< s.size(); i++) {\n if (m1.find(s[i])!= m1.end())\n {\n if (m1[s[i]]...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
3
{ "code": "class Solution {\npublic:\n bool isIsomorphic(string s, string t) {\n map<char,char>mp1,mp2;\n if(s.length()!=t.length())return false;\n for(int i=0;i<s.size();i++){\n char original=s[i];\n char replacement=t[i];\n if(mp1.find(original)!=mp1.end() &&...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
3
{ "code": "class Solution {\npublic:\n\n bool bCheck(std::string s, std::string t)\n {\n std::unordered_map<char, char> umap;\n\n for (int i = 0; i < s.size(); i++)\n {\n if (umap.find(s[i]) == umap.end())\n umap[s[i]] = t[i];\n else\n {\n ...
205
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
3
{ "code": "#include <unordered_map>\nclass Solution {\npublic:\n bool check(string str1, string str2){\n // Unordered map to store the\n // Hash value for each string\n unordered_map<char, int> fre;\n \n int size1 = str1.size();\n int size2 = str2.size();\n \n // Che...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
0
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, str_k, str3; getline(cin, str) && getline(cin, str_k) && getline(cin...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
0
{ "code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, str_k, str3; getline(cin, str) && getline(cin, str_k) && getline(cin...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
0
{ "code": "static auto speedup = []() { std::ios_base::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); return NULL; }();\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long total = accumulate(nums.begin(),nums.end(...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
0
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum = 0, cnt = 0;\n int sacrifice = INT_MAX;\n \n for (int n : nums) {\n int xorValue = n ^ k;\n if (xorValue > n) {\n ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
0
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n \n long long int cg = 0;\n long long int mini = 1e18;\n long long int sum = 0;\n for(int i = 0;i<nums.size();i++)\n {\n sum+=max(nums[i],(n...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
0
{ "code": "//greedy approach\n\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum=0, cnt=0;\n int sacrifice = INT_MAX;\n\n for(int n:nums){\n int xorValue = n^k;\n if(xorValue>n){\n ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
0
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum = 0, cnt = 0;\n int sacrifice = INT_MAX;\n \n for (int n : nums) {\n int xorValue = n ^ k;\n if (xorValue > n) {\n ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
0
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum = 0, cnt = 0, sacrifice = INT_MAX;\n for (long long n : nums) {\n sum += max(n ^ k, n);\n cnt += (n ^ k) > n;\n sacrifice = min(sac...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
1
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long sum = 0;\n int cnt=0,minDiff = INT_MAX;\n for(const auto & num : nums){\n int X = (num^k);\n if(X > num)\n {\n su...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<int> diff(n);\n long long sum = 0;\n for(int i=0; i<nums.size(); i++){\n diff[i] = (nums[i]^k) - nums[i];\n su...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<int> diff(n);\n long long sum = 0;\n for(int i=0; i<nums.size(); i++){\n diff[i] = (nums[i]^k) - nums[i];\n su...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n=nums.size();\n vector<int> ans(n,0);\n for(int i=0;i<n;i++) ans[i]=nums[i]^k;\n int count=0;\n long long mini=INT_MAX;\n long long sum=0;\n ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n\tlong long maximumValueSum(std::vector<int>& nums, int k, std::vector<std::vector<int>>& edges) {\n\t\tstd::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n int n = nums.size();\n\t\tstd::vector<int> delta(n);\n\t\tlong lon...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\nprivate:\n long long dp[20001][2];\n long long f(int idx,int even,vector<int> &nums,int k) {\n // base cases\n if(idx >= nums.size()) return even? 0 : -1e9;\n if(dp[idx][even] != -1) return dp[idx][even];\n\n long long take = (nums[idx]^k) + f(idx+1,even^...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long dp[20005][2];\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n memset(dp,-1,sizeof(dp));\n return fun(0,0,nums,k);\n }\n long long fun(int idx,int ev,vector<int>& nums,int k){\n if(idx==nums.size()){...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long dp[20001][2];\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n if (n == 0) return 0; // Edge case: empty nums array\n\n // Initialize dp array with -1 (indicating uncomputed states...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<int> changed;\n for (int i = 0; i < nums.size(); ++i) {\n if ((nums[i] ^ k) < nums[i]) {\n continue;\n }\n\n nums[i] ^= k;\n changed.push_back(nums[i]);\n...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<long long>delta(n);\n for(int i =0; i<n; i++){\n delta[i]=(nums[i]^k)-nums[i];\n }\n sort(delta.begin(), delta.end()...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& a, int k, vector<vector<int>>& edges) {\n int n=a.size();\n\n vector<int> num;\n long long mn=INT_MIN+5;\n int zero=0;\n long long sum=0;\n\n for(int i=0;i<n;i++){\n sum+=1LL*a[i...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size(), mxmn = -1e9;\n vector<int> pos;\n long long sum = 0, extra = 0; \n for(int i=0; i<n; i++) {\n sum += nums[i];\n int xk ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n // void dfs(int node, int par, vector<int>& nums, int k, vector<vector<int>>& graph, vector<vector<long long>>& dp){\n // for(auto it:graph[node]){\n\n // }\n // }\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<ll> netDiff(n, 0);\n ll sum = 0;\n for(int i = 0; i < n; i++){\n sum += nums[i] * 1ll;\n n...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<int> delta(nums.size());\n \n for (int i = 0; i < nums.size(); ++i) {\n delta[i] = (nums[i] ^ k) - nums[i];\n }\n \n std::sort(delta...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n#define ll long long \n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n=nums.size();\n vector<int>v(n);\n for(int i=0;i<n;i++){\n v[i]=(nums[i]^k)-nums[i];\n }\n sort(v.rbegin(),v.rend());\...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n long long res = accumulate(nums.begin(), nums.end(), 0LL);\n vector<int> temp(n);\n for(int i=0;i<n;i++){\n temp[i] = (nums[i]^k)-...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector <int > d(nums.size());\n long long sum=0;\n for (int i=0;i<nums.size();i++){\n d[i]=(nums[i]^k)-nums[i];\n sum+=nums[i];\n }\n s...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans = 0;\n vector<int> v;\n for(int a: nums) {\n ans += a;\n v.push_back((a ^ k) - a);\n }\n sort(v.begin(), v.end(), greater...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "using LL=long long;\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n=nums.size();\n LL originSum=accumulate(nums.begin(),nums.end(),0LL);\n vector<LL> diff(n);\n for(int i=0;i<n;i++){\n diff[i]=(...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "const int maxn = 2e5 + 5;\nlong long dp[maxn][2];\nclass Solution\n{\npublic:\n long long rec_sol(int idx, vector<int> &nums, int k, int even)\n {\n int n = nums.size();\n if (idx == n)\n {\n // return even == 1 ? 0 : INT_MIN;\n return (dp[idx][even] = (even...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n // MISTAKES -->\n // 1. Use sgrtl() instead of sqrt() for long long\n // 2. GCD of |x-y|?\n // 3. Huffman Coding? Merging 2 smallest elements?\n // 4. Coordinate Compression?\n // 5. Min/Max Spanning Tree.\n // 6. Topo sort ? if there is something related to...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<int> v;\n long long sum = 0;\n for(int i = 0; i < nums.size(); i++) {\n v.push_back((nums[i] ^ k) - nums[i]);\n sum += nums[i];\n }\n ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& a, int k, vector<vector<int>>& g) {\n vector<int> pos, neg;\n for (int i = 0; i < a.size(); i++) {\n int x = a[i] ^ k;\n if (x > a[i]) pos.push_back(x - a[i]);\n else neg.push_back(x - a[i])...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) \n {\n vector<int>fayda;\n\n long long sum=0;\n\n for(int i=0;i<nums.size();i++)\n {\n sum+=nums[i];\n\n fayda.push_back((nums[i]^k)-nums[i]);\...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\ntypedef long long ll;\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<int>fayda;\n ll normalSum=0;\n for(int i=0;i<nums.size();i++){\n normalSum+=nums[i];\n fayda.push_back((nums[i]^k)-nums...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans=0;\n\n long long cnt=0;\n int n=nums.size();\n for(int i=0;i<n;i++){\n if((nums[i]^k)>=nums[i]){\n cnt++;\n }\n ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans = 0;\n int n = nums.size();\n vector<long long> v;\n for(int i=0; i<n; i++){\n cout<<(nums[i]^k)<<\" \";\n v.push_back((nums[i]^...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n=nums.size();\n vector<long long> delta;\n long long sum=0;\n for(int x:nums) sum+=x;\n for(int i=0; i<n; i++){\n delta.push_back((nums[i]^k)...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
2
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n vector<long long> delta;\n for(auto& num: nums) {\n delta.push_back((num^k) - num);\n }\n sort(delta.begin(), delta.end(), ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n long long ans = accumulate(nums.begin(),nums.end(),0LL);// case: we don't change anything\n vector<long long> delta;\n for(int& num : nums) d...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n priority_queue<long long, vector<long long>> pq;\n long long sum = 0;\n for(int i = 0; i < n; i++){\n //cout << ( (nums[i]^k) - nu...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<long long> diffs;\n long long result = 0;\n for (int num: nums) {\n result += num;\n diffs.push_back((num ^ k) - num);\n }\n sor...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& a, int k, vector<vector<int>>& edges) {\n int n=a.size();\n vector<pair<int,int> > diff;\n long long ans=0;\n for(int i=0;i<n;i++){\n int temp = (a[i]^k)-a[i];\n // cout << a[i] << \" \...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<long long> v;\n long long ans = 0;\n for (auto i : nums){\n v.push_back((i ^ k) - i);\n ans += i;\n }\n long long p = 0, s = ans...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n long long ans = 0LL;\n\n vector<pair<long long, long long>> ns;\n\n int n = nums.size();\n long long diff = LLONG_MAX, before = -1LL, after;\n \n for ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "#define pii pair<int,int>\n\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& v2, int K, vector<vector<int>>& edges) \n {\n\n vector<int> v1; for(auto it:v2) v1.push_back(it^K);\n vector< pii > d; for(int i=0;i<v1.size();i++) d.push_back({ v1[i], v2[i]}); \n sor...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\ntypedef long long int ll;\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n ll n=nums.size();\n multimap<ll,ll> mp;\n ll flag=0;\n vector<ll> vis(n,0);\n for(ll i=0;i<n;i++){\n if((nums[i]^k)>num...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n\n long long maximumValueSum(vector<int>& a, int k, vector<vector<int>>& edges) {\n int n = a.size();\n vector<vector<ll>> dp(n, vector<ll> (2, -1e9));\n dp[0][0] = a[0];\n dp[0][1] = a[0] ^ k;\n for(int i = 1; i < n; i+...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n \n \n \n vector<vector<long>> dp(n+1,vector<long>(2,0));\n\n dp[n][0]=-1e9;\n dp[n][1]=0;\n for(int idx=n-1 ; idx>=0 ;...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n // Node: edge is unimportant - can apply on any pair of nums\n int n = nums.size();\n vector<vector<long long>> dp(n, vector<long long>(2, -1));\n dp[0][0] = nums[0...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "typedef long long ll;\nconst ll inf = -1e9;\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k,\n vector<vector<int>>& edges) {\n ll n = nums.size();\n vector<vector<ll>> dp(n + 1, vector<ll>(2, 0));\n dp[0][0] = nums[0];\n ...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\n typedef long long int ll;\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<pair<ll,ll>> xored(nums.size());\n for(int i=0;i<nums.size();i++){\n xored[i] = {(nums[i]^k) - nums[i],nums[i]};\n co...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "#define f(i,a,n) for(int i=a;i<n;i++)\n#define ll long long\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n\n ll sum =0;\n int n = nums.size();\n multiset<int,greater<int>> m;\n\n f(i,0,n) {\n int x =...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n int n = nums.size();\n//agar ek tree me dekhe toh hm koi bhi 2 node ka xor kr skte hai not necessarily adjacent.. \n//toh basically yeh ek array hai aur koi bhi 2 index ka xor kr skt...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<pair<int,int>> v;\n for(int i=0;i<nums.size();i++)\n {\n v.push_back({(nums[i]^k)-nums[i],i});\n }\n sort(v.begin(),v.end());\n long...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "//m2\n//knapsack DP, can't use tree DP.\n\n\n//instead of xor analysis, m2 is for practice.\n\n//[A] xor property: xor is both multiplication & division.\n\n//1. so operations on edges (a,b) & (b,c) only affects a & c.\n//2. since it's connectd (tree), we can have a path connecting any 2 nodes --> operatio...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "//m2\n//knapsack DP, can't use tree DP.\n\n\n//instead of xor analysis, m2 is for practice.\n\n//[A] xor property: xor is both multiplication & division.\n\n//1. so operations on edges (a,b) & (b,c) only affects a & c.\n//2. since it's connectd (tree), we can have a path connecting any 2 nodes --> operatio...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "//m2\n//knapsack DP, can't use tree DP.\n\n\n//instead of xor analysis, m2 is for practice.\n\n//[A] xor property: xor is both multiplication & division.\n\n//1. so operations on edges (a,b) & (b,c) only affects a & c.\n//2. since it's connectd (tree), we can have a path connecting any 2 nodes --> operatio...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "//m2\n//knapsack DP, can't use tree DP.\n\n\n//instead of xor analysis, m2 is for practice.\n\n//[A] xor property: xor is both multiplication & division.\n\n//1. so operations on edges (a,b) & (b,c) only affects a & c.\n//2. since it's connectd (tree), we can have a path connecting any 2 nodes --> operatio...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\n\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k,\n\n vector<vector<int>>& edges) {\n\n vector<vector<long long>> memo(nums.size(), vector<long long>(2, -1));\n\n return maxSumOfNodes(0, 1, nums, k, memo);\n\n }\n\nprivate:\...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k,\n vector<vector<int>>& edges) {\n vector<vector<long long>> memo(nums.size(), vector<long long>(2, -1));\n return maxSumOfNodes(0, 1, nums, k, memo);\n }\n\nprivate:\n long lo...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\n\npublic:\n\n long long maximumValueSum(vector<int>& nums, int k,\n\n vector<vector<int>>& edges) {\n\n vector<vector<long long>> memo(nums.size(), vector<long long>(2, -1));\n\n return maxSumOfNodes(0, 1, nums, k, memo);\n\n }\n\nprivate:\...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k,\n vector<vector<int>>& edges) {\n vector<vector<long long>> memo(nums.size(), vector<long long>(2, -1));\n return maxSumOfNodes(0, 1, nums, k, memo);\n }\n\nprivate:\n long lo...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long util(vector<int> &nums, int k,vector<vector<long long>>& dp, int isEven, int index)\n {\n if(index == nums.size())\n {\n if(isEven)return 0;\n else return INT_MIN;\n }\n \n if(dp[index][isEven] != -1)return...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\n struct FindSumsResult {\n long long resultIfFlipped;\n long long resultIfNotFlipped;\n };\n\n // Returns the maximum possible sum, and whether the root needs to be flipped to get there\n FindSumsResult findSums(const vector<int>& nums, int k, vector<vector<int>>...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<vector<int>> adjacencies(nums.size(),vector<int>());\n for(vector<int>& edge:edges){\n adjacencies[edge[0]].push_back(edge[1]);\n adjacencies[edge[1]...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n vector<vector<int>> adjacencies(nums.size(),vector<int>());\n for(vector<int>& edge:edges){\n adjacencies[edge[0]].push_back(edge[1]);\n adjacencies[edge[1]...
3,307
<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is a...
3
{ "code": "class Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k,\n vector<vector<int>>& edges) {\n vector<vector<int>> diff;\n for (auto x : nums) {\n // cout<<x<<\" \"<<x^k<<endl;\n diff.push_back({(x ^ k) - x, x});\n ...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n bool static comp(int a, int b){\n if(a>b) return 1;\n return 0;\n }\n long long maximumHappinessSum(vector<int>& a, int k) {\n long long ans=0;\n sort(a.begin(), a.end(), comp);\n int turn=0;\n for(int i=0;i<a.size();i++){\n ...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n std::ranges::sort(happiness, std::greater{});\n\n auto fold_op = [time = 0](long long lhs, int rhs) mutable {\n return lhs + std::max(rhs - time++, 0);\n };\n\n return std:...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n std::ranges::sort(happiness, std::greater<int>());\n int decrement = 0;\n\n long long res = 0;\n for (int h : happiness) {\n if (h - decrement <= 0 || k == 0) {\n ...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n ranges::sort(happiness);\n int size = happiness.size();\n int j=0;\n long long result = 0;\n for (int i=size-1;i>=0 && j<k;i--,j++) {\n if (happiness[i] - j >=0) {\n...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\n public:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n long ans = 0;\n int decremented = 0;\n\n ranges::sort(happiness, greater<>());\n\n for (int i = 0; i < k; ++i) {\n ans += max(0, happiness[i] - decremented);\n ++decremented;\n }\n\n r...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& v, int k) {\n sort(v.begin(),v.end());\n int n=v.size();\n long long int happy=v[n-1];\n long long int r=1;\n for(int i=n-2;i>=n-k;i--){\n if(v[i]-r>=0)\n v[i]=v[i]-r;\n else...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int> &vec, int k) {\n int n = vec.size();\n sort(vec.begin(), vec.end());\n\n long long ans = 0;\n int cur = 0;\n for(int i=n-1; i>=0; i--) {\n if(vec[i] - cur >= 0) ans += (vec[i] - cur);\n ...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(happiness.begin(),happiness.end(),greater<int>());\n long long sum=0;\n for(int i=0;i<k;i++){\n sum+=max(happiness[i]-i,0);\n }\n return sum;\n }\n};", "me...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(happiness.begin(),happiness.end(),greater<int>());\n long long sum=0;\n for(int i=0;i<k;i++){\n if(happiness[i]-i>=0){\n sum+=happiness[i]-i; \n ...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
0
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(begin(happiness), end(happiness));\n long long turn = 0;\n long long r = 0;\n while (\n turn < k &&\n 0 < happiness[happiness.size() - 1 - turn] - turn\n ...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
1
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(happiness.begin(),happiness.end(),greater<int>());\n int n=happiness.size(),i=1;\n int dec=0;\n long long ans=0;\n ans=ans+happiness[0];\n dec++;\n while(i<n...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
1
{ "code": "#define ll long long int\nclass Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n ll ans=0, decrease=0;\n //sort in descending order;\n sort(happiness.begin(), happiness.end(), greater<int>());\n //need to make only k selections;\n ...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
2
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n sort(happiness.begin(), happiness.end(), greater<int>());\n long long ans=0;\n int i=0;\n for(int j=0;j<k;j++){\n ...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
2
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n int geq_k_count = 0, n = happiness.size();\n for (int x : happiness) {\n geq_k_count += x >= k;\n }\n\n if (geq_k_count >= k) {\n auto start = happiness.begin();...
3,351
<p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select...
2
{ "code": "class Solution {\npublic:\n long long maximumHappinessSum(std::vector<int>& happiness, int k) {\n std::sort(happiness.begin(), happiness.end(), std::greater<int>());\n\n std::vector<bool> selected(happiness.size(), false);\n\n long long ans = 0;\n int count = 0;\n\n ...