question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
minimum-swaps-to-make-sequences-increasing | C++ Solution| Dp | O(n) | Well commented code and well explained | c-solution-dp-on-well-commented-code-and-l3ko | Here we are taking dp[n][2] where for any index i there are 2 case-\n-->dp[i][0] - when we don\'t want to swap the ith state\n-->dp[i][1]- when we want to swap | yash_kothari | NORMAL | 2021-03-19T12:16:02.226969+00:00 | 2021-03-19T12:16:02.227000+00:00 | 362 | false | Here we are taking dp[n][2] where for any index i there are 2 case-\n-->dp[i][0] - when we don\'t want to swap the ith state\n-->dp[i][1]- when we want to swap the ith state \nThen for any index i we have two situations as follows-\n--> A[i-1]<A[i] && B[i-1]<B[i] then\n\t\t------> if we don\'t swap the ith state dp[i... | 2 | 0 | [] | 3 |
minimum-swaps-to-make-sequences-increasing | Python DP solution O(n) time O(1) space | python-dp-solution-on-time-o1-space-by-h-ud74 | \nclass Solution:\n def minSwap(self, A: List[int], B: List[int]) -> int:\n \n # DP\n # \n # state: f(i, s) := min swaps to make | hooraywhoami | NORMAL | 2021-02-21T23:37:35.876466+00:00 | 2021-02-21T23:42:45.452110+00:00 | 220 | false | ```\nclass Solution:\n def minSwap(self, A: List[int], B: List[int]) -> int:\n \n # DP\n # \n # state: f(i, s) := min swaps to make A[:i+1] B[:i+1] strckly increasing, s indicating if last bit is swapped or not\n # funct: \n # f(i, 0) = min(f(i-1, 0), f(i-1, 1)) (chec... | 2 | 0 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | Clean Python Solution (With Other Similar Problems) | clean-python-solution-with-other-similar-xu3n | python\nclass Solution(object):\n def minSwap(self, A, B):\n keep = [float(\'inf\') for _ in xrange(len(A))]\n swap = [float(\'inf\') for _ in | christopherwu0529 | NORMAL | 2021-02-14T07:04:47.213467+00:00 | 2021-02-14T07:04:47.213515+00:00 | 179 | false | ```python\nclass Solution(object):\n def minSwap(self, A, B):\n keep = [float(\'inf\') for _ in xrange(len(A))]\n swap = [float(\'inf\') for _ in xrange(len(A))]\n \n keep[0] = 0\n swap[0] = 1\n \n for i in xrange(1, len(A)):\n \n if A[i]>A[i-1] ... | 2 | 0 | [] | 1 |
minimum-swaps-to-make-sequences-increasing | Python DP solution with explanation | python-dp-solution-with-explanation-by-a-hzpo | \nclass Solution(object):\n def minSwap(self, A, B):\n if len(A) != len(B) or len(A) == 0:\n return -1 \n \n # | amadiaos | NORMAL | 2020-12-03T16:03:00.346849+00:00 | 2020-12-03T16:03:00.346896+00:00 | 186 | false | ```\nclass Solution(object):\n def minSwap(self, A, B):\n if len(A) != len(B) or len(A) == 0:\n return -1 \n \n #it shows the starting status at 0 position\n #pkeep stores the previous no swap minimum value, so at 0 position, the value is 0 if no swap\n #p... | 2 | 0 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | logically swap or not swap recursively O(n) a solution explanation you can understand | logically-swap-or-not-swap-recursively-o-fq6r | At each index, we compare the results after swapping and not swapping A and B.\n\nwe need to know from the previous step, if A and B are swapped, if so previous | coolgk | NORMAL | 2020-11-11T23:48:32.341397+00:00 | 2020-11-11T23:53:58.925772+00:00 | 207 | false | **At each index, we compare the results after swapping and not swapping A and B.**\n\n**we need to know from the previous step, if A and B are swapped, if so `previous a` becomes `previous b`, and `previous b` becomes `previous a`**\n\n**when `current a > previous b` AND `current b > previous a`, swap action is possibl... | 2 | 0 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | Java explanation using DP | java-explanation-using-dp-by-alex_molina-ytux | The idea is that you have an array int[][] dp = new int[A.length][2], where dp[i][0] means we have the min number of swaps up to index i assuming we didnt swap | alex_molina | NORMAL | 2020-10-01T22:14:21.846581+00:00 | 2020-10-01T22:17:01.749332+00:00 | 187 | false | The idea is that you have an array int[][] dp = new int[A.length][2], where dp[i][0] means we have the min number of swaps up to index i assuming we didnt swap the ith column entries, and dp[i][1] means we have the min number of swaps up to index i assuming we swapped the ith column entries.\n\nTo get the next column e... | 2 | 0 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | [C] Non-DP easy-to-understand O(n) solution O(1) space | c-non-dp-easy-to-understand-on-solution-ytma2 | The DP solution is difficult to figure out. Below is my solution.\n\nFor the input arrays A, B of length n, denoted by problem (A, B, n), define an index e to b | hang_er | NORMAL | 2020-04-25T20:53:06.235136+00:00 | 2020-04-26T00:06:16.377891+00:00 | 232 | false | The DP solution is difficult to figure out. Below is my solution.\n\nFor the input arrays A, B of length n, denoted by problem (A, B, n), define an index e to be "good" if e=0 OR min{A[e], B[e]} > max{A[e-1], B[e-1]}. Whether or not to swap a "good" index e is not affected by the subarrays [0 ... e-1].\n\nFor the examp... | 2 | 0 | ['C'] | 0 |
minimum-swaps-to-make-sequences-increasing | Java dp solution | java-dp-solution-by-cuny-66brother-dc9n | \nclass Solution {\n int MAX=2000;\n public int minSwap(int[] A, int[] B) {\n MAX=A.length+10;\n if(A.length==1)return 0;\n int dp[][ | CUNY-66brother | NORMAL | 2020-03-02T22:04:34.195345+00:00 | 2020-03-02T22:04:34.195395+00:00 | 210 | false | ```\nclass Solution {\n int MAX=2000;\n public int minSwap(int[] A, int[] B) {\n MAX=A.length+10;\n if(A.length==1)return 0;\n int dp[][]=new int[2][A.length]; //[Nonswap,swap]\n dp[0][0]=0;dp[1][0]=1;\n for(int i=1;i<A.length;i++){\n //Non swap\n int nonA=... | 2 | 1 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | C++ beat 99.3% with clear explanations | c-beat-993-with-clear-explanations-by-sk-t6m8 | \nclass Solution {\npublic:\n int minSwap(vector<int> &A, vector<int> &B) {\n int vec_len = A.size();\n /*dp[i][0]: the cost if do not exchange | sktzwhj | NORMAL | 2018-10-18T00:32:22.910511+00:00 | 2018-10-18T00:32:22.910561+00:00 | 398 | false | ```\nclass Solution {\npublic:\n int minSwap(vector<int> &A, vector<int> &B) {\n int vec_len = A.size();\n /*dp[i][0]: the cost if do not exchange at position i. dp[i][1]: the cost of changing at position i*/\n vector <vector<int>> dp = vector <vector< int >> (vec_len, vector<int>(2, INT32_MAX))... | 2 | 1 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | Simple DP in Java with time:O(N) space:O(1) | simple-dp-in-java-with-timeon-spaceo1-by-qo42 | \nIf you use the recursive function, the recursion would be something like:\n\nO swapped X not swapped\n\n[0] O X\n[1] | luckman | NORMAL | 2018-04-30T01:04:30.983599+00:00 | 2018-04-30T01:04:30.983599+00:00 | 613 | false | ```\nIf you use the recursive function, the recursion would be something like:\n\nO swapped X not swapped\n\n[0] O X\n[1] O X O X\n[2] O X O X O X O X \n :\n \n \nOptimal Subtructure would be:\n \n [i] O + 1\n ... | 2 | 0 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | very easy to understand python solution | very-easy-to-understand-python-solution-6ixd3 | \nclass Solution(object):\n def minSwap(self, A, B):\n """\n :type A: List[int]\n :type B: List[int]\n :rtype: int\n """\n | medi_ | NORMAL | 2018-03-19T15:19:14.697973+00:00 | 2018-03-19T15:19:14.697973+00:00 | 477 | false | ```\nclass Solution(object):\n def minSwap(self, A, B):\n """\n :type A: List[int]\n :type B: List[int]\n :rtype: int\n """\n \n keep=[float(\'inf\') for i in range(len(A))]\n swap=[float(\'inf\') for i in range(len(A))]\n \n # first element you c... | 2 | 1 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | Best C++ Solution || Space optimize approach | best-c-solution-space-optimize-approach-jnmxk | Complexity
Time complexity:O(n)
Space complexity:O(1)
Code | kansalhimanshu123 | NORMAL | 2025-04-04T18:32:49.926456+00:00 | 2025-04-04T18:32:49.926456+00:00 | 21 | false |
# Complexity
- Time complexity:O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int Solve(vector<int>&nums1,vector<int>&nums2){
int n=nums1.size();
vector<int>nex... | 1 | 0 | ['Array', 'Dynamic Programming', 'C++'] | 0 |
minimum-swaps-to-make-sequences-increasing | Easy Solution using dp + memo+ tabulation | easy-solution-using-dp-memo-tabulation-b-5xrx | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | HYDRO2070 | NORMAL | 2025-01-01T18:19:40.337893+00:00 | 2025-01-01T18:19:40.337893+00:00 | 22 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 1 | 0 | ['Array', 'Dynamic Programming', 'C', 'C++'] | 1 |
minimum-swaps-to-make-sequences-increasing | Easiest optimized || C++ | easiest-optimized-c-by-radhakrishnaaaa-8wz8 | Complexity
Time complexity: O(n)
Space complexity: O(n)
Code | RadhaKrishnaaaa | NORMAL | 2025-01-01T02:33:05.957731+00:00 | 2025-01-01T02:33:05.957731+00:00 | 53 | false | # Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(n)
<!-- Add your space coacasmplexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
vector<vector<int>>dp;
int solve (int i, bool prev_swapped, vector<int>& nums1, vector<... | 1 | 0 | ['Dynamic Programming', 'C++'] | 0 |
minimum-swaps-to-make-sequences-increasing | Python O(n) time, O(1) space | python-on-time-o1-space-by-babos-ranj | \npython3 []\nclass Solution:\n def minSwap(self, nums1: List[int], nums2: List[int]) -> int:\n dp = [0, 1]\n\n for i in range(1, len(nums1)):\ | babos | NORMAL | 2024-11-19T06:45:56.559040+00:00 | 2024-11-19T06:45:56.559067+00:00 | 77 | false | \n```python3 []\nclass Solution:\n def minSwap(self, nums1: List[int], nums2: List[int]) -> int:\n dp = [0, 1]\n\n for i in range(1, len(nums1)):\n noSwap, yesSwap = 0, 0\n\n # check valid configurations\n if (nums1[i-1] >= nums1[i]) or (nums2[i-1] >= nums2[i]): \n ... | 1 | 0 | ['Python3'] | 0 |
minimum-swaps-to-make-sequences-increasing | All approach ...Recursion To.......DP............/\/\/\/\.................... | all-approach-recursion-todp-by-ghanshyam-p6vb | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Ghanshyam_bunkar016 | NORMAL | 2024-08-23T05:24:47.664015+00:00 | 2024-08-23T05:24:47.664047+00:00 | 2 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['C++'] | 0 |
minimum-swaps-to-make-sequences-increasing | space optimized || using dynamic programming || beats (runtime -> 94.41%) (memory-> 90.65%)🔥 | space-optimized-using-dynamic-programmin-pwg2 | Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n\nusing recursion\n\n\n int rec(vector<int>& nums1, vector<int>& | arnavmehta290 | NORMAL | 2023-09-17T10:21:06.344515+00:00 | 2023-09-17T10:21:06.344538+00:00 | 8 | false | - Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n```\nusing recursion\n```\n\n int rec(vector<int>& nums1, vector<int>& nums2, int swaps, int i){\n if(i>=nums1.size()){\n return 0;\n }\n int prev1 = nums1[i-1];\n int prev2 = nums2[i-1];\n\n ... | 1 | 0 | ['Array', 'Dynamic Programming', 'Recursion', 'Memoization', 'Queue', 'C++'] | 0 |
minimum-swaps-to-make-sequences-increasing | Java with explanation. DFS + Memoization. SC: O(N), TC: O(N) | java-with-explanation-dfs-memoization-sc-eh6p | \n# Code\n\nclass Solution {\n Integer[][] memo;\n int SWAPPED = 0;\n int NOT_SWAPPED = 1;\n\n int NOT_VALID = (int) Math.pow(10, 6);\n public in | vera_uva | NORMAL | 2023-08-29T15:06:46.614822+00:00 | 2023-08-29T15:06:46.614854+00:00 | 216 | false | \n# Code\n```\nclass Solution {\n Integer[][] memo;\n int SWAPPED = 0;\n int NOT_SWAPPED = 1;\n\n int NOT_VALID = (int) Math.pow(10, 6);\n public int minSwap(int[] nums1, int[] nums2) {\n this.memo = new Integer[nums1.length][2];\n return dfs(nums1, nums2, 0, NOT_SWAPPED);\n }\n\n pri... | 1 | 0 | ['Java'] | 0 |
minimum-swaps-to-make-sequences-increasing | Rust | Bottom-up DP, One-Pass, no extra space | rust-bottom-up-dp-one-pass-no-extra-spac-yf6x | Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n\npub fn solve(\n vec_0: Vec<u32>,\n vec_1: Vec<u32>,\n) -> usize {\n let | soyflourbread | NORMAL | 2023-08-05T02:17:40.393864+00:00 | 2023-08-05T02:17:40.393890+00:00 | 8 | false | # Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\npub fn solve(\n vec_0: Vec<u32>,\n vec_1: Vec<u32>,\n) -> usize {\n let mut dp = [usize::MIN, 1];\n\n for i in 1..vec_0.len() {\n let (e0, e1) = (vec_0[i], vec_1[i]);\n let (p0, p1) = (vec_0[i - 1], vec... | 1 | 0 | ['Dynamic Programming', 'Rust'] | 0 |
minimum-swaps-to-make-sequences-increasing | C++ || DP || MEMOIZATION DP | c-dp-memoization-dp-by-hey_himanshu-mh0f | REC + MEMO\n\nclass Solution {\npublic:\n int solve(vector<int>& nums1, vector<int>& nums2 , int index , int swapped , vector<vector<int>> &dp){\n\n | Hey_Himanshu | NORMAL | 2023-06-14T07:24:38.593830+00:00 | 2023-06-14T07:24:38.593851+00:00 | 455 | false | REC + MEMO\n```\nclass Solution {\npublic:\n int solve(vector<int>& nums1, vector<int>& nums2 , int index , int swapped , vector<vector<int>> &dp){\n\n // base case \n if(index == nums1.size()){\n return 0 ;\n }\n\n int ans = INT_MAX ;\n int prev1 = nums1[index-1] ... | 1 | 0 | ['Dynamic Programming', 'C', 'C++'] | 0 |
minimum-swaps-to-make-sequences-increasing | Solution | solution-by-deleted_user-9ezd | C++ []\nconst int ZERO = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n int minSwap(v | deleted_user | NORMAL | 2023-05-01T00:08:37.700242+00:00 | 2023-05-01T01:05:02.880695+00:00 | 1,974 | false | ```C++ []\nconst int ZERO = []() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n int minSwap(vector<int>& a, vector<int>& b) {\n int n = static_cast<int>(a.size());\n vector<int> swaps(n, n);\n vector<int> noswaps(n, n);\n ... | 1 | 0 | ['C++', 'Java', 'Python3'] | 0 |
minimum-swaps-to-make-sequences-increasing | C++ simple solution 96.04% faster 中文註解 | c-simple-solution-9604-faster-zhong-wen-abuk6 | \n\n// 801. Minimum Swaps To Make Sequences Increasing\nclass Solution {\npublic:\n int minSwap(std::vector<int>& nums1, std::vector<int>& nums2) {\n | paulchen2713 | NORMAL | 2022-10-10T05:05:47.834685+00:00 | 2022-10-10T05:05:47.834712+00:00 | 62 | false | \n```\n// 801. Minimum Swaps To Make Sequences Increasing\nclass Solution {\npublic:\n int minSwap(std::vector<int>& nums1, std::vector<int>& nums2) {\n // \u7576\u524D\u4F4D\u7F6E i \u662F\u5426\u8981\u4EA4\u63DB, \u53EA\u53D6\u6C7A\u65BC\u7576\u524D\u4F4D\u548C\u524D\u4E00\u4F4D\u662F\u5426\u662F\u56B4\u683... | 1 | 0 | ['C'] | 0 |
minimum-swaps-to-make-sequences-increasing | Recursive -> TopDown -> BottomUP -> Space Optimized | recursive-topdown-bottomup-space-optimiz-z1i2 | \nclass Solution {\nprivate:\n int recursive(vector<int>& nums1, vector<int>& nums2, int idx, int swapped){\n cout << idx << " " << swapped << endl;\n | krishnajsw | NORMAL | 2022-09-28T14:19:05.988158+00:00 | 2022-09-28T14:19:05.988224+00:00 | 64 | false | ```\nclass Solution {\nprivate:\n int recursive(vector<int>& nums1, vector<int>& nums2, int idx, int swapped){\n cout << idx << " " << swapped << endl;\n if(idx == nums1.size()) return 0;\n int ans = INT_MAX;\n if(idx == 0 || nums1[idx] > nums1[idx - 1] && nums2[idx] > nums2[idx - 1]){\n ... | 1 | 0 | ['Dynamic Programming', 'Recursion'] | 0 |
minimum-swaps-to-make-sequences-increasing | C++ DP Solution [Tabulation] | c-dp-solution-tabulation-by-iamanjali-iw0k | \nclass Solution {\npublic:\n int minSwap(vector<int>& nums1, vector<int>& nums2) {\n \n int n = nums1.size();\n \n vector<vector | iamanjali | NORMAL | 2022-09-28T09:58:15.035938+00:00 | 2022-09-28T09:58:15.035980+00:00 | 51 | false | ```\nclass Solution {\npublic:\n int minSwap(vector<int>& nums1, vector<int>& nums2) {\n \n int n = nums1.size();\n \n vector<vector<int>> dp(n, vector<int>(2,-1));\n \n dp[0][0] = 0;\n dp[0][1] = 1;\n \n \n \n for(int i=1;i<n;i++) {\n ... | 1 | 0 | ['Dynamic Programming', 'C'] | 0 |
minimum-swaps-to-make-sequences-increasing | Java ||Recursion and Memoization Solution || clears understanding | java-recursion-and-memoization-solution-8hy3v | ```\nclass Solution {\n \n int [][] dp;\n public int minSwap(int[] nums1, int[] nums2) {\n List list1 = new ArrayList<>();\n \n for( | kurmiamreet44 | NORMAL | 2022-09-17T08:09:55.954800+00:00 | 2022-09-17T08:09:55.954837+00:00 | 102 | false | ```\nclass Solution {\n \n int [][] dp;\n public int minSwap(int[] nums1, int[] nums2) {\n List<Integer> list1 = new ArrayList<>();\n \n for(int x : nums1)\n {\n list1.add(x);\n }\n list1.add(0,-1);\n \n List<Integer> list2 = new ArrayList<>();\n... | 1 | 0 | ['Java'] | 0 |
minimum-swaps-to-make-sequences-increasing | c++ | RECURSION + MEMOIZATION | (DP Approach) | c-recursion-memoization-dp-approach-by-l-0tgc | Brute Force Recursion Approach\n\nint bruteForce()(vector<int>& v1, vector<int>& v2, int i){\n\tint n = v1.size();\n\tif(i>1 && (v1[i-2]>= v1[i-1] || v2[i-2]>=v | lokesh_0 | NORMAL | 2022-08-16T10:10:37.015925+00:00 | 2022-08-16T10:10:37.015950+00:00 | 119 | false | **Brute Force Recursion Approach**\n```\nint bruteForce()(vector<int>& v1, vector<int>& v2, int i){\n\tint n = v1.size();\n\tif(i>1 && (v1[i-2]>= v1[i-1] || v2[i-2]>=v2[i-1])) return 1e6;\n\tif(i==n) return 0;\n\n\t// Swap for current index so 1 + getMinSwap(...);\n\tint min_swaps = INT_MAX;\n\t// swapping for current ... | 1 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'C'] | 1 |
minimum-swaps-to-make-sequences-increasing | c++ easy memonization DP | c-easy-memonization-dp-by-code_in_red-9qrq | \nclass Solution {\npublic:\n int dp[100005][5];\n int help(vector<int>&a,vector<int>&b,int i,int s){\n if(i==a.size())return 0;\n if(dp[i][ | code_in_red | NORMAL | 2022-05-30T07:46:52.507635+00:00 | 2022-05-30T07:46:52.507666+00:00 | 129 | false | ```\nclass Solution {\npublic:\n int dp[100005][5];\n int help(vector<int>&a,vector<int>&b,int i,int s){\n if(i==a.size())return 0;\n if(dp[i][s]!=-1)return dp[i][s];\n int r=INT_MAX;\n if(s){ //previous element was swapped\n if(i-1>=0&&a[i]>b[i-1]&&b[i]>a... | 1 | 0 | ['Dynamic Programming', 'Memoization', 'C'] | 0 |
minimum-swaps-to-make-sequences-increasing | ✅ Memoization | With Explanation | Simple | C++ | memoization-with-explanation-simple-c-by-hg7g | \nclass Solution {\npublic:\n vector <vector <int>> dp;\n // dp[i][0] -> swaps needed to make array from index i to n stricly increasing considering index | vinoltauro | NORMAL | 2022-04-01T06:39:09.008889+00:00 | 2022-04-01T06:39:09.008927+00:00 | 201 | false | ```\nclass Solution {\npublic:\n vector <vector <int>> dp;\n // dp[i][0] -> swaps needed to make array from index i to n stricly increasing considering index i is not swapped\n // dp[i][1] -> swaps needed to make array from index i to n stricly increasing considering index i is swapped\n \n int helper(ve... | 1 | 0 | [] | 0 |
minimum-swaps-to-make-sequences-increasing | JAVA: EASY DP | java-easy-dp-by-nicolas2lee-aubs | The idea is very simple, enumerate all cases.\ndp[i][0] do not swap current pair \ndp[i][1] swap current pair\n\n* a1\nif (a1<b2 && b1<a2){\n dp[i][0] = Math | nicolas2lee | NORMAL | 2022-03-06T19:40:29.927509+00:00 | 2022-03-06T19:43:38.601196+00:00 | 413 | false | The idea is very simple, enumerate all cases.\ndp[i][0] do not swap current pair \ndp[i][1] swap current pair\n\n* a1<a2 && b1<b2\n\n```\nif (a1<b2 && b1<a2){\n dp[i][0] = Math.min(Math.min(dp[i-1][0], dp[i-1][1]), dp[i][0]);\n dp[i][1] = Math.min(Math.min(dp[i-1][0]+1,dp[i-1][1]+1), dp[i][1]);\n}else{\n dp[i]... | 1 | 0 | ['Dynamic Programming', 'Java'] | 1 |
minimum-swaps-to-make-sequences-increasing | A graspable recursive solution | a-graspable-recursive-solution-by-su7ss-7uah | \nclass Solution {\n public int minSwap(int[] nums1, int[] nums2) {\n Map<String, Integer> memo = new HashMap<>();\n return Math.min(dfs(nums1, | su7ss | NORMAL | 2022-01-24T05:37:15.936577+00:00 | 2022-01-24T05:37:15.936619+00:00 | 177 | false | ```\nclass Solution {\n public int minSwap(int[] nums1, int[] nums2) {\n Map<String, Integer> memo = new HashMap<>();\n return Math.min(dfs(nums1, nums2, 1, false, memo), \n dfs(nums1, nums2, 1, true, memo) + 1);\n }\n \n private int dfs(int[] nums1, int[] nums2, int ind... | 1 | 1 | ['Recursion', 'Memoization'] | 1 |
minimum-swaps-to-make-sequences-increasing | C++ DP O(N) Time and O(1) space solution | c-dp-on-time-and-o1-space-solution-by-ha-a1s8 | \nclass Solution {\npublic:\n int minSwap(vector<int> &one, vector<int> &two)\n {\n int n = one.size();\n int swap = 1;\n int noSwap = 0;\n\n for | haidermalik | NORMAL | 2022-01-04T08:03:42.478587+00:00 | 2022-01-04T08:03:42.478621+00:00 | 358 | false | ```\nclass Solution {\npublic:\n int minSwap(vector<int> &one, vector<int> &two)\n {\n int n = one.size();\n int swap = 1;\n int noSwap = 0;\n\n for (int i = 1; i < n; i++)\n {\n int oldSwap = swap;\n int oldNoSwap = noSwap;\n\n if (one[i] > one[i - 1] and one[i] > two[i - 1] and two[i] ... | 1 | 0 | ['Dynamic Programming', 'C'] | 1 |
minimum-swaps-to-make-sequences-increasing | [Java] DP Iterative solution | java-dp-iterative-solution-by-ailyasov-z6lp | DP Iterative solution\nFor each element in both arrays we should maintain the invariant:\nnums[i - 1] < nums[i]\n\nStates:\ndp[i][0] minimum number of swaps to | ailyasov | NORMAL | 2022-01-03T20:00:38.500777+00:00 | 2022-01-28T21:07:41.273920+00:00 | 243 | false | **DP Iterative solution**\nFor each element in both arrays we should maintain the invariant:\n`nums[i - 1] < nums[i]`\n\nStates:\n`dp[i][0]` minimum number of swaps to have [0:i] sorted and no swap made at i index\n`dp[i][1] ` minimum number of swaps to have [0:i] sorted and swap i index of both arrays\n\nFor each elem... | 1 | 0 | ['Dynamic Programming', 'Iterator', 'Java'] | 0 |
minimum-swaps-to-make-sequences-increasing | [C++] Dynamic programming with O(n) Time and O(n) Space | c-dynamic-programming-with-on-time-and-o-n69n | We easily see that the position of any element of the two provided arrays is fixed, either we choose to swap it or keep it as original. Thus, at any position th | trieutrng | NORMAL | 2021-11-24T09:07:38.478054+00:00 | 2021-11-24T10:45:35.123763+00:00 | 406 | false | We easily see that the position of any element of the two provided arrays is fixed, either we choose to swap it or keep it as original. Thus, at any position there are 2 states we can maintain, I call them **keep** and **swap** (keep if we choose to keep the elements as original, and swap is the opposite case).\n\nAt a... | 1 | 0 | ['Dynamic Programming', 'C'] | 0 |
minimum-swaps-to-make-sequences-increasing | Python DP solution O(n) Time and O(1) Space | python-dp-solution-on-time-and-o1-space-ffyxz | \nclass Solution:\n def minSwap(self, nums1: List[int], nums2: List[int]) -> int:\n dp_none_swapped = 0\n dp_swapped = 1\n n = len(nums1 | chang_liu | NORMAL | 2021-10-14T19:16:59.701693+00:00 | 2021-10-14T19:16:59.701741+00:00 | 226 | false | ```\nclass Solution:\n def minSwap(self, nums1: List[int], nums2: List[int]) -> int:\n dp_none_swapped = 0\n dp_swapped = 1\n n = len(nums1)\n for i in range(1, n):\n #First, if at i, we don\'t swap\n #Then, if at i, we swap \n new_none_swapped = n + n\n... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | [Java/C++/Python] Simple recursion and Follow-Up | javacpython-simple-recursion-and-follow-edk24 | Intuition\nThe first player colors a node,\nthere are at most 3 nodes connected to this node.\nIts left, its right and its parent.\nTake this 3 nodes as the roo | lee215 | NORMAL | 2019-08-04T04:02:55.997216+00:00 | 2019-10-22T17:23:33.627274+00:00 | 22,669 | false | # **Intuition**\nThe first player colors a node,\nthere are at most 3 nodes connected to this node.\nIts left, its right and its parent.\nTake this 3 nodes as the root of 3 subtrees.\n\nThe second player just color any one root,\nand the whole subtree will be his.\nAnd this is also all he can take,\nsince he cannot cro... | 317 | 4 | [] | 35 |
binary-tree-coloring-game | Easy to understand for everyone | easy-to-understand-for-everyone-by-mudin-nq27 | Count left and right children\'s nodes of the player 1\'s initial node with value x. Lets call countLeft and countRight.\n1. if countLeft or countRight are big | mudin | NORMAL | 2019-08-04T05:31:27.921010+00:00 | 2019-08-04T05:31:27.921065+00:00 | 8,700 | false | Count left and right children\'s nodes of the player 1\'s initial node with value `x`. Lets call `countLeft` and `countRight`.\n1. if `countLeft` or `countRight` are bigger than `n/2`, player 2 chooses this child of the node and will win.\n2. If `countLeft + countRight + 1` is smaller than `n/2`, player 2 chooses the ... | 123 | 1 | [] | 12 |
binary-tree-coloring-game | Simple Clean Java Solution | simple-clean-java-solution-by-ayyild1z-1wlo | Short explanation:\n\nWhen you find the selected node, there are three different paths you can block: left right parent In order to guarantee your win, one of t | ayyild1z | NORMAL | 2019-08-26T18:49:04.297051+00:00 | 2019-08-26T18:57:24.285030+00:00 | 3,054 | false | **Short explanation:**\n\nWhen you find the selected node, there are three different paths you can block: `left` `right` `parent` In order to guarantee your win, one of those paths should include more nodes than the sum of other two paths. \n\n.\n\n```java\npublic boolean btreeGameWinningMove(TreeNode root, int n, int ... | 83 | 0 | ['Java'] | 5 |
binary-tree-coloring-game | c++,0ms, modular, beats 100% (both time and memory) with algo and image | c0ms-modular-beats-100-both-time-and-mem-q64g | The second player will pick y as either left child, right child or parent (depending on which one has max nodes in their vicinity) of the node picked by first p | goelrishabh5 | NORMAL | 2019-08-04T04:51:06.711368+00:00 | 2019-08-04T05:23:19.484788+00:00 | 3,710 | false | The second player will pick y as either left child, right child or parent (depending on which one has max nodes in their vicinity) of the node picked by first player.\nIf the no of nodes available for second player is greater than first, he wins \nNote : Equal case will never arise since n is odd\n\nFor example : \n![i... | 67 | 2 | ['C', 'Binary Tree'] | 4 |
binary-tree-coloring-game | C++ count nodes in x subtree | c-count-nodes-in-x-subtree-by-votrubac-ngy7 | Intuition\nAfter the first player choose x node, the best options for the second player are:\n- Choose the parent of x. Second player will color all nodes outsi | votrubac | NORMAL | 2019-08-04T04:56:28.317478+00:00 | 2019-08-04T05:14:45.739827+00:00 | 2,128 | false | # Intuition\nAfter the first player choose ```x``` node, the best options for the second player are:\n- Choose the parent of ```x```. Second player will color all nodes outside ```x``` subtree.\n- Choose the left child of ```x```. Second player will color all nodes in the left child.\n- ... or the right child.\n# Solut... | 36 | 1 | [] | 1 |
binary-tree-coloring-game | Confusing problem statement | confusing-problem-statement-by-nice_dev-kdlp | Can anyone make me understand what does the problem statement mean? I looked at this example \n\n\nInput: root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3\nOutpu | nice_dev | NORMAL | 2019-08-04T09:02:35.094664+00:00 | 2019-08-04T09:02:35.094696+00:00 | 2,362 | false | Can anyone make me understand what does the problem statement mean? I looked at this example \n\n```\nInput: root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3\nOutput: true\nExplanation: The second player can choose the node with value 2.\n```\n\nWhy node with value `2`? Why not any other node? | 33 | 1 | [] | 6 |
binary-tree-coloring-game | Python 3 | DFS | One pass & Three pass | Explanation | python-3-dfs-one-pass-three-pass-explana-hhi7 | Intuition\n- When first player chose a node x, then there are 3 branches left there for second player to choose (left subtree of x, right subtree of x, parent e | idontknoooo | NORMAL | 2020-08-17T20:34:26.323020+00:00 | 2020-08-17T20:34:26.323065+00:00 | 1,184 | false | ### Intuition\n- When first player chose a node `x`, then there are 3 branches left there for second player to choose (left subtree of `x`, right subtree of `x`, parent end of `x`. \n- For the second player, to ensure a win, we need to make sure that there is one branch that dominate the sum of the other 2 branches. As... | 18 | 0 | ['Depth-First Search', 'Python', 'Python3'] | 2 |
binary-tree-coloring-game | C++ Easy To Understand Code with Explanation | c-easy-to-understand-code-with-explanati-p3at | Given that total nodes are n.\n###### For any node x, \n###### a.find the count of nodes in the left subtree \n###### b.and the count of nodes in the right subt | pratyush63 | NORMAL | 2020-05-07T11:33:03.956399+00:00 | 2020-05-07T11:34:01.301722+00:00 | 699 | false | ###### Given that total nodes are n.*\n###### For any node x, \n###### a.find the count of nodes in the left subtree \n###### b.and the count of nodes in the right subtree\n###### c.Count of nodes above node x(remaining tree) will be n-(a)-(b)-1\n###### If player 2 picks the right child, the right subtree is blocked fo... | 13 | 0 | [] | 2 |
binary-tree-coloring-game | Iterative BFS, python with my explanation | iterative-bfs-python-with-my-explanation-t5rj | There are three "zones" in the tree:\n1. Left subtree under Red\n2. Right subtree under Red\n3. The remainder of the tree "above" Red\n\nBlue can pick the left | bookra | NORMAL | 2020-01-04T04:49:14.940282+00:00 | 2020-01-04T04:49:14.940332+00:00 | 1,246 | false | There are three "zones" in the tree:\n1. Left subtree under Red\n2. Right subtree under Red\n3. The remainder of the tree "above" Red\n\nBlue can pick the left child, right child, or parent of Red to control zones 1, 2, or 3, respectivley.\n\nTherefore we count the number of nodes in two of the zones. The third zone is... | 11 | 0 | ['Breadth-First Search', 'Python3'] | 1 |
binary-tree-coloring-game | ✅ [C++] Easy Intuitive solution with Explanation | c-easy-intuitive-solution-with-explanati-jaqu | This question is easy once you figure out what to do. We need to check if we can win or not. Its simple. Just reduce the win chances of your opponent. \nHow do | biT_Legion | NORMAL | 2022-05-19T11:27:25.255112+00:00 | 2022-05-19T11:27:25.255155+00:00 | 1,135 | false | This question is easy once you figure out what to do. We need to check if we can win or not. **Its simple**. Just reduce the win chances of your opponent. \n***How do we do that?***\nWe will try to block the movement of the oppenent(Lets call him red) by choosing one of his neighbour. That way, he will have no choice a... | 10 | 1 | ['Depth-First Search', 'C', 'C++'] | 0 |
binary-tree-coloring-game | Python 3 || 12 lines, w/ explanation and example || T/S: 98% / 93% | python-3-12-lines-w-explanation-and-exam-g3q7 | The problem reduces to whether any of the three subgraphs with edges to node x have at least (n+1)//2 nodes.\n\nHere\'s the plan:\n- Traverse the tree with dfs | Spaulding_ | NORMAL | 2022-12-30T22:47:02.108506+00:00 | 2024-06-01T15:54:43.429995+00:00 | 761 | false | The problem reduces to whether any of the three subgraphs with edges to node `x` have at least `(n+1)//2` nodes.\n\nHere\'s the plan:\n- Traverse the tree with `dfs` recursively.\n- For each `node`, rewrite `node.val` with the count of nodes in its subtree.\n- Evaluate whether `node` is x, and if so, determine whether ... | 8 | 0 | ['Python3'] | 0 |
binary-tree-coloring-game | Simple recursive solution in C++ with explanation [0ms] | simple-recursive-solution-in-c-with-expl-kk94 | Because the game is played on a tree there are no cycles in the graph, and marking a node divides-off a part of the graph, making it forever inaccessible to the | ahcox | NORMAL | 2019-11-07T17:50:25.835479+00:00 | 2019-11-07T17:53:23.827868+00:00 | 548 | false | Because the game is played on a tree there are no cycles in the graph, and marking a node divides-off a part of the graph, making it forever inaccessible to the other player. Therefore, a first move which cuts off more of the tree than remains accessible to the other player is an automatic winning one. The best such mo... | 7 | 0 | ['Depth-First Search', 'Recursion', 'C', 'Binary Tree'] | 0 |
binary-tree-coloring-game | SIMPLE JAVA APPROACH WITH EXPLANATION | simple-java-approach-with-explanation-by-49fp | \t// time complexity O(N) , space complexity O(height)\n\t/ approach :\n\t\t\t--> as we are given the starting red color node(x), the only place to start color | the_moonLight | NORMAL | 2021-01-26T08:48:35.537604+00:00 | 2021-01-26T08:48:35.537643+00:00 | 304 | false | \t// time complexity O(N) , space complexity O(height)\n\t/* approach :\n\t\t\t--> as we are given the starting red color node(x), the only place to start coloring blue that ensures blue to win is to color any adjacent node of x to blue.\n\t\t\t--> if we color left child of x to blue then we can stop the red color pla... | 5 | 0 | [] | 0 |
binary-tree-coloring-game | Java solution beats 100% time: Break the problem down into two easier problems | java-solution-beats-100-time-break-the-p-5ew4 | \nclass Solution {\n int count;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n if (root == null)\n return false; | jasonhey93 | NORMAL | 2020-01-27T20:59:57.943745+00:00 | 2020-01-27T21:00:21.137142+00:00 | 399 | false | ```\nclass Solution {\n int count;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n if (root == null)\n return false;\n \n // find the node with value x\n TreeNode target = search(root, x);\n \n // separate the tree into three parts around... | 5 | 0 | ['Recursion', 'Java'] | 0 |
binary-tree-coloring-game | c++ 0ms easy solution !!!! | c-0ms-easy-solution-by-shivanshu0287-h574 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | shivanshu0287 | NORMAL | 2023-09-13T08:18:32.578821+00:00 | 2023-09-13T08:18:32.578843+00:00 | 265 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | 0 | ['C++'] | 1 |
binary-tree-coloring-game | [C++] Simplest - Recursive - 2 solutions. | c-simplest-recursive-2-solutions-by-user-kexn | \n\n\nThis is a game theory question so instead of just making moves, \'blue\' need to damage \'red\'. \nThe idea is that every node can have at max 3 neighbou | user2085X | NORMAL | 2022-04-14T11:08:48.323428+00:00 | 2022-04-14T11:08:48.323464+00:00 | 303 | false | \n\n\nThis is a game theory question so instead of just making moves, \'blue\' need to damage \'red\'. \nThe idea is that every node can have at max 3 neighbour nodes(1> parent, 2> left child, 3> right child),... | 4 | 0 | ['Backtracking', 'Depth-First Search', 'Recursion', 'Binary Tree'] | 0 |
binary-tree-coloring-game | JAVA | Explained | beginner friendly | java-explained-beginner-friendly-by-code-idvc | \n\t// When you find the selected node, there are three different paths you can block: left ,right or parent \n\t// if i color left(a) node blue, then red perso | codewizard27 | NORMAL | 2022-01-18T15:50:39.079836+00:00 | 2022-01-18T15:50:39.079877+00:00 | 398 | false | \n\t// When you find the selected node, there are three different paths you can block: left ,right or parent \n\t// if i color left(a) node blue, then red person can color all right and parent nodes red\n\t// if i color right (b) node blue, then red person can color all left and parent nodes red\n\t// if i color parent... | 4 | 0 | ['Java'] | 1 |
binary-tree-coloring-game | C++ || Easy Solution | c-easy-solution-by-shubham_0221-pbuk | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | shubham_0221 | NORMAL | 2023-02-20T11:41:44.260591+00:00 | 2023-02-20T11:41:44.260641+00:00 | 455 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
binary-tree-coloring-game | 0ms | BFS | Diagram | Tree as Graph | Explained | 0ms-bfs-diagram-tree-as-graph-explained-8un2t | This is a standard simulation problem. You don\'t need to think of all the cases of expansion just deal with the stopping case, i.e. when both red and blue are | aryanjofficial57 | NORMAL | 2022-10-07T08:35:10.121223+00:00 | 2022-10-07T08:35:10.121257+00:00 | 251 | false | This is a standard simulation problem. You don\'t need to think of all the cases of *expansion* just deal with the **stopping case**, i.e. when both **red** and **blue** are fully expanded leaving no **uncolored** node.\n\n*The key is to imagine this **tree as graph** with parent and childrens as adjacent neighbors *\n... | 3 | 0 | ['Tree', 'Breadth-First Search', 'Graph'] | 1 |
binary-tree-coloring-game | C++ Easy To Understand Solution | Recursion | Node Count | c-easy-to-understand-solution-recursion-hz6mv | \n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0) | abhinandan__22 | NORMAL | 2022-03-02T19:43:48.481941+00:00 | 2022-03-02T19:43:48.481988+00:00 | 260 | false | ```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right... | 3 | 0 | ['C'] | 0 |
binary-tree-coloring-game | C++ Recursion Solution | c-recursion-solution-by-ahsan83-bvkj | Runtime: 4 ms, faster than 82.92% of C++ online submissions for Binary Tree Coloring Game.\nMemory Usage: 11.6 MB, less than 5.05% of C++ online submissions for | ahsan83 | NORMAL | 2020-10-31T01:57:57.458205+00:00 | 2020-10-31T02:00:12.907321+00:00 | 207 | false | Runtime: 4 ms, faster than 82.92% of C++ online submissions for Binary Tree Coloring Game.\nMemory Usage: 11.6 MB, less than 5.05% of C++ online submissions for Binary Tree Coloring Game.\n\nPlayer 1 chooses Node x first which has 3 neighbor nodes (parent, left, right). \nPlayer 2 will choose one of those 3 neighbor no... | 3 | 0 | ['Depth-First Search', 'Recursion', 'C'] | 0 |
binary-tree-coloring-game | [C++] beats 100% in time and memory [Detailed Explanantion] | c-beats-100-in-time-and-memory-detailed-z7mt7 | \n/*\n https://leetcode.com/problems/binary-tree-coloring-game/\n \n Idea is to find the total nodes in the left, right and before node x. Then the sec | cryptx_ | NORMAL | 2020-01-13T17:01:37.433000+00:00 | 2020-01-13T17:01:37.433046+00:00 | 285 | false | ```\n/*\n https://leetcode.com/problems/binary-tree-coloring-game/\n \n Idea is to find the total nodes in the left, right and before node x. Then the second player can just\n choose to color the branch with majority nodes(> N/2). If such a node is there then he can win, otherwise not possible.\n \n T... | 3 | 0 | [] | 0 |
binary-tree-coloring-game | Java. 100% faster. Easy to understand | java-100-faster-easy-to-understand-by-mc-sho7 | \nclass Solution {\n \n private int countOpenNodes(TreeNode n, int selected) {\n if(n == null || n.val == selected) { return 0; }\n \n | mc1234 | NORMAL | 2019-09-11T19:10:07.111999+00:00 | 2019-09-11T19:10:07.112049+00:00 | 609 | false | ```\nclass Solution {\n \n private int countOpenNodes(TreeNode n, int selected) {\n if(n == null || n.val == selected) { return 0; }\n \n return 1 + countOpenNodes(n.left, selected) + countOpenNodes(n.right, selected);\n }\n \n private TreeNode findNode(TreeNode n, int val) {\n ... | 3 | 0 | [] | 2 |
binary-tree-coloring-game | Java O(N) | 100% Faster Solution | java-on-100-faster-solution-by-tbekpro-d0lr | Complexity\n- Time complexity: O(N)\n\n# Code\n\nclass Solution {\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n TreeNode[] re | tbekpro | NORMAL | 2023-12-15T14:03:12.195467+00:00 | 2023-12-16T23:15:03.548545+00:00 | 492 | false | # Complexity\n- Time complexity: O(N)\n\n# Code\n```\nclass Solution {\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n TreeNode[] red = new TreeNode[1];\n findNode(root, x, red);\n int[] countLeft = new int[1], countRight = new int[1];\n countNodes(red[0].left, coun... | 2 | 0 | ['Java'] | 0 |
binary-tree-coloring-game | Java 0ms - DFS | java-0ms-dfs-by-_sikarwar-zg79 | \n Describe your first thoughts on how to solve this problem. \n\n Descre your approach to solving the problem. \n\n# Complexity\n- Time complexity: O(n)\n Add | _sikarwar_ | NORMAL | 2022-12-27T13:15:28.444240+00:00 | 2022-12-27T13:15:28.444270+00:00 | 836 | false | \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- Descre your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definitio... | 2 | 0 | ['Tree', 'Depth-First Search', 'Binary Tree', 'Java'] | 0 |
binary-tree-coloring-game | Python, DFS | python-dfs-by-swepln-q35h | Intuition\nWe have 3 different ways to choose the element that will beat opponent.\n1. Choose left sub-tree of opponent node\n2. Choose right sub-tree of oppone | swepln | NORMAL | 2022-11-20T01:17:19.036397+00:00 | 2022-11-20T01:17:19.036430+00:00 | 396 | false | # Intuition\nWe have 3 different ways to choose the element that will beat opponent.\n1. Choose left sub-tree of opponent node\n2. Choose right sub-tree of opponent node\n3. Choose parent of opponent node\n\nThen we should calculate amount of nodes in left and right sub-trees. If one of them grater than sum of others -... | 2 | 0 | ['Python3'] | 0 |
binary-tree-coloring-game | Simple java solution with basic intuition | simple-java-solution-with-basic-intuitio-7po6 | If you carefully notice then you will observe that whichever player(player 1 or player 2) color more than half nodes will win the game, this is basic intuition | 404_coder | NORMAL | 2021-06-07T09:57:01.179882+00:00 | 2021-06-07T09:57:01.179911+00:00 | 132 | false | If you carefully notice then you will observe that whichever player(player 1 or player 2) color more than half nodes will win the game, this is basic intuition of this problem. Now given the node choosen by player 1, player 2 can choose a node either from left or from right child or it can choose parent of given node. ... | 2 | 0 | [] | 0 |
binary-tree-coloring-game | Simple python O(n) solution that beats 100% | simple-python-on-solution-that-beats-100-i075 | We need to count tree sizes of children of x node. \nThen the tree size of the rest of the tree could be calculated directly from these findings.\nx_parent = n | avakatushka | NORMAL | 2020-12-05T10:47:42.254229+00:00 | 2020-12-05T10:47:42.254263+00:00 | 126 | false | We need to count **tree sizes** of children of x node. \nThen the tree **size of the rest of the tree** could be calculated directly from these findings.\nx_parent = n - x_left - x_right - 1\nIf any of the tree sizes gets the majority, it\'s possible for us to win:\ncapacity > n - capacity \n```\ndef btreeGameWinningMo... | 2 | 0 | [] | 0 |
binary-tree-coloring-game | Explained | C++ | explained-c-by-harshjoeyit-wsxx | Approach\n1. It is always best case for use if either choose y \n\na) left child of x\n\tthis case works when number of nodes in left subtree of x are > n/2\n\n | harshjoeyit | NORMAL | 2020-08-21T14:04:24.565810+00:00 | 2020-08-21T14:05:03.834579+00:00 | 188 | false | > Approach\n1. It is always best case for use if either choose y \n\na) left child of x\n\tthis case works when number of nodes in left subtree of x are > n/2\n\nb) right chilf of x\n\tthis case works when number of nodes in right subtree of x are > n/2\n\nc) parent of x\n\tthis case works when number in nodes in tree ... | 2 | 0 | [] | 0 |
binary-tree-coloring-game | Java Recursion | java-recursion-by-kylinzhuo-i8fo | \n\n\nLet\'s assume the first player took Node 3 in the above tree. \n\nThere are 3 areas to consider: \n- {Node 3\'s left sub tree} (area 1)\n- {Node 3\'s rig | kylinzhuo | NORMAL | 2020-04-04T04:55:13.036821+00:00 | 2020-04-13T07:12:00.138082+00:00 | 208 | false | \n\n\nLet\'s assume the first player took Node 3 in the above tree. \n\nThere are 3 areas to consider: \n- {Node 3\'s left sub tree} (area 1)\n- {Node 3\'s right sub tree} (area 2)\n- {whole tree - subtree with Node 3 as the root} (area 3)\n\nI... | 2 | 0 | [] | 2 |
binary-tree-coloring-game | Beats 100% 100% - With Explanation - Java | beats-100-100-with-explanation-java-by-b-qo7j | \nidea is simple,\nsecond player has 3 options to block player one,\n1. Block moving to its left child\n2. Block moving to its right child\n3. Block moving to i | bkatwal | NORMAL | 2020-02-03T11:59:20.339917+00:00 | 2020-02-03T12:00:13.425923+00:00 | 187 | false | \nidea is simple,\nsecond player has 3 options to block player one,\n1. Block moving to its left child\n2. Block moving to its right child\n3. Block moving to its parent\n\nAs player one started first the win is only possible if you have more than n/2 nodes in either one of them, beacuse player one can chose again afte... | 2 | 0 | [] | 0 |
binary-tree-coloring-game | Java Solution with explanation | java-solution-with-explanation-by-pulkit-h2s8 | My approach is a bit lengthy.\nI broke down the problem into 2 subcases.\n\nCASE 1\nWe calcaulate the number of nodes in the subtree rooted at X, and if the num | pulkitjainn | NORMAL | 2019-09-12T06:37:08.133765+00:00 | 2019-09-12T06:39:31.088842+00:00 | 415 | false | My approach is a bit lengthy.\nI broke down the problem into 2 subcases.\n\n**CASE 1**\nWe calcaulate the number of nodes in the subtree rooted at `X`, and if the number of nodes in in this subtree is greater then the count of rest of the nodes in the tree we return false.\n\n**CASE 2**\nIn this approach, we calcaculat... | 2 | 1 | [] | 0 |
binary-tree-coloring-game | python | python-by-cybernanana-8v2k | \nclass Solution(object):\n def btreeGameWinningMove(self, root, n, x):\n """\n :type root: TreeNode\n :type n: int\n :type x: in | cybernanana | NORMAL | 2019-08-14T02:10:07.675193+00:00 | 2019-08-14T02:10:07.675227+00:00 | 277 | false | ```\nclass Solution(object):\n def btreeGameWinningMove(self, root, n, x):\n """\n :type root: TreeNode\n :type n: int\n :type x: int\n :rtype: bool\n """\n self.left = None\n self.right = None\n self.helper(root, x)\n parent = n - 1 - self.left -... | 2 | 0 | ['Python3'] | 0 |
binary-tree-coloring-game | [C++] Simple strategy (and some DFS as an afterthought) | c-simple-strategy-and-some-dfs-as-an-aft-6fqt | The optimal strategy is to make y either the parent of x, or a child. Any other choice is suboptimal, as x could always first grow their territory towards you, | mhelvens | NORMAL | 2019-08-04T10:23:24.360101+00:00 | 2019-08-04T10:23:24.360131+00:00 | 154 | false | The optimal strategy is to make `y` either the parent of `x`, or a child. Any other choice is suboptimal, as `x` could always first grow their territory _towards_ you, stealing points that you would otherwise have blocked off.\n\n* If `y` is a child, it gets the number of nodes in that sub-tree, and `x` gets all other ... | 2 | 1 | ['Depth-First Search', 'C'] | 0 |
binary-tree-coloring-game | Simple Cpp solution passes all test cases | simple-cpp-solution-passes-all-test-case-2clr | When the first player choses a node there are three options available for the second player.\nHe can either choose the parent or left child or right child. By d | player007 | NORMAL | 2019-08-04T04:30:30.552459+00:00 | 2019-08-04T14:38:26.140583+00:00 | 118 | false | When the first player choses a node there are three options available for the second player.\nHe can either choose the parent or left child or right child. By doing so he is blocking the chosen path for the first player as in a tree there is only one path between two nodes.\nSo what we can do is to chose the node out o... | 2 | 0 | [] | 0 |
binary-tree-coloring-game | Simple Python, Easy to understand | simple-python-easy-to-understand-by-davy-jtm8 | The node selected by the first person divides the tree by three parts: left, right, parent. We can only choose one region.\n\nclass Solution:\n def btreeGame | davyjing | NORMAL | 2019-08-04T04:06:53.470328+00:00 | 2019-08-04T04:07:07.714527+00:00 | 210 | false | The node selected by the first person divides the tree by three parts: left, right, parent. We can only choose one region.\n```\nclass Solution:\n def btreeGameWinningMove(self, root: TreeNode, n: int, x: int) -> bool:\n self.loc = None\n def dfs(node):\n if not node:\n return... | 2 | 2 | [] | 0 |
binary-tree-coloring-game | Python based on left, right and above | python-based-on-left-right-and-above-by-cxjrm | The first step is to calculate the size of the subtree rooted at each node.\nOnce we have this info, it boils down to we as Player 2 choosing the node, that is | Cubicon | NORMAL | 2019-08-04T04:02:46.019858+00:00 | 2019-08-04T04:09:49.199920+00:00 | 234 | false | The first step is to calculate the size of the subtree rooted at each node.\nOnce we have this info, it boils down to we as Player 2 choosing the node, that is either parent of X, or left child of X or right child of X.\n\nLets definte "Above" = number of nodes that are not in my subtree.\n\nIf we choose the parent of... | 2 | 2 | [] | 1 |
binary-tree-coloring-game | Java Solution | java-solution-by-zqin9-wkaf | \n\t// value of left node of x.\n int left = 0;\n // value of right node of x.\n int right = 0;\n public boolean btreeGameWinningMove(TreeNode root, | zqin9 | NORMAL | 2019-08-04T04:02:39.766601+00:00 | 2019-08-04T04:05:14.307477+00:00 | 234 | false | ```\n\t// value of left node of x.\n int left = 0;\n // value of right node of x.\n int right = 0;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n if (root.left == null && root.right == null) {\n return false;\n }\n int[] numOfChildren = new int[n];\n ... | 2 | 1 | [] | 0 |
binary-tree-coloring-game | C++ O(N) recursive | c-on-recursive-by-murkyautomata-y1sl | Optimal strategy is choosing a node adjacent to your opponents to claim the region beyond it.\n\nclass Solution {\npublic:\n int nLeft, nRight, nAbove, y;\n | murkyautomata | NORMAL | 2019-08-04T04:02:37.756648+00:00 | 2019-08-04T04:02:37.756687+00:00 | 248 | false | Optimal strategy is choosing a node adjacent to your opponents to claim the region beyond it.\n```\nclass Solution {\npublic:\n int nLeft, nRight, nAbove, y;\n bool btreeGameWinningMove(TreeNode* root, int n, int x) {\n nLeft=0, nRight=0, nAbove=0, y=x;\n count(root);\n return (\n ... | 2 | 1 | [] | 0 |
binary-tree-coloring-game | simple JAVA O(n) solution with explanation | simple-java-on-solution-with-explanation-nc7d | Given a red node the best chance we can win is to blue-color its parent or either of its children.\nWhy?\nBecause by doing so we block the first player in one o | may6 | NORMAL | 2019-08-04T04:01:18.623591+00:00 | 2019-08-04T04:35:56.350279+00:00 | 361 | false | Given a red node the best chance we can win is to blue-color its parent or either of its children.\nWhy?\nBecause by doing so we block the first player in one of the three directions. We hence take all the rest of the nodes as our own. \nAny other options can\'t beat these three. A simple proof of condradiction can pro... | 2 | 1 | [] | 1 |
binary-tree-coloring-game | Python - Recursive Solution ✅ | python-recursive-solution-by-itsarvindhe-wiad | The idea is to keep track of the parent nodes of each node first. That will be required when we have to traverse towards the top of a node.\n\nThen, we will che | itsarvindhere | NORMAL | 2024-06-05T05:24:59.383142+00:00 | 2024-06-05T05:26:40.257844+00:00 | 111 | false | The idea is to keep track of the parent nodes of each node first. That will be required when we have to traverse towards the top of a node.\n\nThen, we will check how many nodes each subtree has which has root as the node which is the neighbor of the node that "x" selects initially.\n\n\n and you can block it\'s any one direction and you have ... | 1 | 0 | ['C++'] | 2 |
binary-tree-coloring-game | Boring ||Easy||beats 100% ,0ms || left, right, parent | boring-easybeats-100-0ms-left-right-pare-7wxk | Intuition\n Describe your first thoughts on how to solve this problem. \njust 3 ways\npick leftsubtree\npick rightsubtree\npick parent node\n# Approach\n Descri | vikas_dor | NORMAL | 2023-11-29T09:07:38.525633+00:00 | 2023-11-29T09:07:38.525662+00:00 | 216 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\njust 3 ways\npick leftsubtree\npick rightsubtree\npick parent node\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n- ... | 1 | 0 | ['Binary Tree', 'C++'] | 0 |
binary-tree-coloring-game | simple python dfs beats 98% solution | simple-python-dfs-beats-98-solution-by-p-7is8 | We first build a undirectd acyclic graph \nAs player 1 choose a node first, the only chance for player 2 to win is to choose a children of node with value x , w | Pseudo_intelligent | NORMAL | 2023-01-16T00:52:02.562754+00:00 | 2023-01-16T00:52:02.562784+00:00 | 119 | false | ***We first build a undirectd acyclic graph \n*As player 1 choose a node first, the only chance for player 2 to win is to choose a children of node with value x , which are \'chances\' . We always choose node next to player 1\'s node, major reason is only \none edge between 2 nodes, this act like a \'separator\'.\n We ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | C++ : 0 ms, faster than 100.00% of C++ online submissions for Binary Tree Coloring Game. | c-0-ms-faster-than-10000-of-c-online-sub-h7cm | \nclass Solution {\npublic:\n TreeNode *chosen = NULL;\n \n bool btreeGameWinningMove(TreeNode* root, int n, int x) {\n preorder(root , x);\n | sonud | NORMAL | 2022-08-16T05:09:21.329092+00:00 | 2022-08-16T05:09:21.329139+00:00 | 327 | false | ```\nclass Solution {\npublic:\n TreeNode *chosen = NULL;\n \n bool btreeGameWinningMove(TreeNode* root, int n, int x) {\n preorder(root , x);\n \n int leftnodes = countNodes(chosen -> left);\n int rightnodes = countNodes(chosen -> right);\n int upNodes = n - (leftnodes + rig... | 1 | 0 | ['Depth-First Search', 'C'] | 0 |
binary-tree-coloring-game | C++ solution 100% better Using Recursion | c-solution-100-better-using-recursion-by-05rk | class Solution {\npublic:\n\n int total_descent(TreeNode root)\n {\n \n if(root==NULL)\n {\n return 0;\n }\n els | dippatel11 | NORMAL | 2022-07-15T09:13:15.323846+00:00 | 2022-07-15T09:13:15.323889+00:00 | 129 | false | class Solution {\npublic:\n\n int total_descent(TreeNode* root)\n {\n \n if(root==NULL)\n {\n return 0;\n }\n else if(root->left==NULL&&root->right==NULL)\n {\n return 1;\n }\n else\n {\n int ans=0;\n ans+=total... | 1 | 0 | [] | 1 |
binary-tree-coloring-game | A java starightforward and stepwise solution(comments included) || 0ms | a-java-starightforward-and-stepwise-solu-xybe | class Solution {\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n TreeNode red=getNode(root,x); //getting the x node\n | knuckleHEADd | NORMAL | 2022-07-10T08:49:00.926370+00:00 | 2022-07-10T17:26:37.826639+00:00 | 206 | false | ```class Solution {\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n TreeNode red=getNode(root,x); //getting the x node\n int countPar=getCount(root,red); //count if I set blue as parent of red\n int countLeft=getCount(red.left,red); //count if I set blue as leftChi... | 1 | 0 | ['Binary Tree', 'Java'] | 0 |
binary-tree-coloring-game | PYTHON SOL | LINEAR TIME | EXPLAINED WITH PICTURE | EASY | TRAVERSING | | python-sol-linear-time-explained-with-pi-xyhm | TIME AND SPACE COMPLEXITY\nRuntime: 36 ms, faster than 89.14% of Python3 online submissions for Binary Tree Coloring Game.\nMemory Usage: 14 MB, less than 52.72 | reaper_27 | NORMAL | 2022-07-03T12:18:14.577734+00:00 | 2022-07-03T12:18:14.577767+00:00 | 212 | false | # TIME AND SPACE COMPLEXITY\nRuntime: 36 ms, faster than 89.14% of Python3 online submissions for Binary Tree Coloring Game.\nMemory Usage: 14 MB, less than 52.72% of Python3 online submissions for Binary Tree Coloring Game.\n\n\n# EXPLANATION\n{\n if(root==NULL)return 0;\n return rec(root->left)+rec(root->right)+1;\n }\n \n void fi | ishanudr | NORMAL | 2022-06-13T07:48:39.690555+00:00 | 2022-06-13T07:48:39.690585+00:00 | 115 | false | ```TreeNode* tg=NULL;\n int rec(TreeNode* root){\n if(root==NULL)return 0;\n return rec(root->left)+rec(root->right)+1;\n }\n \n void fi(TreeNode* root,int x){\n if(root==NULL)return;\n if(root->val==x){\n tg=root;\n return;\n }\n fi(root->righ... | 1 | 1 | ['Recursion', 'C'] | 0 |
binary-tree-coloring-game | Java Binary Tree | java-binary-tree-by-manishen-gj85 | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n | Manishen | NORMAL | 2022-06-09T15:12:38.820410+00:00 | 2022-06-09T15:12:38.820452+00:00 | 213 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = l... | 1 | 0 | ['Binary Tree', 'Java'] | 0 |
binary-tree-coloring-game | JAVA AC solution || 0ms | java-ac-solution-0ms-by-priyansh007-pzfx | intuition that i got is if the player x already took his chance we have to greedily choose max of (left subtree of x,right subtree of x,other subtree)\nas we ch | Priyansh007 | NORMAL | 2022-05-26T15:49:32.888478+00:00 | 2022-05-26T15:49:32.888515+00:00 | 64 | false | intuition that i got is if the player x already took his chance we have to greedily choose max of (left subtree of x,right subtree of x,other subtree)\nas we choose this then if the other size is still greater than our choosed size we cant win\nelse we can win\n```\nclass Solution {\n int xRightSubtreeSum=0;\n in... | 1 | 0 | ['Recursion', 'Game Theory', 'Java'] | 0 |
binary-tree-coloring-game | Simple Java Solution using DFS | Easy to Understand | simple-java-solution-using-dfs-easy-to-u-gdy3 | Please upvote, if you find it useful :)\n\n\nclass Solution {\n \n public int xleft;\n public int xright;\n \n public int size(TreeNode node, int | kxbro | NORMAL | 2022-05-18T17:30:53.197060+00:00 | 2022-05-18T17:30:53.197105+00:00 | 201 | false | Please upvote, if you find it useful :)\n\n```\nclass Solution {\n \n public int xleft;\n public int xright;\n \n public int size(TreeNode node, int x) {\n if(node == null) {\n return 0;\n }\n \n int ls = size(node.left, x);\n int rs = size(node.right, x);\n ... | 1 | 0 | ['Depth-First Search', 'Java'] | 1 |
binary-tree-coloring-game | Python easy solution (DFS) | python-easy-solution-dfs-by-ganyue246-40v4 | \n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.lef | ganyue246 | NORMAL | 2022-03-31T01:58:47.294749+00:00 | 2022-03-31T01:58:47.294800+00:00 | 145 | false | ```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def btreeGameWinningMove(self, root: Optional[TreeNode], n: int, x: int) -> bool:\n \n ... | 1 | 0 | ['Python', 'Python3'] | 0 |
binary-tree-coloring-game | C++ | DFS | Commented | c-dfs-commented-by-singhabhinavv-vjgs | \n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0) | singhabhinavv | NORMAL | 2022-03-23T06:07:53.934643+00:00 | 2022-03-23T06:07:53.934686+00:00 | 88 | false | ```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right... | 1 | 0 | ['Binary Tree'] | 0 |
binary-tree-coloring-game | python | python-by-hzhaoc-n2gf | if x is root, it cuts tree into two parts: left child, right child. Only when the two parts are same num of nodes, player 2 will never win\n- if x is not root, | hzhaoc | NORMAL | 2022-01-06T04:07:56.595277+00:00 | 2022-01-06T04:07:56.595322+00:00 | 46 | false | - if x is root, it cuts tree into two parts: left child, right child. Only when the two parts are same num of nodes, player 2 will never win\n- if x is not root, it cuts tree into three parts: left child, right child, parent. Only when the largest part of three are more than half of total nodes, player 2 will win.\n\n`... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | py3: binary tree coloring game | py3-binary-tree-coloring-game-by-snalli-ra40 | \n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.lef | snalli | NORMAL | 2021-12-12T21:58:07.346704+00:00 | 2021-12-12T22:00:12.278660+00:00 | 64 | false | ```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def btreeGameWinningMove(self, root: Optional[TreeNode], n: int, x: int) -> bool:\n \n ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | C++ | Faster than 100% | Winning Strategy | c-faster-than-100-winning-strategy-by-ap-ihau | Essentially, the (possible) winning strategy is to choose a neighbor node to color \'BLUE\'. This will block the \'RED\' player from moving into the part of the | apartida | NORMAL | 2021-11-03T17:53:31.203662+00:00 | 2021-11-03T17:53:31.203692+00:00 | 128 | false | Essentially, the (possible) winning strategy is to choose a neighbor node to color \'BLUE\'. This will block the \'RED\' player from moving into the part of the tree that has the most nodes. To this, the optimal \'BLUE\' (neighbor) node is the one that contains the most nodes. \n\n```\n /* Pointer to \'RED\' node. *... | 1 | 0 | ['Depth-First Search', 'C'] | 0 |
binary-tree-coloring-game | C++ beat 100%, explained | c-beat-100-explained-by-wzypangpang-m88c | The trick here is in order to optimally choose y, there are only 3 options\n1) parent node of x\n2) left child of x\n3) right child of x\n\nNote that once we ch | wzypangpang | NORMAL | 2021-09-18T19:20:16.091236+00:00 | 2021-09-18T19:20:16.091277+00:00 | 141 | false | The trick here is in order to optimally choose y, there are only 3 options\n1) parent node of x\n2) left child of x\n3) right child of x\n\nNote that once we choose y, the tree is "split" into 2 subtrees (you can think of cutting the edge connecting x and y).\nthe following actions of 2 players are constrained within t... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | C++ || DFS || Neat and Simple Code | c-dfs-neat-and-simple-code-by-dharineesh-qm5v | \nclass Solution {\npublic:\n bool btreeGameWinningMove(TreeNode* root, int n, int x) {\n TreeNode* xNode = findNode(root, x);\n int leftCount | Dharineesh | NORMAL | 2021-07-31T09:01:30.829595+00:00 | 2021-07-31T09:01:30.829635+00:00 | 187 | false | ```\nclass Solution {\npublic:\n bool btreeGameWinningMove(TreeNode* root, int n, int x) {\n TreeNode* xNode = findNode(root, x);\n int leftCount = countNodes(xNode->left);\n int rightCount = countNodes(xNode->right);\n int parentCount = n - leftCount - rightCount - 1;\n \n ... | 1 | 0 | ['Depth-First Search', 'Recursion', 'C'] | 0 |
binary-tree-coloring-game | C++ 100% faster | c-100-faster-by-mohammeddeifallah-50qu | Just counting the nodes in the subtree rooted at x.\n\n/**\n * Definition for a binary tree node.\n * * struct TreeNode {\n * int val;\n * TreeNode *le | mohammeddeifallah | NORMAL | 2021-07-29T21:47:59.466628+00:00 | 2021-07-29T21:48:25.925583+00:00 | 133 | false | Just counting the nodes in the subtree rooted at `x`.\n```\n/**\n * Definition for a binary tree node.\n * * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) ... | 1 | 0 | ['Breadth-First Search', 'Recursion', 'Queue', 'C'] | 0 |
binary-tree-coloring-game | Easy Simple Java solution with Approach || 100% fast | easy-simple-java-solution-with-approach-bwy5x | Approach \nEveryone loves winning Therefore in this game we also want to win so for that we will block the first player\'s right subtree or left subtree or pare | amitverma1050 | NORMAL | 2021-07-04T18:31:08.770483+00:00 | 2021-07-04T18:32:51.789210+00:00 | 139 | false | **Approach** \nEveryone loves winning Therefore in this game we also want to win so for that we will block the first player\'s right subtree or left subtree or parent subtree on the basis of most nodes in subtree .\ncount1 -> we find the count of left subtree\ncount2 -> we find the count of right subtree\nn-count1-coun... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | C++| Java | 100% Faster | 0 ms | Easy Solution | c-java-100-faster-0-ms-easy-solution-by-j506q | C++\n### \n\nExplanation:\n\nThe node with value x divides the tree in three parts, parent region, left child region and right child region. If number of nodes | kush_yadav | NORMAL | 2021-06-09T19:12:16.398036+00:00 | 2021-11-06T07:11:57.850623+00:00 | 190 | false | ### **C++**\n### \n\n**Explanation:**\n\nThe node with value x divides the tree in three parts, parent region, left child region and right child region. If number of nodes in any of those regions is more that n/2 then y can win.\n\n```\nclass Solution {\n //This node will store the node with value X\n TreeNode* X... | 1 | 0 | ['C', 'Java'] | 0 |
binary-tree-coloring-game | VERY EASY 0 MS SOLUTION | very-easy-0-ms-solution-by-xxsidxx-wdnt | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n | xxsidxx | NORMAL | 2021-05-11T13:50:49.368243+00:00 | 2021-05-11T13:50:49.368291+00:00 | 72 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = l... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Java: Easy solution | java-easy-solution-by-abhi192-q77v | ```\n// three positions are candidates of y\n// case 1: left child of x has greater number of elements\n//case 2: right child of x has greater number of element | abhi192 | NORMAL | 2021-04-26T10:36:23.657213+00:00 | 2021-04-26T10:36:23.657259+00:00 | 140 | false | ```\n// three positions are candidates of y\n// case 1: left child of x has greater number of elements\n//case 2: right child of x has greater number of elements\n// case 3: parent has greater number of elements \n\nclass Solution {\n int xlc,xrc; \n public boolean btreeGameWinningMove(TreeNode root, int n, int ... | 1 | 0 | ['Java'] | 0 |
binary-tree-coloring-game | Python dfs iteration + recursion w/comments | python-dfs-iteration-recursion-wcomments-utsa | py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.l | leovam | NORMAL | 2021-02-28T01:50:57.112302+00:00 | 2021-02-28T01:50:57.112328+00:00 | 153 | false | ```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\n\'\'\'\nw: tree traversal + greedy\nh: the node x partiton the tree into three parts:\n 1: its left subtree\n ... | 1 | 0 | ['Depth-First Search', 'Python'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.