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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
stone-game-v | Beats 60.00%of users with JavaScript | beats-6000of-users-with-javascript-by-ss-hvj9 | \n# Code\n\nvar stoneGameV = function(stoneValue) {\n let n = stoneValue.length;\n const pre = new Array(n + 1).fill(0);\n for (let i = 1; i <= n; i++) {\n | SSDeepakReddy | NORMAL | 2023-10-14T05:32:22.145488+00:00 | 2023-10-14T05:32:22.145514+00:00 | 56 | false | \n# Code\n```\nvar stoneGameV = function(stoneValue) {\n let n = stoneValue.length;\n const pre = new Array(n + 1).fill(0);\n for (let i = 1; i <= n; i++) {\n pre[i] = pre[i - 1] + stoneValue[i - 1];\n }\n const dp = [...Array(n).fill(null)].map((_) => new Array(n).fill(0));\n for (let l = 1; l < n; l++) {\n ... | 1 | 0 | ['JavaScript'] | 0 |
stone-game-v | Basic Java Solution(Commented) | basic-java-solutioncommented-by-karthik_-r5nd | 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 | Karthik_1512 | NORMAL | 2023-09-16T17:16:11.484621+00:00 | 2023-09-16T17:16:11.484644+00:00 | 140 | 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 | ['Dynamic Programming', 'Recursion', 'Memoization', 'Prefix Sum', 'Java'] | 1 |
stone-game-v | Python O(n^2) DP Solution, faster than 100%(1399ms) | python-on2-dp-solution-faster-than-10013-cf6a | \n\'\'\'\nSuppose we know the k\' for stones[i..j], what do we know about k\' for stones[i..j+1]? It is either the same or it got shifted a few places to the ri | hemantdhamija | NORMAL | 2022-10-16T07:53:06.184473+00:00 | 2022-10-16T07:53:06.184514+00:00 | 846 | false | ```\n\'\'\'\nSuppose we know the k\' for stones[i..j], what do we know about k\' for stones[i..j+1]? It is either the same or it got shifted a few places to the right.\nAnd so if we calculate dp values in the order: dp[i][i], dp[i][i+1], dp[i][i+2], ..., dp[i][j], we can essentially keep track of k\' as we go within th... | 1 | 1 | ['Dynamic Programming', 'Memoization', 'Python', 'Python3'] | 0 |
stone-game-v | Simple Partition DP | Recursive | Memoization | simple-partition-dp-recursive-memoizatio-2vj5 | We need to try out all possible partition for every subproblem hence partition dp.\n\tBase Case: When there is only one element left in subarray (i.e i == j) we | lrrajput2001 | NORMAL | 2022-09-08T13:05:06.769420+00:00 | 2022-09-08T13:05:50.037944+00:00 | 395 | false | We need to try out all possible partition for every subproblem hence **partition dp**.\n\t**Base Case:** When there is only one element left in subarray (i.e i == j) we return 0 because alice cannot score anything from that.\n```\nint f(int i,int j,vector<int> &stoneValue,vector<vector<int>> &dp){\n if(i == j... | 1 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'C'] | 0 |
stone-game-v | c++ | c-by-rony_the_loser-iqt7 | (```) class Solution {\npublic:\n \n int dp[501][501];\n \n int help(vector& v, int start, int end)\n {\n if(start > end) \n { | Algo_wizard2020 | NORMAL | 2022-08-27T18:38:41.015707+00:00 | 2022-08-27T18:38:41.015740+00:00 | 37 | false | (```) class Solution {\npublic:\n \n int dp[501][501];\n \n int help(vector<int>& v, int start, int end)\n {\n if(start > end) \n {\n return 0;\n }\n \n int sum = 0, left_sum = 0, right_sum =0,ans = 0;\n \n if(dp[start][end] != -1)\n ... | 1 | 0 | [] | 0 |
stone-game-v | [C++] || DP || Check Every possible Partition | c-dp-check-every-possible-partition-by-r-gmm6 | \nclass Solution {\npublic:\n int dp[501][501] ;\n int solve(vector<int>&nums , int s , int e){\n if(s == e) return 0 ; \n if(dp[s][e] != -1 | rahul921 | NORMAL | 2022-07-13T13:27:01.106070+00:00 | 2022-07-13T13:36:17.826639+00:00 | 181 | false | ```\nclass Solution {\npublic:\n int dp[501][501] ;\n int solve(vector<int>&nums , int s , int e){\n if(s == e) return 0 ; \n if(dp[s][e] != -1) return dp[s][e] ;\n \n int sum = 0 , left = 0 , right = 0 ;\n for(int i = s ; i <= e ; ++i) sum += nums[i] ;\n \n int op... | 1 | 0 | ['C'] | 0 |
stone-game-v | Worst case time complexity when using recursion without DP | worst-case-time-complexity-when-using-re-o0nl | I was able to solve this problem using DP in O(n^2) time + top down approach.\n\nBut wasnt able to do satisfactory time complexity analysis when only using recu | pritamprakash | NORMAL | 2022-07-03T11:19:18.244161+00:00 | 2022-07-03T11:19:18.244219+00:00 | 73 | false | I was able to solve this problem using DP in O(n^2) time + top down approach.\n\nBut wasnt able to do satisfactory time complexity analysis when only using recursion without using any DP?\n\nHas anyone done time complexity analysis when only using recusrsion without any DP ? | 1 | 0 | ['Recursion'] | 0 |
stone-game-v | C++ solution. || Recursion+Memoization. | c-solution-recursionmemoization-by-samar-yrvw | \n#define ll long long\n#define vb vector<bool>\n#define vi vector<int>\n#define vl vector<long long>\n#define vvb vector<vector<bool>>\n#define vvi vector<vect | samarthya2912 | NORMAL | 2022-06-07T03:20:08.151053+00:00 | 2022-06-07T03:20:08.151084+00:00 | 87 | false | ```\n#define ll long long\n#define vb vector<bool>\n#define vi vector<int>\n#define vl vector<long long>\n#define vvb vector<vector<bool>>\n#define vvi vector<vector<int>>\n#define vvl vector<vector<long long>>\n#define pii pair<int,int>\n#define all(i) i.begin(),i.end()\n#define f(i,s,e) for(int i = s; i < e; i++)\n#d... | 1 | 0 | [] | 0 |
stone-game-v | ONLY GO SOLUTION | DP (only using indices) | only-go-solution-dp-only-using-indices-b-nmih | ```\nfunc stoneGameV(stoneValue []int) int {\n type Key struct {\n r int\n l int\n }\n prefix := []int{0}\n curr := 0\n for _, s := | Pedraamy | NORMAL | 2022-04-20T22:52:30.618025+00:00 | 2022-04-20T23:03:32.602933+00:00 | 79 | false | ```\nfunc stoneGameV(stoneValue []int) int {\n type Key struct {\n r int\n l int\n }\n prefix := []int{0}\n curr := 0\n for _, s := range stoneValue {\n curr += s\n prefix = append(prefix, curr)\n }\n memo := make(map[Key]int)\n var dp func(lb int, rb int) int\n dp... | 1 | 0 | [] | 0 |
stone-game-v | CPP/Game Theory/Highly Commented/Step-by-step/Easy Approach/N2 | cppgame-theoryhighly-commentedstep-by-st-hps1 | \nclass Solution {\npublic:\n int dp[501][501];\n \n// Time Complexity : O(N2)\n// Idea is to check \n// from i ...... j\n// parition the | vijaypal1621 | NORMAL | 2022-03-14T15:16:01.853726+00:00 | 2022-03-14T15:20:50.429739+00:00 | 101 | false | ```\nclass Solution {\npublic:\n int dp[501][501];\n \n// Time Complexity : O(N2)\n// Idea is to check \n// from i ...... j\n// parition the array[i...j] from k = [i to (j-1)]\n// forming two parts arr[i...k] and arr[k+1 .... j]\n// check whichever is greater, discard and recur for smaller... | 1 | 0 | ['Dynamic Programming', 'Prefix Sum'] | 0 |
stone-game-v | Do Only What is stated in the question | Noob Solution | Game Strategy | do-only-what-is-stated-in-the-question-n-ssfb | ```\nclass Solution {\npublic:\n vector> dp;\n int fun(int i,int j,vector &arr,vector &pre){\n //Base Case \n if(i == j) return 0;\n | njcoder | NORMAL | 2022-03-06T11:32:19.617434+00:00 | 2022-03-06T11:32:44.638667+00:00 | 115 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> dp;\n int fun(int i,int j,vector<int> &arr,vector<int> &pre){\n //Base Case \n if(i == j) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n int score = INT_MIN;\n for(int part = i+1;part <= j;part++){\n int left = p... | 1 | 0 | ['Recursion', 'Memoization'] | 1 |
stone-game-v | [C++/DP and Memorization] 172 ms (94.32%) , 10.6 MB (75.68%) O(n^2) | cdp-and-memorization-172-ms-9432-106-mb-ds10f | The following algorithm is an 1-index solution.\nFirst a DP table is defined as\ndp[i][j] := the maximum score with the initial states: stones in the range from | HirofumiTsuda | NORMAL | 2021-08-18T11:41:57.569137+00:00 | 2021-08-20T14:58:55.966878+00:00 | 172 | false | The following algorithm is an 1-index solution.\nFirst a DP table is defined as\ndp[i][j] := the maximum score with the initial states: stones in the range from i to j (i and j are included).\nThen, there are (j - i) stones in this state. It means there are (j - i - 1) separators.\nIt is clear that the value dp[i][j] i... | 1 | 0 | [] | 0 |
stone-game-v | Simple C++ Dynamic Programming Solution | simple-c-dynamic-programming-solution-by-f135 | \nclass Solution {\npublic:\n \n int dp[505][505];\n \n int fun(vector<int>& stoneValue, int i, int j){\n \n if(i == j){\n | tanishqj2005 | NORMAL | 2021-07-20T17:47:05.237864+00:00 | 2021-07-20T17:47:05.237907+00:00 | 98 | false | ```\nclass Solution {\npublic:\n \n int dp[505][505];\n \n int fun(vector<int>& stoneValue, int i, int j){\n \n if(i == j){\n return 0;\n }\n \n if(dp[i][j] != -1){\n return dp[i][j];\n }\n \n int sum = 0, curr = 0, ans = 0;\n ... | 1 | 0 | [] | 0 |
stone-game-v | Top down DP | Commented | top-down-dp-commented-by-apooos3-297p | \nclass Solution {\n public int stoneGameV(int[] s) {\n int n = s.length;\n\t\t//prefix array so that difference between left and right can be calcula | apooos3 | NORMAL | 2021-06-11T18:59:12.892598+00:00 | 2021-06-11T19:02:59.100933+00:00 | 312 | false | ```\nclass Solution {\n public int stoneGameV(int[] s) {\n int n = s.length;\n\t\t//prefix array so that difference between left and right can be calculated in O(1)\n int[] prefix = new int[n];\n\t\t//2D DP array for each cut (length) vs. elements containing the actual sum\n int[][] dp = new int... | 1 | 0 | ['Dynamic Programming', 'Memoization', 'Java'] | 0 |
stone-game-v | DP n^3 with optimisation and elimination. Detailed explanation. [Latest] [Python] | dp-n3-with-optimisation-and-elimination-isryp | The idea of elimiation was based on GP and inspired from @Prezes Code. In O(n^3) we trim some corner cases. Like if we are itterating over different partionions | glastonbury | NORMAL | 2021-06-04T19:46:12.451498+00:00 | 2021-06-04T19:49:35.265342+00:00 | 155 | false | The idea of elimiation was based on GP and inspired from @Prezes Code. In O(n^3) we trim some corner cases. Like if we are itterating over different partionions for a given subarray(start,end) we keep track of what is the max from any given subarry. Now when we compute leftRow Sum and rightRow sum we apply an elimation... | 1 | 1 | [] | 0 |
stone-game-v | Recursion ->DP | recursion-dp-by-sinhaneha455-cm9x | TLE Error Recursion Code\n\n public int solve(int si , int ei , int[]arr){\n \n if(si>ei){\n return 0;\n }\n \n in | sinhaneha455 | NORMAL | 2021-05-11T12:59:44.921023+00:00 | 2021-05-11T13:00:20.543162+00:00 | 311 | false | **TLE Error Recursion Code**\n```\n public int solve(int si , int ei , int[]arr){\n \n if(si>ei){\n return 0;\n }\n \n int rightPart =0 , leftPart=0 , result=0;\n \n for(int i=si;i<=ei;i++){\n rightPart+=arr[i];\n }\n \n for(int ... | 1 | 0 | ['Dynamic Programming', 'Recursion', 'Java'] | 0 |
stone-game-v | C# solution using Memoization and Prefix Sum | c-solution-using-memoization-and-prefix-psz82 | \npublic class Solution {\n int[,] dp;\n int[] prefix;\n public int Solve(int[] arr, int i, int j)\n {\n if(i>=j) return 0;\n if(dp[i, | aj936563 | NORMAL | 2021-05-08T11:10:30.118437+00:00 | 2021-05-08T11:10:30.118465+00:00 | 56 | false | ```\npublic class Solution {\n int[,] dp;\n int[] prefix;\n public int Solve(int[] arr, int i, int j)\n {\n if(i>=j) return 0;\n if(dp[i,j]!=0) return dp[i,j];\n int res = 0;\n for(int k=i;k<=j;k++)\n {\n int left = prefix[k+1] - prefix[i];\n int righ... | 1 | 0 | [] | 0 |
stone-game-v | 100% (T), 100%(S) :: C / Python - Memoization | 100-t-100s-c-python-memoization-by-tuhin-lmm6 | Down below, you\'ll find C and Python implementations for the same. Funny how a 100% faster C solution exceeds the allotted time limit when coded in Python, The | tuhinnn_py | NORMAL | 2021-05-06T15:09:39.632686+00:00 | 2021-05-06T15:09:39.632717+00:00 | 254 | false | Down below, you\'ll find C and Python implementations for the same. Funny how a 100% faster C solution exceeds the allotted time limit when coded in Python, The code is pretty self explanatory. Let me know if you still need help in the comments below.\n\n**C**\n\n```\nint dp[501][501];\n\nint max(int a, int b)\n{\n ... | 1 | 0 | ['Memoization', 'C', 'Python'] | 1 |
stone-game-v | 80ms C++ DP and reduce branches | 80ms-c-dp-and-reduce-branches-by-mz1007-veqv | The key to accelerate the code is to reduce branches (if(2*min(sumL, sumR) < tmp) continue;), which helps to reduce runtime from 800ms to 80ms (faster than 97%) | mz1007 | NORMAL | 2021-03-29T14:14:43.637621+00:00 | 2021-03-30T18:38:10.513341+00:00 | 152 | false | The key to accelerate the code is to reduce branches (`if(2*min(sumL, sumR) < tmp) continue;`), which helps to reduce runtime from 800ms to 80ms (faster than 97%).\n```c++\nclass Solution {\npublic:\n int stoneGameV(vector<int>& v) {\n //dp[i][j]: max scores obtained from v[i:j]\n //dp[i][j] = max{ ( ... | 1 | 0 | [] | 2 |
stone-game-v | Javascript beats 100% time and 100% memory (368ms, 39.4mb) | javascript-beats-100-time-and-100-memory-lysu | \nvar stoneGameV = function(stones) {\n let bestAns = 0\n let stoneSum = stones.reduce((sum, current)=> {return sum+ current},0)\n\n function splitAndA | raphyhayes | NORMAL | 2021-03-22T20:18:35.767200+00:00 | 2021-03-22T20:30:35.880266+00:00 | 135 | false | ```\nvar stoneGameV = function(stones) {\n let bestAns = 0\n let stoneSum = stones.reduce((sum, current)=> {return sum+ current},0)\n\n function splitAndAdd(stoneSum, ans, leftBound,rightBound){\n\t\n if (rightBound === leftBound){return ans}\n\n if (rightBound - leftBound === 1){\n re... | 1 | 0 | ['JavaScript'] | 1 |
stone-game-v | C++ Solution [Recursive Memoization ] | c-solution-recursive-memoization-by-sha_-nxh8 | \nclass Solution {\npublic:\n \n vector<int> presum;\n \n vector<vector<int>> dp;\n \n vector<int> stones;\n \n int solve(int i,int j)\n | sha_256 | NORMAL | 2021-01-07T07:08:28.336630+00:00 | 2021-01-07T07:08:28.336664+00:00 | 110 | false | ```\nclass Solution {\npublic:\n \n vector<int> presum;\n \n vector<vector<int>> dp;\n \n vector<int> stones;\n \n int solve(int i,int j)\n {\n \n\n\n if(i>j)\n return 0;\n\n \n if(i == j)\n return 0;\n \n \n if(j-i == 1)\n... | 1 | 0 | [] | 0 |
stone-game-v | C++ | DP | ft. Prefix - Sum | Clean Code. | c-dp-ft-prefix-sum-clean-code-by-lekhesh-syum | \n\nclass Solution {\npublic:\n int dp[501][501];\n int prefixSum[501];\n \n int helper(vector<int>& s, int l, int r){\n \n if(l >= r) | lekhesh12 | NORMAL | 2020-10-26T18:03:31.100195+00:00 | 2020-10-26T18:03:31.100248+00:00 | 116 | false | \n```\nclass Solution {\npublic:\n int dp[501][501];\n int prefixSum[501];\n \n int helper(vector<int>& s, int l, int r){\n \n if(l >= r) return 0; // BASE case\n \n if(dp[l][r] != -1)return dp[l][r]; // Return pre commputed sub-problem.\n \n int ans = INT_MIN;\n ... | 1 | 0 | [] | 0 |
stone-game-v | Java solution from brute-force to DP O(n^3) | java-solution-from-brute-force-to-dp-on3-gbyg | Solution brute-force - Time Limit Exceeded \n\npublic class StoneGameVRecursiveApproach {\n public static void main(String[] args) {\n int[] stoneGame | akiramonster | NORMAL | 2020-10-04T22:37:44.193620+00:00 | 2020-10-04T22:37:44.193664+00:00 | 99 | false | Solution brute-force - Time Limit Exceeded \n```\npublic class StoneGameVRecursiveApproach {\n public static void main(String[] args) {\n int[] stoneGame = {6, 2, 3, 4, 5, 5};\n System.out.println(stoneGameV(stoneGame));\n }\n\n public static int stoneGameV(int[] stoneValue) {\n int n = st... | 1 | 0 | [] | 0 |
stone-game-v | Over-commented code for Stone Game V (1563) | over-commented-code-for-stone-game-v-156-bi2b | Hopefully this helps some folks. Over-commented :-)\n\nclass Solution {\npublic:\n int sgv( int b, /* Beginning index of row of stone | tktripathy | NORMAL | 2020-09-10T02:16:53.689418+00:00 | 2020-09-10T02:23:44.386236+00:00 | 148 | false | Hopefully this helps some folks. Over-commented :-)\n```\nclass Solution {\npublic:\n int sgv( int b, /* Beginning index of row of stones */\n int e, /* Ending index of row */\n vector<int>& s, /* Original row of stones - prefix-s... | 1 | 0 | [] | 1 |
stone-game-v | [Python] Need help with TLE | DP O(n^3) | python-need-help-with-tle-dp-on3-by-zhen-ue2b | 126/131 cases passed. I used lru to cache results from function. \n\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n # stoneVa | zhenglun | NORMAL | 2020-09-01T03:25:39.446560+00:00 | 2020-09-01T03:25:39.446605+00:00 | 269 | false | 126/131 cases passed. I used lru to cache results from function. \n```\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n # stoneValue\n prefix = [0] \n for v in stoneValue:\n prefix.append(prefix[-1]+v)\n @lru_cache(None)\n def dp(i, j):\n ... | 1 | 0 | ['Python'] | 1 |
stone-game-v | [Java] Easy Bottom Up O(N^3) | java-easy-bottom-up-on3-by-lancewang-fra5 | ```\n\npublic int stoneGameV(int[] stoneValue) {\n int n = stoneValue.length;\n int[] sum = new int[n + 1];\n \n for(int i = 0; i < | lancewang | NORMAL | 2020-08-30T03:28:31.326601+00:00 | 2020-08-30T03:43:35.741919+00:00 | 86 | false | ```\n\npublic int stoneGameV(int[] stoneValue) {\n int n = stoneValue.length;\n int[] sum = new int[n + 1];\n \n for(int i = 0; i < n; i++){\n sum[i+1] = sum[i] + stoneValue[i];\n }\n \n int[][] dp = new int[n + 1][n + 1];\n \n for(int len = 2; l... | 1 | 0 | [] | 0 |
stone-game-v | well comment and easy to understand.. | well-comment-and-easy-to-understand-by-f-26z3 | We are going to explore all the devisior of stoneValue and maintain a sum...\nat any position there could be 3 condition\n1.leftsum is greater then right sum\n\ | faltu_admi | NORMAL | 2020-08-29T05:07:26.349298+00:00 | 2020-08-29T05:07:26.349348+00:00 | 125 | false | We are going to explore all the devisior of stoneValue and maintain a sum...\nat any position there could be 3 condition\n1.leftsum is greater then right sum\n\tin this case bob will through out left part and alice got the point as right sum and rest of game start with right part of stoneValue\n2.right sum is greater t... | 1 | 0 | [] | 0 |
stone-game-v | Easy JAVA dp solution | easy-java-dp-solution-by-virti-4co0 | \nclass Solution {\n Integer dp[][];\n public int stoneGameV(int[] stoneValue) {\n dp=new Integer[stoneValue.length][stoneValue.length];\n f | virti | NORMAL | 2020-08-24T16:07:17.295591+00:00 | 2020-08-24T16:07:54.527030+00:00 | 109 | false | ```\nclass Solution {\n Integer dp[][];\n public int stoneGameV(int[] stoneValue) {\n dp=new Integer[stoneValue.length][stoneValue.length];\n for(int i=1;i<stoneValue.length;i++) stoneValue[i]+=stoneValue[i-1];\n return check(0,stoneValue.length-1,stoneValue);\n }\n public int check(in... | 1 | 0 | ['Dynamic Programming'] | 0 |
stone-game-v | easy DP solution using cumulative sum (JAVA) | easy-dp-solution-using-cumulative-sum-ja-0em4 | \nclass Solution {\n public int stoneGameV(int[] stoneValue) {\n int n= stoneValue.length;\n if(n==1) return 0;\n int A[]= new int[n+1]; | nrjain1997 | NORMAL | 2020-08-24T12:31:39.869105+00:00 | 2020-08-24T12:31:39.869153+00:00 | 83 | false | ```\nclass Solution {\n public int stoneGameV(int[] stoneValue) {\n int n= stoneValue.length;\n if(n==1) return 0;\n int A[]= new int[n+1];\n int dp[][] = new int[n+1][n+1];\n for(int[] x:dp)Arrays.fill(x,-1);\n A[0]=0;\n for(int i=1;i<n+1;i++) A[i]=A[i-1]+stoneValue[... | 1 | 0 | ['Dynamic Programming', 'Prefix Sum'] | 0 |
stone-game-v | DP Javascript | dp-javascript-by-rockwell153-kjaf | \n/**\n:subproblem\n what is the maximum score of the rest of the stones\n\n:recurrence\n for all possible break points\n if equal halfs\n | rockwell153 | NORMAL | 2020-08-23T18:04:55.021498+00:00 | 2020-08-23T18:05:04.633135+00:00 | 113 | false | ```\n/**\n:subproblem\n what is the maximum score of the rest of the stones\n\n:recurrence\n for all possible break points\n if equal halfs\n dp[left][right] = max(\n leftsum + recur( left ),\n rightsum + recur( right )\n )\n else \n dp[le... | 1 | 0 | ['Dynamic Programming', 'JavaScript'] | 0 |
stone-game-v | C# Prefix Sum + Memoization | c-prefix-sum-memoization-by-ve7545-uz17 | Runtime: 228 ms\nMemory Usage: 33.7 MB\n\n public int StoneGameV(int[] stoneValue) {\n int[,] dp = new int[stoneValue.Length, stoneValue.Length]; | ve7545 | NORMAL | 2020-08-23T15:43:23.240503+00:00 | 2020-08-23T15:57:00.697791+00:00 | 77 | false | Runtime: 228 ms\nMemory Usage: 33.7 MB\n```\n public int StoneGameV(int[] stoneValue) {\n int[,] dp = new int[stoneValue.Length, stoneValue.Length]; \n int[] prefixSum = new int[stoneValue.Length+1];\n \n for(int i=0; i< stoneValue.Length; i++)\n {\n prefixSum[i+1]... | 1 | 0 | [] | 1 |
stone-game-v | please clear my doubt | please-clear-my-doubt-by-anshuman2043-bwbr | \nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n ans = 0\n def game(l,score):\n #print(l)\n #pri | Anshuman2043 | NORMAL | 2020-08-23T09:06:05.966531+00:00 | 2020-08-23T09:06:05.966562+00:00 | 136 | false | ```\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n ans = 0\n def game(l,score):\n #print(l)\n #print(score)\n if len(l) <= 1:\n nonlocal ans\n ans = max((ans,score))\n return\n s = sum(l)\... | 1 | 0 | [] | 1 |
stone-game-v | TopDownDFS+Memo(O(N^3)) And BottomUpDP(O(N^2)) | topdowndfsmemoon3-and-bottomupdpon2-by-j-xomg | Top Down DFS + Memo 460ms\ncpp\nclass Solution {\n vector<int> prefixSum;\n vector<vector<int>> mem;\n int cnt = 0;\n void dfs(const vector<int>& pr | jiah | NORMAL | 2020-08-23T08:05:35.198352+00:00 | 2020-08-23T08:09:33.253435+00:00 | 123 | false | Top Down DFS + Memo 460ms\n```cpp\nclass Solution {\n vector<int> prefixSum;\n vector<vector<int>> mem;\n int cnt = 0;\n void dfs(const vector<int>& prefixSum, int begin, int end) {\n if (mem[begin][end])\n return;\n if (begin+1 == end)\n return;\n cnt ++;\n ... | 1 | 0 | [] | 0 |
stone-game-v | Prefix Sum +DP(Memoization) | prefix-sum-dpmemoization-by-ghoshashis54-db68 | \nint cache[505][505];\nint pre[505];\nint n;\nint dp(int left,int right)\n{\n if(left == right)\n return 0;\n int &ans = cache[left][right];\n | ghoshashis545 | NORMAL | 2020-08-23T08:00:39.298549+00:00 | 2020-08-23T08:00:39.298593+00:00 | 102 | false | ```\nint cache[505][505];\nint pre[505];\nint n;\nint dp(int left,int right)\n{\n if(left == right)\n return 0;\n int &ans = cache[left][right];\n if(ans != -1)\n return ans;\n // left subarray -> [left , partition_index]\n // right subarray -> [partition_index + 1 , right]\n \n for(... | 1 | 0 | [] | 2 |
stone-game-v | Python PrefixSum + DFS memo | python-prefixsum-dfs-memo-by-ysz951-7s79 | \nclass Solution(object):\n def stoneGameV(self, stones):\n """\n :type stoneValue: List[int]\n :rtype: int\n """\n n = le | ysz951 | NORMAL | 2020-08-23T04:19:11.844616+00:00 | 2020-08-23T04:19:11.844650+00:00 | 96 | false | ```\nclass Solution(object):\n def stoneGameV(self, stones):\n """\n :type stoneValue: List[int]\n :rtype: int\n """\n n = len(stones)\n preSum = [0] * (n + 1)\n for i in range(1, n + 1):\n preSum[i] = stones[i - 1] + preSum[i - 1]\n memo = {}\n ... | 1 | 0 | [] | 0 |
stone-game-v | [Java] 45ms clean code, Top down dp O(n^3) | java-45ms-clean-code-top-down-dp-on3-by-6kuj3 | Java\nclass Solution {\n public int stoneGameV(int[] stoneValue) {\n int n = stoneValue.length;\n int[] preSum = new int[n + 1];\n for ( | binglelove | NORMAL | 2020-08-23T04:11:47.131280+00:00 | 2020-08-23T04:11:47.131337+00:00 | 129 | false | ```Java\nclass Solution {\n public int stoneGameV(int[] stoneValue) {\n int n = stoneValue.length;\n int[] preSum = new int[n + 1];\n for (int i = 0; i < n; i++)\n preSum[i + 1] = preSum[i] + stoneValue[i];\n int[][] memo = new int[n][n];\n return dfs(stoneValue, preSum,... | 1 | 1 | [] | 0 |
stone-game-v | Any idea how to speedup bottoms up DP ? | C++ | O(N^3) | TLE | any-idea-how-to-speedup-bottoms-up-dp-c-alf80 | imo, dp questions should accept both top down and bottom up approaches. No able to figure out what I could have done to improve this solution. Suggestions are w | all_might | NORMAL | 2020-08-23T04:05:42.285150+00:00 | 2020-08-23T04:11:08.659923+00:00 | 156 | false | > imo, dp questions should accept both top down and bottom up approaches. No able to figure out what I could have done to improve this solution. Suggestions are welcome.\n\n```\nclass Solution {\npublic:\n typedef long long ll;\n int stoneGameV(vector<int>& sv) {\n int n = sv.size();\n vector<int> p... | 1 | 0 | [] | 2 |
stone-game-v | C++ DP | Easy Solution | Memoization | c-dp-easy-solution-memoization-by-kaintp-ydam | class Solution {\npublic:\n \n \n int findMax(vector&prefix, int start, int end, vector>&dp ){\n \n if(start==end){\n return 0 | kaint | NORMAL | 2020-08-23T04:05:40.851553+00:00 | 2020-08-23T04:09:10.862069+00:00 | 95 | false | class Solution {\npublic:\n \n \n int findMax(vector<int>&prefix, int start, int end, vector<vector<int>>&dp ){\n \n if(start==end){\n return 0;\n } \n if(dp[start][end]!=-1)\n return dp[start][end];\n int maxans=0;\n \n for(int cut=... | 1 | 0 | [] | 0 |
stone-game-v | [Java] Top Down DP with Memo O(N^3) | java-top-down-dp-with-memo-on3-by-yuhwu-vg24 | \nclass Solution {\n int[][] memo;\n public int stoneGameV(int[] stoneValue) {\n int n = stoneValue.length;\n int[] pSum = new int[n+1];\n | yuhwu | NORMAL | 2020-08-23T04:02:17.321815+00:00 | 2020-08-23T04:02:17.321877+00:00 | 119 | false | ```\nclass Solution {\n int[][] memo;\n public int stoneGameV(int[] stoneValue) {\n int n = stoneValue.length;\n int[] pSum = new int[n+1];\n memo = new int[n+1][n+1];\n for(int i=1; i<=n; i++){\n pSum[i] = pSum[i-1] + stoneValue[i-1];\n }\n return calc(pSum, 0... | 1 | 0 | [] | 0 |
stone-game-v | Easy To understand! JAVA DFS+Memory+preSum!! | easy-to-understand-java-dfsmemorypresum-8u3vh | I think it is clear to understand! Maybe someone can help me to explain it.\n\n\nclass Solution {\n public int stoneGameV(int[] stoneValue) {\n\n int | sugersu | NORMAL | 2020-08-23T04:02:13.159504+00:00 | 2020-08-23T04:02:13.159572+00:00 | 112 | false | I think it is clear to understand! Maybe someone can help me to explain it.\n\n```\nclass Solution {\n public int stoneGameV(int[] stoneValue) {\n\n int n=stoneValue.length;\n if(n<2) return 0;\n \n long[] preSum=new long[n+1];\n Map<Pair<Integer,Integer>,Long> memo= new HashMap<>(... | 1 | 0 | [] | 0 |
stone-game-v | Top Down DP with Memoization | top-down-dp-with-memoization-by-zsshen-rh2l | \nclass Solution {\npublic:\n int stoneGameV(vector<int>& arr) {\n \n int n = arr.size();\n\n vector<int> prefix(n + 1, 0);\n for | zsshen | NORMAL | 2020-08-23T04:02:02.827965+00:00 | 2020-08-23T04:02:02.828013+00:00 | 123 | false | ```\nclass Solution {\npublic:\n int stoneGameV(vector<int>& arr) {\n \n int n = arr.size();\n\n vector<int> prefix(n + 1, 0);\n for (int i = 1 ; i <= n ; ++i) {\n prefix[i] = prefix[i - 1] + arr[i - 1];\n }\n \n vector<vector<int>> dp(n, vector<int>(n, -1)... | 1 | 0 | [] | 0 |
stone-game-v | [Python3] intuitive DP | python3-intuitive-dp-by-xavier90-g0oq | \nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n n = len(stoneValue)\n \n pre = [0]\n for e in stoneValue:\n | xavier90 | NORMAL | 2020-08-23T04:01:33.439148+00:00 | 2020-08-27T23:54:09.155057+00:00 | 172 | false | ```\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n n = len(stoneValue)\n \n pre = [0]\n for e in stoneValue:\n pre.append(pre[-1]+e)\n \n import functools\n @functools.lru_cache(None)\n def dp(i, j):\n if i >= j:\... | 1 | 1 | [] | 0 |
stone-game-v | [Java] Simple top-down dfs+memo(with comments) | java-simple-top-down-dfsmemowith-comment-0rki | Intuition\nSimulate the game by dividing the array into two subarrays at each iteration. Add the subarray with smaller sum to result of current search and conti | dorayaki1018 | NORMAL | 2020-08-23T04:01:30.858769+00:00 | 2020-08-23T04:22:01.977688+00:00 | 161 | false | **Intuition**\nSimulate the game by dividing the array into two subarrays at each iteration. Add the subarray with smaller sum to result of current search and continue searching the subarray with smaller sum.\n\n`dp[i][j]` represents the max score we can get in range `[i, j]`\n\nWe use the `presum` array to comput the ... | 1 | 0 | [] | 0 |
stone-game-v | [C++] Prefix Sum + DP (Memoization) | c-prefix-sum-dp-memoization-by-jayesh_jo-70tw | Complexity
Time complexity: O(n³)
Space complexity: O(n²)
Code | Jayesh_Joshi | NORMAL | 2025-04-05T13:17:14.353885+00:00 | 2025-04-05T13:17:14.353885+00:00 | 1 | false |
# Complexity
- Time complexity: O(n³)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(n²)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int n;
vector<int> v, p;
vector<vector<int>> dp;
int solve(int l, int r) {
... | 0 | 0 | ['C++'] | 0 |
stone-game-v | Simple|| DP || Memoization Code || | simple-dp-memoization-code-by-rwt2003-gida | IntuitionApproachComplexity
Time complexity:
O(n^3)
Space complexity:
O(n^2)Code | rwt2003 | NORMAL | 2025-03-08T10:29:05.915101+00:00 | 2025-03-08T10:29:05.915101+00:00 | 2 | 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)$$ -->
O(n^3)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(... | 0 | 0 | ['C++'] | 0 |
stone-game-v | Easy CPP Solution | easy-cpp-solution-by-vidhyarthisunav-ors4 | Code | vidhyarthisunav | NORMAL | 2025-03-04T17:56:15.791401+00:00 | 2025-03-04T17:56:15.791401+00:00 | 5 | false | # Code
```cpp []
class Solution {
public:
vector<int> prefix_sum;
int dp[501][501];
int dfs(int i, int j) {
if (i == j) return 0;
if (dp[i][j] != -1) return dp[i][j];
int res = 0;
for (int idx = i; idx < j; idx++) {
int left_sum = prefix_sum[idx + 1] - prefi... | 0 | 0 | ['C++'] | 0 |
stone-game-v | ✅ C++ sol. using TOP DOWN (RECURSIVE) 2-d DP and PREFIX-SUM | c-sol-using-top-down-recursive-2-d-dp-an-vf43 | Code | deepanshugupta134 | NORMAL | 2025-02-19T18:48:13.557923+00:00 | 2025-02-19T18:48:13.557923+00:00 | 6 | false |
# Code
```cpp []
class Solution {
public:
int dp[501][501];
int fun(vector<int>&v , vector<int>&ps , int l , int r ){
if(r - l == 0){
return 0 ;
}
if(dp[l][r] != -1){
return dp[l][r];
}
int ans = 0 ;
int lp = 0 , rp = 0 ;
for(i... | 0 | 0 | ['Array', 'Dynamic Programming', 'Recursion', 'Memoization', 'Game Theory', 'C++'] | 0 |
stone-game-v | Bottom up DP with Knuth optimization | bottom-up-dp-with-knuth-optimization-by-hflwb | ApproachBottom up DP with Knuth optimization.Sorry in Japanese, but refer tomy blog postfor the idea.Complexity
Time complexity:O(N^2)
Space complexity:O(N^2) | kudojp | NORMAL | 2025-02-18T09:27:28.795886+00:00 | 2025-02-18T09:29:18.085076+00:00 | 6 | false | # Approach
Bottom up DP with Knuth optimization.
Sorry in Japanese, but refer to [my blog post](https://qiita.com/kudojp/items/5811b6d6444c866780a5) for the idea.
# Complexity
- Time complexity: `O(N^2)`
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: `O(N^2)`
<!-- Add your space complexi... | 0 | 0 | ['Python3'] | 0 |
stone-game-v | Solution using Prefix sum and DP | solution-using-prefix-sum-and-dp-by-user-47qv | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | user5947v | NORMAL | 2025-01-30T09:03:49.144909+00:00 | 2025-01-30T09:03:49.144909+00:00 | 2 | 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
`... | 0 | 0 | ['C++'] | 0 |
stone-game-v | 1563. Stone Game V | 1563-stone-game-v-by-g8xd0qpqty-ii15 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | G8xd0QPqTy | NORMAL | 2025-01-13T16:24:20.719004+00:00 | 2025-01-13T16:24:20.719004+00:00 | 4 | 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
`... | 0 | 0 | ['C++'] | 0 |
stone-game-v | Why this problem pass all cases in cpp but not in python3. | why-this-problem-pass-all-cases-in-cpp-b-ixei | Code | bepriyansh | NORMAL | 2025-01-02T13:24:42.378145+00:00 | 2025-01-02T13:24:42.378145+00:00 | 4 | false | # Code
```cpp []
class Solution {
vector<int> sv, pre;
vector<vector<int>> cache;
int n;
int gs(int l, int r) { return pre[r] - (l > 0 ? pre[l - 1] : 0); }
int dp(int l, int r) {
if (l == r)
return 0;
if (cache[l][r] != -1)
return cache[l][r];
int res... | 0 | 0 | ['C++'] | 0 |
stone-game-v | Dynamic Programming with Prefix Sum - O(N^3) - C++ | dynamic-programming-with-prefix-sum-on3-et2p5 | Intuition The problem is about maximizing a score obtained by choosing optimal subarrays in a sequence. The recursive nature of evaluating subarrays makes it co | upperknoot | NORMAL | 2024-11-19T22:12:05.903644+00:00 | 2024-11-19T22:12:05.903679+00:00 | 6 | false | # Intuition
The problem is about maximizing a score obtained by choosing optimal subarrays in a sequence. The recursive nature of evaluating subarrays makes it conducive to a dynamic programming approach. We can utilize prefix sums to efficiently compute the sum of any subarray, which is a key operation in the solution... | 0 | 0 | ['Dynamic Programming', 'Prefix Sum', 'C++'] | 0 |
stone-game-v | Swift with recursion + caching. | swift-with-recursion-caching-by-wenjiema-iiwo | Intuition\nThe problem can be solved with recursion. Two helper functions each represent Alice and Bob, they both return Alice\'s sum:\n1. Alice would devide ar | wenjiema | NORMAL | 2024-11-08T20:58:41.073531+00:00 | 2024-11-08T20:58:41.073552+00:00 | 1 | false | # Intuition\nThe problem can be solved with recursion. Two helper functions each represent Alice and Bob, they both return Alice\'s sum:\n1. Alice would devide array into two parts, and find the division that returns biggest result.\n2. Bob would throw away array with the bigger sum, then add the smaller sum to Alice\'... | 0 | 0 | ['Swift'] | 0 |
stone-game-v | My java 287ms solution 33% faster | my-java-287ms-solution-33-faster-by-ragh-ut4r | 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 | raghavrathore7415 | NORMAL | 2024-10-27T05:07:38.217977+00:00 | 2024-10-27T05:07:38.217999+00:00 | 3 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
stone-game-v | [Accepted] Swift | accepted-swift-by-vasilisiniak-9arq | \nclass Solution {\n func stoneGameV(_ stoneValue: [Int]) -> Int {\n\n let sum = stoneValue.reduce(into: [0]) { $0.append($0.last! + $1) }\n \n | vasilisiniak | NORMAL | 2024-10-07T09:53:44.373752+00:00 | 2024-10-07T09:53:44.373785+00:00 | 0 | false | ```\nclass Solution {\n func stoneGameV(_ stoneValue: [Int]) -> Int {\n\n let sum = stoneValue.reduce(into: [0]) { $0.append($0.last! + $1) }\n \n var dp = Array(\n repeating: Array(repeating: 0, count: stoneValue.count),\n count: stoneValue.count\n )\n\n for ... | 0 | 0 | ['Swift'] | 0 |
stone-game-v | 1563. Stone Game V.cpp | 1563-stone-game-vcpp-by-202021ganesh-8x3l | Code\n\nclass Solution {\npublic:\n int stoneGameV(vector<int>& stoneValue) {\n int n = stoneValue.size();\n dp.resize(n, vector<int>(n, -1));\n retu | 202021ganesh | NORMAL | 2024-10-03T06:24:14.020888+00:00 | 2024-10-03T06:24:14.020923+00:00 | 5 | false | **Code**\n```\nclass Solution {\npublic:\n int stoneGameV(vector<int>& stoneValue) {\n int n = stoneValue.size();\n dp.resize(n, vector<int>(n, -1));\n return stoneGameV(stoneValue, 0, n - 1);\n }\n private:\n vector<vector<int>> dp;\n int stoneGameV(const vector<int>& stoneValue, int i, int j) {\n if(... | 0 | 0 | ['C'] | 0 |
stone-game-v | 2D DP,WITH PREFIX SUM. | 2d-dpwith-prefix-sum-by-damon109-ncwe | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is about maximizing Alice\'s score by carefully choosing how to split the a | damon109 | NORMAL | 2024-09-04T11:29:41.680759+00:00 | 2024-09-04T11:29:41.680787+00:00 | 6 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is about maximizing Alice\'s score by carefully choosing how to split the array of stones in each round. Alice aims to divide the stones such that Bob is forced to throw away the row with the larger sum, leaving Alice with the... | 0 | 0 | ['Dynamic Programming', 'C++'] | 0 |
stone-game-v | My Solution | my-solution-by-hope_ma-7uh5 | \n/**\n * let `n` be the length of the vector `stoneValue`\n *\n * the dp solution is employed.\n * dp[start][end] stands for the maximum score that Alice can o | hope_ma | NORMAL | 2024-09-03T06:33:38.858938+00:00 | 2024-09-03T16:16:02.803281+00:00 | 3 | false | ```\n/**\n * let `n` be the length of the vector `stoneValue`\n *\n * the dp solution is employed.\n * dp[start][end] stands for the maximum score that Alice can obtain from the sub-row whose range is from the index\n * `start` to the index `end` of the vector `stoneValue`, `start` inclusive, ... | 0 | 0 | [] | 0 |
find-n-unique-integers-sum-up-to-zero | [Java/C++/Python] Find the Rule | javacpython-find-the-rule-by-lee215-1iuj | Intuition\nNaive idea\nn = 1, [0]\nn = 2, [-1, 1]\n\nNow write more based on this\nn = 3, [-2, 0, 2]\nn = 4, [-3, -1, 1, 3]\nn = 5, [-4, -2, 0, 2, 4]\n\nIt spre | lee215 | NORMAL | 2019-12-30T17:11:21.003630+00:00 | 2020-01-10T03:05:24.181938+00:00 | 38,328 | false | ## **Intuition**\nNaive idea\n`n = 1, [0]`\n`n = 2, [-1, 1]`\n\nNow write more based on this\n`n = 3, [-2, 0, 2]`\n`n = 4, [-3, -1, 1, 3]`\n`n = 5, [-4, -2, 0, 2, 4]`\n\nIt spreads like the wave.\n<br>\n\n## **Explanation**\nFind the rule\n`A[i] = i * 2 - n + 1`\n<br>\n\n## **Math Observation**\n@zzg_zzm helps explain ... | 434 | 6 | [] | 61 |
find-n-unique-integers-sum-up-to-zero | Simple Java : Fill from both sides | simple-java-fill-from-both-sides-by-ansh-xr7n | start filling up from left and right complementary values (so if we insert 1 from left, insert -1 from right, then insert 2 from left and insert -2 from right a | anshu4intvcom | NORMAL | 2019-12-29T04:14:58.290894+00:00 | 2019-12-29T04:14:58.291004+00:00 | 11,448 | false | - start filling up from left and right complementary values (so if we insert 1 from left, insert -1 from right, then insert 2 from left and insert -2 from right and so on) :\n\n```\npublic int[] sumZero(int n) {\n int[] res = new int[n];\n int left = 0, right = n - 1, start = 1;\n while (left < rig... | 153 | 5 | [] | 9 |
find-n-unique-integers-sum-up-to-zero | Keep it simple. Add all values till n-1 and then balance it with -sum. | keep-it-simple-add-all-values-till-n-1-a-02jl | Edited. As @StefanPochmann pointed out rightly. starting from i+1 instead of i to avoid the case of 0 and 0 when n = 2.\n```\nclass Solution {\n public int[] | Nayanava | NORMAL | 2019-12-29T04:56:57.541423+00:00 | 2019-12-29T11:55:01.506084+00:00 | 7,223 | false | Edited. As @StefanPochmann pointed out rightly. starting from i+1 instead of i to avoid the case of 0 and 0 when n = 2.\n```\nclass Solution {\n public int[] sumZero(int n) {\n int arr[] = new int[n];\n int sum = 0;\n for(int i = 0; i < n-1; i++) {\n arr[i] = i+1;\n sum += ... | 70 | 6 | [] | 6 |
find-n-unique-integers-sum-up-to-zero | Trivial Python/Ruby/Java/C++ | trivial-pythonrubyjavac-by-stefanpochman-wmz5 | Just use the numbers 1, 2, ..., n-1 as well as their negated sum. I can\'t even be bothered to write the sum formula :-P\n\nPython\n\ndef sumZero(self, n):\n | stefanpochmann | NORMAL | 2019-12-29T11:42:16.225447+00:00 | 2019-12-29T18:15:04.664368+00:00 | 4,809 | false | Just use the numbers 1, 2, ..., n-1 as well as their negated sum. I can\'t even be bothered to write the sum formula :-P\n\n**Python**\n```\ndef sumZero(self, n):\n a = range(1, n)\n return a + [-sum(a)]\n```\n**Ruby**\nSlight variation since `2..n` is one character shorter than `1...n`.\n```\ndef sum_zero(n)\n ... | 55 | 6 | [] | 3 |
find-n-unique-integers-sum-up-to-zero | Java O(n) solution with explanation 0ms 100% | java-on-solution-with-explanation-0ms-10-sptv | Set the values at each sequential pair of indices such that they sum to 0 and each of these pairs is unique.\nIdea: use i,-i for the values at each pair of indi | eduardrg | NORMAL | 2020-11-03T18:17:58.694643+00:00 | 2020-11-03T18:17:58.694688+00:00 | 3,421 | false | Set the values at each sequential pair of indices such that they sum to 0 and each of these pairs is unique.\nIdea: use ```i,-i``` for the values at each pair of indices ```i, i+1```. \nThis won\'t work if ```i = 0``` because the first two elements would be ```0, 0```. Use ```i+1, -(i+1)``` instead to make it work for ... | 43 | 3 | ['Iterator', 'Java'] | 4 |
find-n-unique-integers-sum-up-to-zero | Python3 code: simple and readable with explanation | python3-code-simple-and-readable-with-ex-nvij | Explanation:\n# \nFirst, take a look at a few examples:\n\nn = 1: ans = [0]\nn = 2: ans = [-1,1]\nn = 3: ans = [-1,0,1]\nn = 4: ans = [-2,-1,1,2]\nn = 5: ans = | dr_sean | NORMAL | 2020-01-04T22:03:36.359658+00:00 | 2020-01-05T04:13:06.361346+00:00 | 7,298 | false | # Explanation:\n# \nFirst, take a look at a few examples:\n```\nn = 1: ans = [0]\nn = 2: ans = [-1,1]\nn = 3: ans = [-1,0,1]\nn = 4: ans = [-2,-1,1,2]\nn = 5: ans = [-2,-1,0,1,2]\n```\n\n- So, we should return an array where the values are symmetric. \n- If ```n%2``` is not equal to zero (n is odd), we append 0 to the ... | 40 | 2 | ['Python', 'Python3'] | 7 |
find-n-unique-integers-sum-up-to-zero | VERY SIMPLE C++ CODE- | very-simple-c-code-by-nisarg1406-lsol | DO UPVOTE IF YOU FIND IT USEFUL\n\nCODE - \n\nvector<int> sumZero(int n) {\n\t\tvector<int> res;\n if(n == 0) return res;\n if(n%2 != 0) res.push_ | nisarg1406 | NORMAL | 2020-08-29T04:43:02.565726+00:00 | 2020-08-29T04:43:28.873953+00:00 | 2,202 | false | **DO UPVOTE IF YOU FIND IT USEFUL**\n\nCODE - \n```\nvector<int> sumZero(int n) {\n\t\tvector<int> res;\n if(n == 0) return res;\n if(n%2 != 0) res.push_back(0); //if odd then to push 0\n for(int i=1;i<=floor(n/2);i++){\n res.push_back(i);\n res.push_back(-i);\n }\n ... | 26 | 1 | ['C', 'C++'] | 1 |
find-n-unique-integers-sum-up-to-zero | Easy to understand javascript beginner friendly | easy-to-understand-javascript-beginner-f-0ons | \n\nvar sumZero = function(n) {\n var num = Math.floor(n/2); \n var res = [];\n\n for(var i=1;i<=num;i++){\n res.push(i,-i)\n } \n\n if(n%2!==0){\n | yutoliho | NORMAL | 2020-01-03T00:37:57.480330+00:00 | 2020-01-03T00:37:57.480364+00:00 | 2,426 | false | \n```\nvar sumZero = function(n) {\n var num = Math.floor(n/2); \n var res = [];\n\n for(var i=1;i<=num;i++){\n res.push(i,-i)\n } \n\n if(n%2!==0){\n res.push(0)\n }\n \n return res \n}\n``` | 24 | 1 | ['JavaScript'] | 2 |
find-n-unique-integers-sum-up-to-zero | [Java/Python 3] 2 codes / language. | javapython-3-2-codes-language-by-rock-qso8 | Method 1:\njava\n public int[] sumZero(int n) {\n int[] ans = new int[n];\n for (int start = 0, end = n - 1; start < end; ++start, --end) {\n | rock | NORMAL | 2019-12-29T04:17:26.564681+00:00 | 2019-12-29T14:47:23.190887+00:00 | 1,971 | false | **Method 1:**\n```java\n public int[] sumZero(int n) {\n int[] ans = new int[n];\n for (int start = 0, end = n - 1; start < end; ++start, --end) {\n ans[start] = -end;\n ans[end] = end;\n }\n return ans;\n }\n```\n```python\n def sumZero(self, n: int) -> List[i... | 18 | 3 | [] | 2 |
find-n-unique-integers-sum-up-to-zero | Brain dead approach. Extendable to non zero sum. Beats 100% || Java | brain-dead-approach-extendable-to-non-ze-s0c7 | Intuition\n Describe your first thoughts on how to solve this problem. \n\nYou want ANY array? You will literally get just ANY array!\n\n# Approach\n Describe | pradyot21 | NORMAL | 2024-06-16T16:36:26.223938+00:00 | 2024-06-16T16:36:26.223971+00:00 | 1,050 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nYou want $$ANY$$ array? You will literally get just $$ ANY$$ array!\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe idea probably can\'t get simpler than this.\nWe know the resultant array will be of size *... | 15 | 0 | ['Array', 'Math', 'Java'] | 6 |
find-n-unique-integers-sum-up-to-zero | [Java] 0 ms, faster than 100.00% of Java online submissions | java-0-ms-faster-than-10000-of-java-onli-9pnz | ```\nclass Solution {\n public int[] sumZero(int n) {\n \n \n int[] ans = new int[n];\n for(int i=0; i<n; i++) {\n ans | anuragbhu | NORMAL | 2020-03-19T14:57:54.453615+00:00 | 2020-03-19T15:03:59.914115+00:00 | 1,813 | false | ```\nclass Solution {\n public int[] sumZero(int n) {\n \n \n int[] ans = new int[n];\n for(int i=0; i<n; i++) {\n ans[i] = (i*2)-n+1;\n }\n return ans;\n }\n} | 15 | 0 | ['Java'] | 5 |
find-n-unique-integers-sum-up-to-zero | Two Solutions in Python 3 (one line) (beats 100%) (24 ms) | two-solutions-in-python-3-one-line-beats-vz05 | Asymmetric List: (e.g. [1,2,3,4,5,-15])\n\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n return list(range(1,n))+[-n*(n-1)//2]\n\t\t\n\n | junaidmansuri | NORMAL | 2019-12-29T04:03:49.596115+00:00 | 2019-12-29T04:17:16.140242+00:00 | 2,101 | false | _Asymmetric List:_ (e.g. [1,2,3,4,5,-15])\n```\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n return list(range(1,n))+[-n*(n-1)//2]\n\t\t\n\n```\n_Symmetric List:_ (e.g. [-2,-1,0,1,2] or [-3,-2,-1,1,2,3])\n```\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n return list(r... | 14 | 0 | ['Python', 'Python3'] | 2 |
find-n-unique-integers-sum-up-to-zero | C++ || Easy || Explained || ✅ | c-easy-explained-by-tridibdalui04-znnb | 1. run a loop from 1 to n/2\n### 2. add -i and i in ans vector so that sum can be 0 at end\n#### 3. if size is given odd add 0 to the vector\n\n\n\nclass Solut | tridibdalui04 | NORMAL | 2022-10-19T12:30:23.717481+00:00 | 2022-10-19T12:30:23.717522+00:00 | 889 | false | ### 1. run a loop from 1 to n/2\n### 2. add -i and i in ans vector so that sum can be 0 at end\n#### 3. if size is given odd add 0 to the vector\n\n\n```\nclass Solution {\npublic:\n vector<int> sumZero(int n) {\n vector<int>ans;\n for(int i=1;i<=n/2;i++)\n {\n ans.push_back(-i);\n ... | 12 | 0 | ['Array', 'C'] | 0 |
find-n-unique-integers-sum-up-to-zero | Java || easy to understand || faster than 100% || simple answer. | java-easy-to-understand-faster-than-100-pfk2i | \nclass Solution {\n public int[] sumZero(int n) {\n \n int[] ans = new int[n];\n int start = 0;\n int end = n - 1;\n \n | iamridoydey | NORMAL | 2022-10-09T11:43:12.585540+00:00 | 2022-10-09T11:43:12.585584+00:00 | 1,623 | false | ```\nclass Solution {\n public int[] sumZero(int n) {\n \n int[] ans = new int[n];\n int start = 0;\n int end = n - 1;\n \n while(start < end){\n ans[start] = start + 1;\n ans[end] = ans[start] * (-1);\n start++;\n end--;\n ... | 12 | 0 | ['Array', 'Java'] | 0 |
find-n-unique-integers-sum-up-to-zero | Swift: Find N Unique Integers Sum up to Zero | swift-find-n-unique-integers-sum-up-to-z-nfu6 | swift\nclass Solution {\n func sumZero(_ n: Int) -> [Int] {\n if n <= 1 && 1000 >= n { return [0] }\n var sum = [Int](repeating: 0, count: n)\n | AsahiOcean | NORMAL | 2021-04-08T20:49:49.148081+00:00 | 2021-04-08T21:54:22.291456+00:00 | 477 | false | ```swift\nclass Solution {\n func sumZero(_ n: Int) -> [Int] {\n if n <= 1 && 1000 >= n { return [0] }\n var sum = [Int](repeating: 0, count: n)\n for i in 1...(n >> 1) {\n sum[i-1] = i\n sum[n-i] = -i\n }\n return sum\n }\n}\n``` | 12 | 1 | ['Swift'] | 1 |
find-n-unique-integers-sum-up-to-zero | Fast python | fast-python-by-travelxcodes-jqse | \nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n a=[]\n if n%2!=0:\n a.append(0)\n for i in range(1,n,2):\n | travelXcodes | NORMAL | 2022-11-01T04:01:25.909746+00:00 | 2022-11-01T04:01:25.909784+00:00 | 960 | false | ```\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n a=[]\n if n%2!=0:\n a.append(0)\n for i in range(1,n,2):\n a.append(i)\n a.append(i*(-1))\n return a\n``` | 11 | 0 | [] | 0 |
find-n-unique-integers-sum-up-to-zero | Java: fast, clean and short - symmetric array handles both parities | java-fast-clean-and-short-symmetric-arra-31km | Intuition: We can solve this by using pairs of opposites counting away from zero starting with 1, -1 then 2, -2, etc. If we have an odd sized array, we can add | mattihito | NORMAL | 2022-05-13T07:04:17.489864+00:00 | 2022-10-27T02:55:15.851467+00:00 | 424 | false | **Intuition**: We can solve this by using pairs of opposites counting away from zero starting with 1, -1 then 2, -2, etc. If we have an odd sized array, we can add a zero. But what if we like the idea of our array being sorted for easier readability? We can count the left side up starting at -n/2 and increasing left... | 11 | 0 | ['Java'] | 0 |
find-n-unique-integers-sum-up-to-zero | Python Solution, Super fast and simple | python-solution-super-fast-and-simple-by-zb43 | \nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n a=[]\n if n%2!=0:\n a.append(0)\n for i in range(1,n):\n | excalibur12 | NORMAL | 2020-06-02T02:12:41.423263+00:00 | 2020-06-02T02:12:41.423312+00:00 | 1,200 | false | ```\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n a=[]\n if n%2!=0:\n a.append(0)\n for i in range(1,n):\n if len(a)==n:\n break\n a.append(i)\n a.append(-i)\n return a\n``` | 9 | 1 | ['Python'] | 2 |
find-n-unique-integers-sum-up-to-zero | C++ 100% solution in 3 lines | c-100-solution-in-3-lines-by-xzcvbzfgefd-zy0b | its just a basic arithmetic series. the ans vector is initialized with the negative sum of all integers up to n - 2. The positive sum can be derived from the in | xzcvbzfgefd | NORMAL | 2020-05-29T00:30:13.447653+00:00 | 2020-05-29T00:31:04.290885+00:00 | 1,203 | false | its just a basic arithmetic series. the ans vector is initialized with the negative sum of all integers up to n - 2. The positive sum can be derived from the integers attained in the loop. These two sets cancel out each other resulting in a total sum of 0.\n\nclass Solution {\npublic:\n vector<int> sumZero(int n) { ... | 9 | 3 | ['C'] | 1 |
find-n-unique-integers-sum-up-to-zero | JAVA 100% FASTER and EASY Solution with EXPLANATION | java-100-faster-and-easy-solution-with-e-tdni | JAVA SOLUTION @DeepakKumar\n# In Case of Any Doubt Feel Free to ASK ...\n\n\nclass Solution {\n public int[] sumZero(int n) {\n// Declaring Size of Array of | Deepak2002 | NORMAL | 2021-11-02T15:03:30.477393+00:00 | 2021-11-02T15:03:30.477460+00:00 | 708 | false | # JAVA SOLUTION @DeepakKumar\n# In Case of Any Doubt Feel Free to ASK ...\n\n```\nclass Solution {\n public int[] sumZero(int n) {\n// Declaring Size of Array of SIZE n\n int [] resultantArray = new int[n];\n int num = 1; // Start Numbers to Fill from 1\n int start = 0; // Tak... | 8 | 0 | ['Java'] | 0 |
find-n-unique-integers-sum-up-to-zero | C++ 0 ms Faster than 100% of submissions | c-0-ms-faster-than-100-of-submissions-by-f90s | \nclass Solution {\npublic:\n vector<int> sumZero(int n) {\n int sum = 0;\n int cnt = 1;\n vector<int> v(n,0);\n for (int i = 0; | tempatron | NORMAL | 2020-09-13T11:29:52.356052+00:00 | 2020-09-13T11:30:04.888314+00:00 | 1,045 | false | ```\nclass Solution {\npublic:\n vector<int> sumZero(int n) {\n int sum = 0;\n int cnt = 1;\n vector<int> v(n,0);\n for (int i = 0; i < n ; ++i) {\n if (i != n-1) {\n v[i] = cnt;\n sum += v[i];\n cnt++;\n } else {\n ... | 8 | 0 | ['C'] | 1 |
find-n-unique-integers-sum-up-to-zero | [Python] Simple Solution. Key is to not overthink. (32ms, 14MB) | python-simple-solution-key-is-to-not-ove-rrxs | The key is to not overthink things. As the problem said, you can return any list.\n\nWe only want to iterate until n // 2 because we want half to be positive an | seankala | NORMAL | 2020-08-26T09:17:04.770651+00:00 | 2020-08-26T09:17:04.770693+00:00 | 575 | false | The key is to not overthink things. As the problem said, you can return __*any*__ list.\n\nWe only want to iterate until `n // 2` because we want half to be positive and the other half to be negative integers (excluding 0). For every positive integer, just add its negative counterpart.\n\nIf `n` is an odd number, just ... | 8 | 0 | ['Python', 'Python3'] | 0 |
find-n-unique-integers-sum-up-to-zero | Two JS Solutions | two-js-solutions-by-hbjorbj-j4zf | \n/*\nWe can simply add 1 to n-1 and keep track of this array sum and add the negation of that.\n*/\nvar sumZero = function(n) {\n let sum = 0, res = [];\n | hbjorbj | NORMAL | 2020-03-30T21:48:17.897318+00:00 | 2021-05-19T12:15:37.857069+00:00 | 872 | false | ```\n/*\nWe can simply add 1 to n-1 and keep track of this array sum and add the negation of that.\n*/\nvar sumZero = function(n) {\n let sum = 0, res = [];\n for (let i = 1; i < n; i++) {\n res.push(i);\n sum += i;\n }\n res.push(-sum);\n return res;\n // T.C: O(N)\n // S.C: O(N)\n};... | 8 | 0 | ['JavaScript'] | 3 |
find-n-unique-integers-sum-up-to-zero | Easy Peasy Python Solution ^_^ | easy-peasy-python-solution-_-by-oshine_c-rrvy | Intuition\nHere we get two cases:\nCase 1: \nIf number of elements is odd.\nWe will add a zero at the beginning and rest of the elements will be negative and po | oshine_chan | NORMAL | 2023-07-31T08:27:44.795716+00:00 | 2023-08-02T18:35:01.353308+00:00 | 471 | false | # Intuition\nHere we get two cases:\nCase 1: \nIf number of elements is odd.\nWe will add a zero at the beginning and rest of the elements will be negative and postive integers of ceiling(n/2).\n\nCase 2:\nIf number of elements is even.\nWe will directly add the elements that will be negative and postive integers of ce... | 7 | 0 | ['Python3'] | 3 |
find-n-unique-integers-sum-up-to-zero | JAVA 100% faster solution | java-100-faster-solution-by-bathija111-pyp2 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe most important thing to remember here is that when we initialize array in JAVA all | bathija111 | NORMAL | 2023-01-23T07:09:42.148009+00:00 | 2023-01-23T07:09:42.148041+00:00 | 1,186 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe most important thing to remember here is that when we initialize array in JAVA all the elements are set to zero initially.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIn this approach we are setting two ele... | 7 | 0 | ['Java'] | 1 |
find-n-unique-integers-sum-up-to-zero | JAVA || 100% FASTER || USING IF / ELSE CONDITION. | java-100-faster-using-if-else-condition-96ii7 | Upvote this solution, if you like it, that will make me happy.\nHappy Learning\n\n\n# Code\n\nclass Solution {\n public int[] sumZero(int n) {\n int[] | sharforaz_rahman | NORMAL | 2022-11-30T05:48:26.501614+00:00 | 2022-11-30T05:48:26.501659+00:00 | 1,226 | false | Upvote this solution, if you like it, that will make me happy.\nHappy Learning\n\n\n# Code\n```\nclass Solution {\n public int[] sumZero(int n) {\n int[] new_array = new int[n];\n\n int length_for_even = n / 2;\n int length_for_odd = n / 2;\n if (n % 2 != 0) {\n\n for (int i = ... | 7 | 0 | ['Java'] | 0 |
find-n-unique-integers-sum-up-to-zero | Easy Python Solution (using range function) - faster + less memory than 95% | easy-python-solution-using-range-functio-o1k1 | \nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n first_part = list(range(1, n))\n second_part = -sum(first_part)\n return f | ibizak | NORMAL | 2021-06-10T13:22:08.456371+00:00 | 2021-06-10T13:22:08.456417+00:00 | 353 | false | ```\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n first_part = list(range(1, n))\n second_part = -sum(first_part)\n return first_part + [second_part]\n \n``` | 7 | 0 | ['Array', 'Python'] | 1 |
find-n-unique-integers-sum-up-to-zero | ✅REALLY easy solution✅ | really-easy-solution-by-parijdev-924s | \n# Code\n\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n mass = []\n if n % 2 != 0:\n mass.append(0)\n for i i | parijdev | NORMAL | 2024-03-24T18:16:12.238012+00:00 | 2024-03-24T18:16:12.238043+00:00 | 378 | false | \n# Code\n```\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n mass = []\n if n % 2 != 0:\n mass.append(0)\n for i in range(n):\n if len(mass) != n:\n mass.append(-i-1)\n mass.append(i+1)\n return mass\n```\n\n![photo_2024-03... | 6 | 0 | ['Python3'] | 2 |
find-n-unique-integers-sum-up-to-zero | C++ and C# very easy solution.100% faster. | c-and-c-very-easy-solution100-faster-by-d9ztw | \n\nC++ []\nclass Solution {\npublic:\n vector<int> sumZero(int n) {\n vector<int> answer(n);\n int x;\n if(n%2==0){\n x=n/2;\n | aloneguy | NORMAL | 2023-04-16T18:21:27.983608+00:00 | 2023-04-16T18:21:27.983679+00:00 | 1,319 | false | \n\n```C++ []\nclass Solution {\npublic:\n vector<int> sumZero(int n) {\n vector<int> answer(n);\n int x;\n if(n%2==0){\n x=n/2;\n answer[n-1]=0;\n ... | 6 | 0 | ['Math', 'C++', 'C#'] | 0 |
find-n-unique-integers-sum-up-to-zero | [Swift] Faster than 100% | swift-faster-than-100-by-blackbirdng-86q2 | <- Please vote if my solution was helpful to you.\n\nclass Solution {\n func sumZero(_ n: Int) -> [Int] {\n var result = [Int]()\n var sum = 0\ | blackbirdNG | NORMAL | 2021-08-01T17:09:37.182322+00:00 | 2021-08-01T17:11:45.098941+00:00 | 159 | false | <- Please vote if my solution was helpful to you.\n```\nclass Solution {\n func sumZero(_ n: Int) -> [Int] {\n var result = [Int]()\n var sum = 0\n for i in 1..<n {\n result.append(i)\n sum += i\n }\n result.append(-sum)\n return result\n }\n}\n```\n... | 6 | 0 | ['Swift'] | 1 |
find-n-unique-integers-sum-up-to-zero | 1304 | JavaScript 1-Line Solution | 1304-javascript-1-line-solution-by-spork-suu9 | Now updated to work with the newly discovered integer "2".\n\n> Runtime: 74 ms, faster than 69.44% of JavaScript online submissions\n> Memory Usage: 44.3 MB, le | sporkyy | NORMAL | 2020-03-02T18:07:07.308647+00:00 | 2022-04-26T20:53:57.602439+00:00 | 629 | false | Now updated to work with the newly discovered integer "2".\n\n> Runtime: **74 ms**, faster than *69.44%* of JavaScript online submissions\n> Memory Usage: **44.3 MB**, less than *5.61%* of JavaScript online submissions\n\n```javascript\n/**\n * @param {number} n\n * @return {number[]}\n */\nconst sumZero = n => [...new... | 6 | 0 | ['JavaScript'] | 4 |
find-n-unique-integers-sum-up-to-zero | [Python 3]. Simple. O(n). 28ms | python-3-simple-on-28ms-by-rohin7-x9l0 | Explanation:\n* Return an arithmetic progression with common difference 2, centered around 0\n\n\nclass Solution:\n def sumZero(self, n):\n result=[]\ | rohin7 | NORMAL | 2019-12-29T05:41:34.790260+00:00 | 2019-12-29T05:54:44.723492+00:00 | 1,071 | false | Explanation:\n* Return an arithmetic progression with common difference 2, centered around 0\n\n```\nclass Solution:\n def sumZero(self, n):\n result=[]\n _int_=-n+1\n for i in range(n):\n result.append(_int_)\n _int_=_int_+2\n return result\n``` | 6 | 2 | ['Python', 'Python3'] | 3 |
find-n-unique-integers-sum-up-to-zero | C++||Easy to Understand | ceasy-to-understand-by-return_7-9lpq | ```\nclass Solution {\npublic:\n vector sumZero(int n) \n {\n vector ans(n);\n for(int i=0;i<n;i++)\n {\n ans[i]=i*2-n+1;\ | return_7 | NORMAL | 2022-09-13T19:46:16.017323+00:00 | 2022-09-13T19:46:16.017366+00:00 | 237 | false | ```\nclass Solution {\npublic:\n vector<int> sumZero(int n) \n {\n vector<int> ans(n);\n for(int i=0;i<n;i++)\n {\n ans[i]=i*2-n+1;\n }\n return ans;\n \n }\n};\n//if you like the solution plz upvote. | 5 | 0 | ['C'] | 0 |
find-n-unique-integers-sum-up-to-zero | Java Simple Easy Math | java-simple-easy-math-by-himanshu_pandey-o2t5 | ```class Solution {\n public int[] sumZero(int n) {\n int[] result =new int[n];\n int i=0;\n for ( i=0; i< n-1;i++){\n result[ | Himanshu_Pandey1995 | NORMAL | 2022-04-21T05:44:49.738666+00:00 | 2022-04-21T05:44:49.738712+00:00 | 230 | false | ```class Solution {\n public int[] sumZero(int n) {\n int[] result =new int[n];\n int i=0;\n for ( i=0; i< n-1;i++){\n result[i]=i-n;\n result[i+1]=n-i;\n i++;\n }\n return result;\n }\n} | 5 | 0 | ['Java'] | 0 |
find-n-unique-integers-sum-up-to-zero | Find N Unique Integers Sum up to Zero | Simple Java Solution | Beats 100% | find-n-unique-integers-sum-up-to-zero-si-lsf1 | We have to fill the array with equal number of Postive and Negative numbers. Example: if the value of n is 4, then we can have a array [-1, 1, -2, 2]. If "n" is | deleted_user | NORMAL | 2021-08-29T14:04:55.145546+00:00 | 2021-09-02T13:23:51.747112+00:00 | 297 | false | We have to fill the array with equal number of Postive and Negative numbers. Example: if the value of n is 4, then we can have a array [-1, 1, -2, 2]. If "n" is odd, we will add \'0\' to the array. Example: n = 5, then we can have na array with the following values [-1, 1, -2, 2, 0]\n\n```\nclass Solution {\n public... | 5 | 0 | ['Java'] | 0 |
find-n-unique-integers-sum-up-to-zero | Java solution, faster than 100% | java-solution-faster-than-100-by-optimal-8csj | class Solution {\n public int[] sumZero(int n) {\n \n\t\tint[] res = new int[n];\n \n int cur = 0;\n if(n%2!=0) {\n res | optimalsis | NORMAL | 2020-04-10T02:29:56.615417+00:00 | 2020-04-10T02:29:56.615473+00:00 | 321 | false | class Solution {\n public int[] sumZero(int n) {\n \n\t\tint[] res = new int[n];\n \n int cur = 0;\n if(n%2!=0) {\n res[cur] = 0;\n cur++;\n }\n \n for(int i=1; i<=n/2; i++) {\n res[cur++] = i;\n res[cur++] = -i;\n }\n... | 5 | 1 | [] | 2 |
find-n-unique-integers-sum-up-to-zero | Simple Java Solution | simple-java-solution-by-mugdhagovilkar11-bs01 | \nclass Solution {\n public int[] sumZero(int n) {\n int[] ans = new int[n];\n int num = n/2;\n int index = 0;\n while(num>0)\n | mugdhagovilkar1197 | NORMAL | 2020-01-09T03:26:03.384783+00:00 | 2020-01-09T03:26:03.384851+00:00 | 456 | false | ```\nclass Solution {\n public int[] sumZero(int n) {\n int[] ans = new int[n];\n int num = n/2;\n int index = 0;\n while(num>0)\n {\n ans[index++] = num;\n ans[index++] = num*-1;\n num--;\n }\n if(n%2 == 1)\n ans[index++] ... | 5 | 0 | ['Java'] | 1 |
find-n-unique-integers-sum-up-to-zero | JavaScript Solution | javascript-solution-by-ehdwn1212-gbsq | js\nvar sumZero = function(n) {\n const uniqueIntegers = [];\n const half = parseInt(n / 2);\n \n for (let i = 1; i <= half; i++) {\n uniqueI | ehdwn1212 | NORMAL | 2020-01-06T16:43:31.275727+00:00 | 2020-01-06T16:43:31.275775+00:00 | 703 | false | ```js\nvar sumZero = function(n) {\n const uniqueIntegers = [];\n const half = parseInt(n / 2);\n \n for (let i = 1; i <= half; i++) {\n uniqueIntegers.push(i);\n uniqueIntegers.push(-i);\n }\n \n if (n % 2) {\n uniqueIntegers.push(0);\n }\n \n return uniqueIntegers;\n... | 5 | 0 | ['JavaScript'] | 0 |
find-n-unique-integers-sum-up-to-zero | One Line Rust Solution | one-line-rust-solution-by-wfxr-sx3k | rust\npub fn sum_zero(n: i32) -> Vec<i32> {\n\t(1 - n..n).step_by(2).collect()\n}\n | wfxr | NORMAL | 2019-12-31T02:07:31.568002+00:00 | 2019-12-31T02:07:31.568058+00:00 | 90 | false | ```rust\npub fn sum_zero(n: i32) -> Vec<i32> {\n\t(1 - n..n).step_by(2).collect()\n}\n``` | 5 | 0 | [] | 1 |
find-n-unique-integers-sum-up-to-zero | C# Solution | c-solution-by-leonhard_euler-tf05 | \npublic class Solution \n{\n public int[] SumZero(int n) \n {\n var result = new List<int>();\n for(int i = 1; i <= n/2; i++)\n {\n | Leonhard_Euler | NORMAL | 2019-12-29T04:57:51.542216+00:00 | 2019-12-29T04:58:03.905175+00:00 | 736 | false | ```\npublic class Solution \n{\n public int[] SumZero(int n) \n {\n var result = new List<int>();\n for(int i = 1; i <= n/2; i++)\n {\n result.Add(i);\n result.Add(-i);\n }\n \n if(n % 2 == 1) result.Add(0);\n return result.ToArray();\n }\n... | 5 | 0 | [] | 3 |
find-n-unique-integers-sum-up-to-zero | The most simplest way | the-most-simplest-way-by-nurzhansultanov-3lye | \n\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n result = []\n if n%2==0:\n for i in range(1,(n//2)+1):\n | NurzhanSultanov | NORMAL | 2024-02-06T17:11:00.140312+00:00 | 2024-02-06T17:11:00.140334+00:00 | 412 | false | \n```\nclass Solution:\n def sumZero(self, n: int) -> List[int]:\n result = []\n if n%2==0:\n for i in range(1,(n//2)+1):\n result.append(i)\n result.append((-1)*i)\n result.sort()\n return result\n else:\n for i in range(... | 4 | 0 | ['Python3'] | 0 |
find-n-unique-integers-sum-up-to-zero | BEATS 100% | beats-100-by-nitish_reddy27-8mvg | 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 | nitish_reddy27 | NORMAL | 2023-07-29T08:25:04.371192+00:00 | 2023-07-29T08:31:12.449755+00:00 | 823 | 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: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here... | 4 | 0 | ['Java'] | 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.