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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
get-maximum-in-generated-array | [C++] DP Solution Explained | c-dp-solution-explained-by-ajna-jhep | Kinda of a dull, boring problem, but I get practice is made of that too.\n\nFirst we want to get 2 edge cases out the way, with the first in particular being ot | ajna | NORMAL | 2021-01-16T20:20:39.750392+00:00 | 2021-07-17T12:24:27.487040+00:00 | 3,384 | false | Kinda of a dull, boring problem, but I get practice is made of that too.\n\nFirst we want to get 2 edge cases out the way, with the first in particular being otherwise annoying: when we are given `n < 2`, we can just return `n`.\n\nIn all the other cases, we will create 2 variables:\n* `arr` is an array of `n + 1` (since we count `0`-indexed, but still up to `n`) array we will go populate with the values in the series;\n* `m` will store the currently run maximum.\n\nWe will set the first 2 values of `arr` as instructed (`1` in particular would fail every time we would be given `0`, so that is why we wanted to get rid of it).\n\nWe will then loop, proceed to create each element from `2` to `n` as instructed and update `m` accordingly.\n\nOnce done, we can just return `m` :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n // base cases\n if (n < 2) return n;\n // support variables\n int arr[n + 1], m;\n arr[0] = 0, arr[1] = 1;\n // building arr\n for (int i = 2; i <= n; i++) {\n if (i % 2) arr[i] = arr[i / 2] + arr[i / 2 + 1];\n else arr[i] = arr[i / 2];\n // updating m\n m = max(arr[i], m);\n }\n return m;\n }\n};\n``` | 20 | 0 | ['Dynamic Programming', 'C', 'C++'] | 3 |
get-maximum-in-generated-array | ✅Python3 || 23ms/Beats 97.74% | python3-23msbeats-9774-by-abdullayev_akb-vnm8 | \n\nPlease UPVOTE if it helps \u2764\uFE0F\uD83D\uDE0A\nThank You and Happy To Help You!!\n\n# Python3\n\nclass Solution:\n def getMaximumGenerated(self, n: | abdullayev_akbar | NORMAL | 2023-02-08T12:46:26.510583+00:00 | 2023-02-08T12:56:02.041423+00:00 | 1,454 | false | \n\nPlease UPVOTE if it helps \u2764\uFE0F\uD83D\uDE0A\nThank You and Happy To Help You!!\n\n# Python3\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n dp=[0,1]\n for i in range(2,n + 1):\n dp + =[dp[i//2] if i % 2==0 else dp[i//2] + dp[i//2 + 1]]\n return max(dp[:n + 1])\n\n``` | 15 | 0 | ['Python3'] | 0 |
get-maximum-in-generated-array | Simple Java solution | simple-java-solution-by-ottomonempire-6uah | As you read the problem statement, the algorithm will be same, if we follow it. \nBelow is the easy to understand solution. However feel free to optimise this f | ottomonempire | NORMAL | 2020-11-08T04:10:31.134944+00:00 | 2021-01-15T12:28:42.352009+00:00 | 2,880 | false | As you read the problem statement, the algorithm will be same, if we follow it. \nBelow is the easy to understand solution. However feel free to optimise this further and come up with better solution.\nSpace complexity will be O(n+1).\n\n```\n public int getMaximumGenerated(int n) {\n if (n == 0)\n return 0;\n if (n == 1)\n return 1;\n int[] nums = new int[n + 1];\n nums[0] = 0;\n nums[1] = 1;\n int ans = 1;\n for (int i = 1; (2 * i + 1) <= n; i++) {\n nums[2 * i] = nums[i];\n nums[2 * i + 1] = nums[i] + nums[i + 1];\n ans = Math.max(ans, nums[2 * i + 1]);\n }\n return ans;\n }\n``` | 14 | 1 | ['Dynamic Programming', 'Java'] | 4 |
get-maximum-in-generated-array | Python generators are like space shuttles | python-generators-are-like-space-shuttle-pkyv | Constructive solutions were posted before, but did you know that generators allow you get rid of used array parts, like a space shuttle gets rid of spent fuel t | haiduc | NORMAL | 2021-01-15T21:34:30.880264+00:00 | 2023-05-09T18:52:17.963802+00:00 | 733 | false | Constructive solutions were posted before, but did you know that generators allow you get rid of used array parts, like a space shuttle gets rid of spent fuel tanks?\n\n```\nfrom queue import deque\n\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n \n def arrgen(n):\n yield 0\n q = deque([1])\n while True:\n yield q[0]\n q.append(q[0])\n q.append(q[0]+q[1])\n q.popleft()\n \n g = arrgen(n)\n return max(next(g) for _ in range(n+1)) | 13 | 0 | ['Python3'] | 1 |
get-maximum-in-generated-array | Simulate the Generated Array.. | simulate-the-generated-array-by-admin007-4bsu | \n public int getMaximumGenerated(int n) {\n if (n <= 1) return n;\n int[] nums = new int[n + 1];\n nums[1] = 1;\n int max = 1;\n | admin007 | NORMAL | 2020-11-08T04:33:30.077446+00:00 | 2021-01-15T21:20:28.602333+00:00 | 1,531 | false | ```\n public int getMaximumGenerated(int n) {\n if (n <= 1) return n;\n int[] nums = new int[n + 1];\n nums[1] = 1;\n int max = 1;\n for (int i = 1; i < (n + 1) / 2; i++) {\n if (2 * i <= n) {\n nums[2 * i] = nums[i];\n max = Math.max(max, nums[2 * i]);\n }\n if (2 * i + 1 <= n) {\n nums[2 * i + 1] = nums[i] + nums[i + 1];\n max = Math.max(max, nums[2 * i + 1]);\n } \n }\n return max;\n }\n``` | 11 | 3 | [] | 3 |
get-maximum-in-generated-array | Easy Python Solution O(n) with Explanation | easy-python-solution-on-with-explanation-y0vn | \tclass Solution:\n\t\tdef getMaximumGenerated(self, n: int) -> int:\n\t\t\tif n==0: return 0\n\t\t\tarr = [0, 1]\n\t\t\tfor i in range(2, n+1):\n\t\t\t\tif i%2 | shadow_sm36 | NORMAL | 2020-11-08T04:25:43.249269+00:00 | 2020-11-08T04:25:43.249317+00:00 | 2,202 | false | \tclass Solution:\n\t\tdef getMaximumGenerated(self, n: int) -> int:\n\t\t\tif n==0: return 0\n\t\t\tarr = [0, 1]\n\t\t\tfor i in range(2, n+1):\n\t\t\t\tif i%2==0:\n\t\t\t\t\tarr.append(arr[i//2])\n\t\t\t\telse:\n\t\t\t\t\tarr.append(arr[i//2] + arr[i//2 + 1])\n\t\t\treturn max(arr)\n\t\t\t\nThe Basic Idea is that given our constraints and the Question itself, we can simply just generate the array by simulating what is needed. So, we start with our base array of `[0, 1]` and for any `N > 1` we generate the array as following:\n* If our index is even: we divide the index by 2 and append that result to our base array.\n* If our index is odd: we divide the index by 2 ( and considering only the integer of it, so 7//2 = 3 and not 3.5 ) and use that index and the next of that index.\n\nWe hence generate the entire array from 0 to N, so N+1 elements, and our job now is to only find the maximum of this array. \n\n**Note:** This takes a O(N) time and O(N) space in generating the array. | 10 | 2 | ['Python', 'Python3'] | 6 |
get-maximum-in-generated-array | [C++] Easy Solution - DP | c-easy-solution-dp-by-afrozchakure-3pmb | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n == 0) return 0;\n vector<int> f(n+1, 0);\n f[0] = 0;\n f[1] | afrozchakure | NORMAL | 2020-11-08T04:12:49.759453+00:00 | 2021-01-15T12:24:32.394560+00:00 | 1,939 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n == 0) return 0;\n vector<int> f(n+1, 0);\n f[0] = 0;\n f[1] = 1;\n int maximum = 1;\n for(int i=1; i<= n /2; i++)\n {\n if(i*2 > n || (2*i + 1) > n)\n break;\n f[i*2] = f[i];\n f[i*2 + 1] = f[i] + f[i+1];\n \n maximum = max(maximum, f[2*i + 1]);\n }\n return maximum;\n }\n \n};\n\n``` | 9 | 2 | ['Dynamic Programming', 'C', 'C++'] | 2 |
get-maximum-in-generated-array | Python simple solution | python-simple-solution-by-tovam-qbaj | Python :\n\n\ndef getMaximumGenerated(self, n: int) -> int:\n\tif not n:\n\t\treturn n\n\n\tnums = [0] * (n + 1)\n\tnums[1] = 1\n\n\tfor i in range(2, n + 1): | TovAm | NORMAL | 2021-11-03T23:54:15.784432+00:00 | 2021-11-03T23:54:15.784471+00:00 | 1,042 | false | **Python :**\n\n```\ndef getMaximumGenerated(self, n: int) -> int:\n\tif not n:\n\t\treturn n\n\n\tnums = [0] * (n + 1)\n\tnums[1] = 1\n\n\tfor i in range(2, n + 1): \n\t\tif i % 2:\n\t\t\tnums[i] = nums[i // 2] + nums[i // 2 + 1]\n\n\t\telse:\n\t\t\tnums[i] = nums[i // 2]\n\n\treturn max(nums)\n```\n\n**Like it ? please upvote !** | 8 | 0 | ['Python', 'Python3'] | 0 |
get-maximum-in-generated-array | Awesome Code--->Python3 | awesome-code-python3-by-ganjinaveen-wthy | 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 | GANJINAVEEN | NORMAL | 2023-03-17T07:33:15.497555+00:00 | 2023-03-17T07:33:15.497605+00:00 | 699 | 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)$$ -->\n\n# Code\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n<2:\n return n\n nums=[0]*(n+1)\n nums[0]=0\n nums[1]=1\n for i in range(2,n+1):\n if i%2==0:\n nums[i]=nums[i//2]\n else:\n nums[i]=nums[i//2]+nums[i//2+1]\n return max(nums)\n #please upvote me it would encourage me alot\n\n``` | 6 | 0 | ['Python3'] | 0 |
get-maximum-in-generated-array | Python3 O(n) space & time | python3-on-space-time-by-dark_wolf_jss-miac | \nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n nums = [0,1]\n output = 0\n for i in range(2,n+1):\n toAp | dark_wolf_jss | NORMAL | 2021-12-11T12:47:48.655359+00:00 | 2021-12-11T12:52:53.888456+00:00 | 184 | false | ```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n nums = [0,1]\n output = 0\n for i in range(2,n+1):\n toAppend = nums[i//2] if i%2 == 0 else nums[i//2] + nums[1+(i//2)]\n output = max(output, toAppend)\n nums.append(toAppend)\n return n if n<=1 else output\n``` | 5 | 0 | [] | 0 |
get-maximum-in-generated-array | Java | Beats 100% | Easy iteration | java-beats-100-easy-iteration-by-u99omeg-0454 | \nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0 || n==1) return n;\n \n int nums[]=new int [n+1];\n \n | U99Omega | NORMAL | 2021-08-22T22:04:06.429613+00:00 | 2021-08-22T22:04:06.429644+00:00 | 758 | false | ```\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0 || n==1) return n;\n \n int nums[]=new int [n+1];\n \n nums[0]=0;\n nums[1]=1;\n int max=Integer.MIN_VALUE;\n \n for(int i=2;i<=n;i++){\n if(i%2==0){\n nums[i]=nums[i/2];\n }\n else{\n nums[i]=nums[i/2]+nums[i/2 + 1];\n }\n max=Math.max(max,nums[i]);\n }\n return max;\n }\n}\n``` | 5 | 0 | ['Dynamic Programming', 'Java'] | 0 |
get-maximum-in-generated-array | [Scala] self-referential lazy sequence | scala-self-referential-lazy-sequence-by-385vy | scala\nobject Solution {\n lazy val seq: LazyList[Int] = LazyList(0, 1, 1) #::: seq.tail.zip(seq.drop(2)).flatMap { case (a, b) => Array(a + b, b) }\n def get | nizhnik | NORMAL | 2021-01-15T09:04:30.990559+00:00 | 2021-01-15T09:04:30.990584+00:00 | 328 | false | ```scala\nobject Solution {\n lazy val seq: LazyList[Int] = LazyList(0, 1, 1) #::: seq.tail.zip(seq.drop(2)).flatMap { case (a, b) => Array(a + b, b) }\n def getMaximumGenerated(n: Int): Int = seq.take(n + 1).max\n}\n``` | 5 | 0 | ['Scala'] | 0 |
get-maximum-in-generated-array | C++ Faster Than 100% | c-faster-than-100-by-theriley106-h0a3 | cpp\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n int array[n+1];\n int largestNum = 0;\n \n for (int i=0; i<n+ | theriley106 | NORMAL | 2020-11-09T01:21:48.880652+00:00 | 2020-11-09T01:21:48.880700+00:00 | 800 | false | ```cpp\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n int array[n+1];\n int largestNum = 0;\n \n for (int i=0; i<n+1; i++){\n if (i==0){\n array[0] = 0;\n } else if (i==1){\n array[1] = 1;\n }\n \n if (2 <= 2 * i and 2 * i <= n){\n array[i * 2] = array[i];\n }\n \n if (2 <= 2 * i + 1 and 2 * i + 1 <= n){\n array[2*i+1] = array[i] + array[i+1];\n }\n }\n \n for (int i=0; i<n+1; i++){\n if (array[i] > largestNum){\n largestNum = array[i];\n }\n }\n \n return largestNum;\n }\n};\n``` | 5 | 0 | ['C'] | 1 |
get-maximum-in-generated-array | simple java solution || space optimized dynamic programming 100% faster | simple-java-solution-space-optimized-dyn-jvp1 | ```\n// if you found my solution usefull please upvote it\nclass Solution \n{\n public int getMaximumGenerated(int n) \n {\n if(n==0) return 0; // | Haswanth_kumar | NORMAL | 2022-10-18T18:22:11.337998+00:00 | 2022-10-18T18:22:53.093410+00:00 | 795 | false | ```\n// if you found my solution usefull please upvote it\nclass Solution \n{\n public int getMaximumGenerated(int n) \n {\n if(n==0) return 0; // base case\n int[] nums=new int[n+1];\n nums[0]=0;\n nums[1]=1;\n int idx=1;\n int cnt=0;\n int ans=1;\n for(int i=2;i<nums.length;i++)\n {\n if(i%2==0) nums[i]=nums[idx];\n else \n nums[i]=nums[idx]+nums[idx+1];\n cnt++;\n if(cnt==2)\n {\n cnt=0;\n idx++;\n }\n ans=Math.max(ans,nums[i]);\n }\n return ans;\n }\n} | 4 | 0 | ['Dynamic Programming', 'Java'] | 2 |
get-maximum-in-generated-array | Easy for beginnners [C++] MOST OPTIMIZED 0ms 100% | easy-for-beginnners-c-most-optimized-0ms-abct | Idea\nIdea behind the solution is pretty easy - maintain an array of already obtained values to shortcut calculation on each step.\n## Important lesson for begi | sne4kers | NORMAL | 2022-10-16T21:55:34.809506+00:00 | 2023-06-06T21:48:30.864468+00:00 | 781 | false | ## Idea\nIdea behind the solution is pretty easy - maintain an array of already obtained values to shortcut calculation on each step.\n## Important lesson for beginners\nAlthough the division with `/ 2` and `% 2 == 1` is good enough to pass the problem, it misses an important lesson that division is an expensive opertaion. Instead, when working with division/multiplication by 2, try to use `>>` - bitwise shift to the right - when dividing by power of 2, `<<` - bitwise shift to the left - when multiplying by power of 2, `&` bitwise AND operation for `%` operator and powers of 2. As these operations directly designed for bit level, CPU have mastered these operations and behave faster with them.\n\nSo, the behavior of `i & 1` is the same as `i % 2` (exercise for the reader - try to take a piece of paper and check why). In addition, `i >> 1` is same as `i / 2` (for `int`). Let\'s now write a solution using discussed techniques.\n## Solution\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n\t\t // reserve memory for n + 1 calculation\n vector<int> dp(n + 1, 0);\n // check edge case\n if(n == 0)\n return 0;\n // predefine dp[1] and max value\n dp[1] = 1;\n int max_value = 1;\n\t\t // iterate and calculate according to a given formula\n for(int i = 2; i <= n; ++i){\n if(i & 1)\n dp[i] = dp[i >> 1] + dp[(i >> 1) + 1];\n else \n dp[i] = dp[i >> 1];\n max_value = max(max_value, dp[i]);\n }\n return max_value;\n }\n};\n```\n\nUpvote for more solutions, coding comrades! | 4 | 0 | ['Dynamic Programming', 'Bit Manipulation', 'C++'] | 0 |
get-maximum-in-generated-array | 💯✅Dynamic Programming Easy to Understand short code | dynamic-programming-easy-to-understand-s-lwm9 | I think this code is self explainatory.But if have any query then comment down below..\n\nclass Solution {\n public int getMaximumGenerated(int n) {\n | deity0503 | NORMAL | 2022-10-03T16:54:45.599586+00:00 | 2022-10-03T16:54:45.599633+00:00 | 3,078 | false | I think this code is self explainatory.But if have any query then comment down below..\n```\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0)return 0;\n if(n==1)return 1;\n int []arr=new int[n+1];\n arr[0]=0;\n arr[1]=1;\n int maxi=Integer.MIN_VALUE;\n for(int i=2;i<=n;i++){\n if(i%2==0)arr[i]=arr[i/2];\n else arr[i]=arr[i/2]+arr[(i/2)+1];\n maxi=Math.max(maxi,arr[i]);\n }\n return maxi;\n }\n}\n``` | 4 | 0 | ['Dynamic Programming', 'Java'] | 2 |
get-maximum-in-generated-array | C++ || DP || 100% FASTER || SIMPLE SHORT SOLUTION | c-dp-100-faster-simple-short-solution-by-2ccu | \nint getMaximumGenerated(int n) {\n int dp[n+1];\n if(n==0){return 0;}\n if(n==1){return 1;}\n dp[0]=0;\n dp[1]=1;\n | sxkshi | NORMAL | 2022-09-20T06:49:15.748174+00:00 | 2022-09-20T06:49:15.748216+00:00 | 691 | false | ```\nint getMaximumGenerated(int n) {\n int dp[n+1];\n if(n==0){return 0;}\n if(n==1){return 1;}\n dp[0]=0;\n dp[1]=1;\n int maxi=dp[1];\n \n for(int i=2;i<n+1;i++)\n {\n if(i%2==0)\n {\n dp[i]=dp[i/2];\n }\n else\n {\n dp[i]=dp[i/2]+dp[(i/2)+1];\n }\n \n if(maxi<dp[i])\n {\n maxi=dp[i];\n }\n \n }\n \n return maxi;\n }\n```\n\n***UPVOTE IF HELPFUL! :))*** | 4 | 0 | ['Dynamic Programming', 'C', 'C++'] | 0 |
get-maximum-in-generated-array | [C++] Easy to understand one pass solution (100% time and 100% space) | c-easy-to-understand-one-pass-solution-1-alfr | This solution creates an array of the required size and updates the elements in the array while iterating up to n. Each element is calculated conditionally by c | bhaviksheth | NORMAL | 2021-01-15T21:58:09.836243+00:00 | 2021-01-16T23:57:12.492512+00:00 | 269 | false | This solution creates an array of the required size and updates the elements in the array while iterating up to n. Each element is calculated conditionally by checking if `i` is odd or even.\n\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if (n < 2) return n;\n \n int nums[n + 1];\n nums[0] = 0;\n nums[1] = 1;\n \n int result = 0;\n \n for (int i = 2; i <= n; ++i) {\n nums[i] = i % 2 ? nums[i / 2] + nums[i / 2 + 1] : nums[i / 2];\n result = max(result, nums[i]);\n }\n \n return result;\n }\n};\n``` | 4 | 2 | [] | 1 |
get-maximum-in-generated-array | ✔️ 🔥 Simple C++ Solution 🔥 || Easy to understand || Must Check 🔥 | simple-c-solution-easy-to-understand-mus-b91k | Solution:\n\n---\n\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n\n if(n<2)\n return n;\n\n int arr[n+1];\n\n | codingstar2001 | NORMAL | 2023-06-08T06:26:57.600396+00:00 | 2023-06-08T06:26:57.600440+00:00 | 855 | false | **Solution:**\n\n---\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n\n if(n<2)\n return n;\n\n int arr[n+1];\n\n int maxi = INT_MIN;\n\n arr[0] = 0;\n arr[1] = 1;\n\n for(int i = 2;i<=n;i++){\n if(i%2==1)\n arr[i] = arr[i/2] + arr[i/2 + 1];\n else\n arr[i] = arr[i/2];\n\n // Find max element \n\n maxi = max(maxi,arr[i]); \n }\n return maxi;\n\n return maxi;\n }\n};\n\n\n```\n----\n\n**Analysis:**\n\n Time Complexity : O(n)\n Space Coplexity : O(n)\n \n---\n\nIf this solution helps you,then please ```UPVOTE.```\n\n\nTill then **keep Learning, Keep Growing !!!!**\n\n**Thank You !!!**\n\n\n\n----\n\n | 3 | 0 | ['Dynamic Programming', 'C++'] | 0 |
get-maximum-in-generated-array | Recursion -> Memoization -> Tabulation | recursion-memoization-tabulation-by-tana-c4wy | Recursive\n\n int fun(int n) {\n if(n==0 || n==1) return n;\n \n if(n%2==1) {\n int a = fun((n-1)/2);\n int b = fu | TanayD304 | NORMAL | 2022-11-03T18:49:05.639679+00:00 | 2022-11-03T18:49:05.639714+00:00 | 450 | false | **Recursive**\n```\n int fun(int n) {\n if(n==0 || n==1) return n;\n \n if(n%2==1) {\n int a = fun((n-1)/2);\n int b = fun(((n-1)/2)+1);\n return a+b;\n } else return fun(n/2);\n }\n int getMaximumGenerated(int n) {\n if(n==0 || n==1) return n;\n vector<int> arr(n+1, 0);\n arr[0] = 0;\n arr[1] = 1;\n \n for(int i=2;i<=n;i++) arr[i] = fun(i);\n \n int ans = INT_MIN;\n for(int i=0;i<n+1;i++) ans = max(ans, arr[i]);\n return ans;\n }\n```\n**Memoization**\n```\nint fun(int n, vector<int> &dp) {\n if(n==0 || n==1) return n;\n \n if(dp[n]!=-1) return dp[n];\n if(n%2==1) {\n int a = fun((n-1)/2,dp);\n int b = fun(((n-1)/2)+1,dp);\n return a+b;\n } else return fun(n/2,dp);\n }\n int getMaximumGenerated(int n) {\n if(n==0 || n==1) return n;\n vector<int> dp(n+1, -1);\n dp[0] = 0;\n dp[1] = 1;\n \n for(int i=2;i<=n;i++) dp[i] = fun(i,dp);\n \n int ans = INT_MIN;\n for(int i=0;i<n+1;i++) ans = max(ans, dp[i]);\n return ans;\n }\n```\n**Tabulation**\n```\n int getMaximumGenerated(int n) {\n if(n==0 || n==1) return n;\n vector<int> dp(n+1, 0);\n dp[1] = 1;\n \n for(int i=2;i<=n;i++) {\n if(i%2==1) {\n int a = dp[(i-1)/2];\n int b = dp[((i-1)/2)+1];\n dp[i] = a+b;\n } else dp[i] = dp[i/2];\n }\n int ans = INT_MIN;\n for(int i=0;i<n+1;i++) ans = max(ans, dp[i]);\n return ans;\n }\n``` | 3 | 0 | [] | 0 |
get-maximum-in-generated-array | Java Easiest Solution || 1Ms Runtime Faster than 90% Online Submission || Beginner Friendly Approach | java-easiest-solution-1ms-runtime-faster-23rn | \n//Approch :- 1 Primitive O(n + n) 2Ms Runtime\nclass Solution {\n public int getMaximumGenerated(int n) {\n int[] arr = new int[n+1];\n if(n | Hemang_Jiwnani | NORMAL | 2022-10-14T15:51:49.121239+00:00 | 2022-10-14T15:51:49.121274+00:00 | 1,011 | false | ```\n//Approch :- 1 Primitive O(n + n) 2Ms Runtime\nclass Solution {\n public int getMaximumGenerated(int n) {\n int[] arr = new int[n+1];\n if(n == 0){\n return 0;\n }\n arr[0] = 0;\n arr[1] = 1;\n for(int i=1;i<n;i++){\n if((2*i) < n+1){\n arr[i*2] = arr[i];\n }\n else{break;}\n if(((2*i)+1) < n+1) {\n arr[(2*i)+1] = (arr[i] + arr[i+1]);\n }\n else{break;}\n }\n Arrays.sort(arr);\n return arr[arr.length-1];\n }\n}\n\n//Approch :-2 Advanced Approch O(n) 1Ms Runtime\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n < 2) return n;\n int[] arr = new int[n+1];\n arr[1] = 1;\n int max = 0;\n for(int i =2;i<=n;i++){\n if(i%2 == 0) arr[i] = arr[i/2];\n else arr[i] = arr[i/2] + arr[i/2 + 1];\n max = Math.max(max,arr[i]);\n }\n return max;\n }\n}\n``` | 3 | 0 | ['Java'] | 1 |
get-maximum-in-generated-array | C++|| SIMPLEST SOLUTION || BEGINNER FRIENDLY || FULLY EXPLAINED | c-simplest-solution-beginner-friendly-fu-0h9y | first of all we eliminate the edge cases i.e n == 0;\n2. then we declare a vector, you can use a array, it\'s the same use here;\n3. we fill in the values of 0, | prasoonrajpoot | NORMAL | 2022-07-29T04:31:39.088273+00:00 | 2022-07-29T04:31:39.088313+00:00 | 346 | false | 1. first of all we eliminate the edge cases i.e n == 0;\n2. then we declare a vector, you can use a array, it\'s the same use here;\n3. we fill in the values of 0,1 indexes as given in the question.\n4. we Put in the exact conditions given in the question.\n5. we return the max_element (an stl function) to get the answer;\n\n``` \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n == 0){\n return 0;\n }\n vector<int> answer(n+1);\n answer[0] = 0;\n answer[1] = 1;\n for(int i = 1; 2*i < n; i++){\n answer[2*i] = answer[i];\n answer[2*i + 1] = answer[i] + answer[i+1];\n }\n return *max_element(answer.begin(), answer.end());\n \n }\n};\n```\n\nComment if you got any questions | 3 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | C++ solution | 0s | faster than 100% | 9 lines code | c-solution-0s-faster-than-100-9-lines-co-t9k3 | \nint getMaximumGenerated(int n) {\n if(n==0) return 0;\n if(n==1) return 1;\n int nums[n+1]; nums[0] =0 , nums[1] = 1;\n for( | nova_007 | NORMAL | 2022-07-03T19:09:39.652841+00:00 | 2022-07-03T19:12:35.089084+00:00 | 130 | false | ```\nint getMaximumGenerated(int n) {\n if(n==0) return 0;\n if(n==1) return 1;\n int nums[n+1]; nums[0] =0 , nums[1] = 1;\n for(int i=1; i<(n+1)/2; i++)\n nums[2*i] = nums[i], nums[2*i + 1] = nums[i] + nums[i+1];\n if((n+1)%2) nums[n] = nums[(n)/2];\n int Max = nums[0];\n for(auto i: nums) Max = max(i, Max);\n return Max;\n }\n``` | 3 | 0 | ['Array'] | 0 |
get-maximum-in-generated-array | JAVA 100% FASTER Solution | java-100-faster-solution-by-yash_sawant7-2e6v | \n\n\n\n\n\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n<=1)return n;\n int[] arr=new int[n+1];\n arr[0]=0;\n | yash_sawant724 | NORMAL | 2022-05-06T12:26:05.170101+00:00 | 2022-05-06T12:27:12.703816+00:00 | 485 | false | \n\n\n\n\n```\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n<=1)return n;\n int[] arr=new int[n+1];\n arr[0]=0;\n arr[1]=1;\n \n for(int i=2;i<=n;i++){\n arr[i]= i%2==0?arr[i/2]:arr[i/2]+arr[i-(i/2)];\n }\n \n int max=Integer.MIN_VALUE;\n for(int i:arr){\n max=Math.max(i,max);\n }\n return max;\n }\n}\n``` | 3 | 0 | ['Dynamic Programming', 'Java'] | 0 |
get-maximum-in-generated-array | Go just do it | go-just-do-it-by-dchooyc-8ryx | \nfunc getMaximumGenerated(n int) int {\n if n < 2 {\n return n\n }\n \n a, max := make([]int, n + 1), 0\n \n a[0] = 0\n a[1] = 1\n | dchooyc | NORMAL | 2022-04-19T15:04:05.293028+00:00 | 2022-04-19T15:04:05.293068+00:00 | 190 | false | ```\nfunc getMaximumGenerated(n int) int {\n if n < 2 {\n return n\n }\n \n a, max := make([]int, n + 1), 0\n \n a[0] = 0\n a[1] = 1\n \n for i := 2; i <= n; i++ {\n num := a[i / 2]\n \n if i % 2 == 1 {\n num += a[(i / 2) + 1]\n }\n \n a[i] = num\n \n if num > max {\n max = num\n }\n }\n \n return max\n}\n``` | 3 | 0 | ['Go'] | 0 |
get-maximum-in-generated-array | Simple C++ Solution | simple-c-solution-by-karthi_701-6qwf | class Solution {\npublic:\n int getMaximumGenerated(int n) {\n vectork(n+1000,0);k[0]=0;k[1]=1;int i=2;\n while(i<=n){\n if(i%2==0) | karthi_701 | NORMAL | 2021-09-25T10:20:20.040088+00:00 | 2021-09-26T08:58:46.972226+00:00 | 189 | false | class Solution {\npublic:\n int getMaximumGenerated(int n) {\n vector<int>k(n+1000,0);k[0]=0;k[1]=1;int i=2;\n while(i<=n){\n if(i%2==0) {\n k[i]=k[i/2];\n }else{\n \xA0 \xA0 \xA0 \xA0 \xA0 \xA0 \xA0 k[i]=k[i/2]+k[i/2+1];\n }i++;\n }\n \xA0 if(n==0) return 0;\n return *max_element(k.begin(),k.end());\n }\n}; | 3 | 0 | [] | 1 |
get-maximum-in-generated-array | Simple Python 3 Solution, Easy to understand. | simple-python-3-solution-easy-to-underst-x8pd | \n\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n nums = [0,1]\n j = 1\n for i in range(2,n//2+2):\n nums | sethhritik | NORMAL | 2021-01-22T17:24:31.912986+00:00 | 2021-01-22T17:24:31.913031+00:00 | 133 | false | \n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n nums = [0,1]\n j = 1\n for i in range(2,n//2+2):\n nums.append(nums[j])\n nums.append(nums[j]+nums[j+1])\n j+=1\n return max(nums[:n+1])\n \n``` | 3 | 0 | [] | 0 |
get-maximum-in-generated-array | 𝑒𝓁𝑒𝑔𝒶𝓃𝓉 O(1) space ^^ | elegant-o1-space-by-andrii_khlevniuk-3zh2 | \nint getMaximumGenerated(int n) \n{\n\tauto out{0};\n\tfor(auto i{1}, a{0}, b{1}; i<=n; out = max(out, a = exchange(b, (2*__builtin_ctz(i++)+1)*b-a)));\n\tretu | andrii_khlevniuk | NORMAL | 2020-11-10T12:43:13.678418+00:00 | 2020-11-15T14:34:47.579398+00:00 | 312 | false | ```\nint getMaximumGenerated(int n) \n{\n\tauto out{0};\n\tfor(auto i{1}, a{0}, b{1}; i<=n; out = max(out, a = exchange(b, (2*__builtin_ctz(i++)+1)*b-a)));\n\treturn out; \n}\n```\n\n**Brief explanation**\n\nGo over all `n+1` numbers keeping track of the maximum. It turns out you don\'t need to store the full array. You need only two previous numbers to generate the next one (like in Fibonacci numbers algo). Here I use a builtin `gcc` function `__builtin_ctz` to compute the number of trailing zeroes of a number. So the solution is basically: \n```\nint getMaximumGenerated(int n) \n{\n\tauto out{0};\n\tfor(auto i{1}, a{0}, b{1}; i<=n; ++i)\n {\n auto t = (2 * __builtin_ctz(i) + 1) * b - a;\n a = b;\n b = t;\n out = max(out, a);\n }\n\treturn out; \n}\n```\nThe problem has strong "divide in half" vibe so it\'s not surprising the solution uses binary representation of the numbers.\nOfc you can write custom function for counting **n**umber of **t**railing **z**eroes `ntz`. Here are few options from the book *Hacker\'s delight* by Henry S. Warren:\n\n**Simple**\n\n```\nint ntz(unsigned int x)\n{\n int n{0};\n \n x = ~x & (x-1);\n for(; x!=0; n++, x>>=1);\n return n;\n}\n```\n\n**Reiser\'s algorithm**\n\n```\nint ntz(unsigned x)\n{\n static char table[37] = {32, 0, 1,26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17,\n 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18};\n x = (x&-x)%37;\n return table[x];\n}\n```\n\n**de Bruijn cycle (Danny Dub\xE9)**\n\n```\nint ntz(unsigned int x)\n{\n static char table[32] = {0, 1, 2, 24, 3, 19, 6, 25, 22, 4, 20, 10, 16, 7, 12, 26, 31, \n 23, 18, 5, 21, 9, 15, 11, 30, 17, 8, 14, 29, 13, 28, 27};\n if(x==0) return 32;\n x = (x&-x)*0x04D7651F;\n return table[x>>27];\n}\n```\n\n**Dean Gaudet\'s algorithm**\n\n```\nint ntz(unsigned int x)\n{\n unsigned y, bz, b4, b3, b2, b1, b0;\n \n y = x & -x;\n bz = y ? 0 : 1;\n b4 = y & 0x0000FFFF ? 0 : 16;\n b3 = y & 0x00FF00FF ? 0 : 8;\n b2 = y & 0x0F0F0F0F ? 0 : 4;\n b1 = y & 0x33333333 ? 0 : 2;\n b0 = y & 0x55555555 ? 0 : 1;\n return bz + b4 + b3 + b2 + b1 + b0;\n}\n```\n\n**Seal\'s algorithm**\n\n```\nint ntz(unsigned int x)\n{\n static char table[64] = {32, 0, 1, 12, 2, 6, 0 , 13, 3, 0, 7, 0, 0, 0, 0, 14,\n 10, 4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27, 15,\n 31, 11, 5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0, 20, 26,\n 30, 0, 0, 0, 0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0};\n x = (x&-x)*0x0450FBAF;\n return table[x>>26];\n}\n```\n \n | 3 | 0 | ['C', 'C++'] | 0 |
get-maximum-in-generated-array | [Kotlin] Check Even-Odd | kotlin-check-even-odd-by-parassidhu-i6o4 | \n fun getMaximumGenerated(n: Int): Int {\n if (n == 0) return 0\n val res = IntArray(n + 1)\n var max = 1\n res[0] = 0 // Rule 1\n | parassidhu | NORMAL | 2020-11-08T04:15:09.831820+00:00 | 2020-11-08T04:15:09.831863+00:00 | 359 | false | ```\n fun getMaximumGenerated(n: Int): Int {\n if (n == 0) return 0\n val res = IntArray(n + 1)\n var max = 1\n res[0] = 0 // Rule 1\n res[1] = 1 // Rule 2\n \n for (i in 2 until res.size) {\n if (i % 2 == 0)\n res[i] = res[i/2] // Rule 3\n else \n res[i] = res[i/2] + res[i/2 + 1] // Rule 4\n \n max = maxOf(max, res[i])\n }\n \n return max\n }\n``` | 3 | 0 | ['Kotlin'] | 0 |
get-maximum-in-generated-array | Java | Pre Compute Array as told | java-pre-compute-array-as-told-by-abidee-7bzr | \nclass Solution {\n public int getMaximumGenerated(int n) {\n \n if(n == 0) return 0;\n if(n==1) return 1;\n \n int[] arr | abideenzainuel | NORMAL | 2020-11-08T04:07:58.865358+00:00 | 2020-11-08T04:08:34.622345+00:00 | 415 | false | ```\nclass Solution {\n public int getMaximumGenerated(int n) {\n \n if(n == 0) return 0;\n if(n==1) return 1;\n \n int[] arr = new int[n+1];\n arr[0] = 0;\n arr[1] = 1;\n \n int max = 1;\n \n for(int i=2; i<arr.length; i++){\n if(i%2==0){\n arr[i] = arr[i/2];\n }else{\n int diff = i/2;\n arr[i] = arr[diff] + arr[i-diff];\n }\n max = Math.max(max,arr[i]);\n }\n \n return max;\n }\n}\n``` | 3 | 2 | ['Java'] | 0 |
get-maximum-in-generated-array | Here's the simple Java solution. | heres-the-simple-java-solution-by-jagank-4p7g | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | jagankumaroffl | NORMAL | 2025-01-09T04:38:29.983818+00:00 | 2025-01-09T04:38:29.983818+00:00 | 203 | 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
```java []
class Solution {
public int getMaximumGenerated(int n) {
int[] a = new int[n+1];
if(n<1){
return 0;
}
a[0] = 0;
a[1] = 1;
int t = 1;
for(int i=2;i<n+1;i++){
if(i%2==0){
a[t*2] = a[t];
}
else if(i%2!=0){
a[(t*2)+1] = a[t] + a[t+1];
t++;
}
}
Arrays.sort(a);
return a[n];
}
}
``` | 2 | 0 | ['Java'] | 0 |
get-maximum-in-generated-array | Simple C solution | simple-c-solution-by-aswath_1192-5xsd | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Aswath_1192 | NORMAL | 2025-01-07T15:00:04.645827+00:00 | 2025-01-07T15:00:04.645827+00:00 | 50 | 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
```c []
int getMaximumGenerated(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int nums[n + 1];
nums[0] = 0;
nums[1] = 1;
int max = 1;
for (int i = 2; i <= n; i++) {
if (i % 2 == 0) {
nums[i] = nums[i / 2];
} else {
nums[i] = nums[i / 2] + nums[i / 2 + 1]; }
if (nums[i] > max) {
max = nums[i]; }
}
return max;
}
``` | 2 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | Stern's Diatomic Sequence | sterns-diatomic-sequence-by-iyerke-7pck | Intuition\n Describe your first thoughts on how to solve this problem. \nThis is Stern\'s Diatomic Sequence.\n\n# Approach\n Describe your approach to solving t | iyerke | NORMAL | 2024-04-19T20:50:10.546605+00:00 | 2024-09-01T04:17:01.671905+00:00 | 96 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is Stern\'s Diatomic Sequence.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe apply proposition 4.3 in this paper:\nhttps://www.jstor.org/stable/10.4169/000298910x496714.\n\nNamely, $$a_{n+1}=a_n + a_{n-1} - 2(a_{n-1} \\text{ mod } a_n)$$.\n\n# Complexity\n- Time complexity: $O(n)$.\n\n- Space complexity: $O(1)$.\n\n# Code\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if not n: return n\n n_2, n_1 = 0, 1\n res = 1\n for _ in range(n - 1):\n n_2, n_1 = n_1, n_1 + n_2 - 2 * (n_2 % n_1)\n res = max(res, n_1)\n return res\n``` | 2 | 0 | ['Python3'] | 2 |
get-maximum-in-generated-array | efficient JS | efficient-js-by-javad_pk-k05z | 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 | javad_pk | NORMAL | 2024-02-18T09:38:08.923722+00:00 | 2024-02-18T09:38:08.923755+00:00 | 77 | 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)$$ -->\n\n# Code\n```\n/**\n * @param {number} n\n * @return {number}\n */\nvar getMaximumGenerated = function(n) {\n let arr = (n === 0) ? [0] : [0, 1];\n let i=1\n while(arr.length<=n-1){\n arr.push(arr[i])\n arr.push(arr[i]+arr[i+1])\n i++\n }\n return Math.max(...arr)\n};\n``` | 2 | 0 | ['JavaScript'] | 0 |
get-maximum-in-generated-array | Simple Java Solution | Detail Explanations | O(n) | DP | Please UPVOTE | Vrund Patel | simple-java-solution-detail-explanations-ai90 | Intuition\nThe problem asks for finding the maximum value in an array generated according to a specific pattern. To solve this, we need to simulate the generati | vrund297 | NORMAL | 2023-09-16T08:45:18.227176+00:00 | 2023-09-16T08:45:18.227205+00:00 | 337 | false | # Intuition\nThe problem asks for finding the maximum value in an array generated according to a specific pattern. To solve this, we need to simulate the generation process and keep track of the maximum value encountered during the process.\n\n# Approach\nThe code takes the following approach:\n1. If `n` is 0, return 0, and if `n` is 1, return 1, as these are the base cases.\n2. Initialize an array `dp` of size `n+1` to store the generated values.\n3. Set `dp[0]` to 0 and `dp[1]` to 1, as these are the initial values specified in the problem.\n4. Initialize a variable `max` to keep track of the maximum value encountered in the generated array.\n5. Iterate from `i = 2` to `n`:\n - If `i` is even, set `dp[i]` equal to `dp[i/2]`, as specified in the pattern.\n - If `i` is odd, set `dp[i]` equal to `dp[i/2] + dp[i/2 + 1]`, as specified in the pattern.\n - Update `max` with the maximum of the current `dp[i]` value and the current `max` value.\n6. Return the final value of `max`, which represents the maximum value in the generated array.\n\n# Complexity\n- Time complexity: O(n) because the code iterates through the array `dp` once, updating each element in constant time.\n- Space complexity: O(n) because the code uses an array of size `n+1` to store the generated values, which is directly proportional to the input size `n`.\n\nCode by : Vrund R Patel | Software Developer\n\n# Code\n```\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0) return 0;\n if(n==1) return 1;\n int[] dp = new int[n+1];\n dp[0] = 0;\n dp[1] = 1;\n int max = 0;\n for(int i=2; i<=n; i++){\n if(i%2 ==0){\n dp[i] = dp[i/2];\n }\n else{\n dp[i] = dp[i/2] + dp[i/2 +1];\n }\n max = Math.max(dp[i], max);\n }\n return max;\n \n }\n}\n``` | 2 | 0 | ['Dynamic Programming', 'Java'] | 0 |
get-maximum-in-generated-array | Simple DP solution C++ | simple-dp-solution-c-by-sunnyyad2002-0ec2 | 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 | sunnyyad2002 | NORMAL | 2023-03-31T17:31:31.101448+00:00 | 2023-03-31T17:31:31.101487+00:00 | 1,890 | 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)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if (n < 2) return n;\n vector<int> dp(n+1);\n int m = INT_MIN;\n dp[0] = 0, dp[1] = 1;\n for (int i = 2; i <= n; i++) {\n if (i % 2) dp[i] = dp[i / 2] + dp[i / 2 + 1];\n else dp[i] = dp[i / 2];\n m = max(dp[i], m);\n }\n return m;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
get-maximum-in-generated-array | ✅✅✅ very easy solution 0(n) | very-easy-solution-0n-by-najmiddin1-t0wo | \nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n<2: return n\n arr, m = [0, 1], 0\n for i in range(2, n+1):\n | Najmiddin1 | NORMAL | 2022-12-30T04:59:11.630389+00:00 | 2022-12-30T04:59:11.630436+00:00 | 1,101 | false | ```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n<2: return n\n arr, m = [0, 1], 0\n for i in range(2, n+1):\n arr += [arr[i//2]+arr[i//2+1] if i%2 else arr[i//2]]\n if arr[i]>m: m =arr[i]\n return m\n``` | 2 | 0 | ['Python', 'Python3'] | 0 |
get-maximum-in-generated-array | Java | java-by-niyazjava-vnid | \n\n public int getMaximumGenerated(int n) {\n if (n == 1) return 1;\n if (n == 0) return 0;\n \n int[] arr = new int[n+1];\n | NiyazJava | NORMAL | 2022-11-27T07:27:54.115176+00:00 | 2022-11-27T07:27:54.115222+00:00 | 417 | false | ```\n\n public int getMaximumGenerated(int n) {\n if (n == 1) return 1;\n if (n == 0) return 0;\n \n int[] arr = new int[n+1];\n arr[0] = 0;\n arr[1] = 1;\n int max = 0;\n\n for (int i = 2; i <= n; i++) {\n if (i % 2 == 0) {\n arr[i] = arr[i / 2];\n } else {\n arr[i] = arr[i / 2] + arr[i / 2 + 1];\n }\n max = Math.max(max, arr[i]);\n }\n return max;\n }\n\n``` | 2 | 0 | ['Java'] | 0 |
get-maximum-in-generated-array | JS faster than 100% (easy solution and optimize run time) | js-faster-than-100-easy-solution-and-opt-8drh | \n\n# Code\n# Solution 1: Recursion\n\n/**\n * @param {number} n\n * @return {number}\n */\n const getValue = (n) => {\n if (n === 0) return 0;\n if (n = | kunkka1996 | NORMAL | 2022-11-25T04:39:57.442591+00:00 | 2022-11-25T04:51:37.665651+00:00 | 1,030 | false | \n\n# Code\n# Solution 1: Recursion\n```\n/**\n * @param {number} n\n * @return {number}\n */\n const getValue = (n) => {\n if (n === 0) return 0;\n if (n === 1) return 1;\n\n if (n % 2) {\n const newNumber = (n - 1)/2;\n return getValue(newNumber) + getValue(newNumber + 1);\n } else {\n return getValue(n/2);\n }\n }\nvar getMaximumGenerated = function(n) {\n let output = 0;\n\n for (let i = 1; i <= n; i++) {\n output = Math.max(output, getValue(i))\n }\n\n return output;\n};\n```\n# Solution 2: Memoization (optimize)\n```\n/**\n * @param {number} n\n * @return {number}\n */\n const temp = [0, 1];\n const getValue = (n) => {\n if (n === 0) return 0;\n if (n === 1) return 1;\n\n if (n % 2) {\n const newNumber = (n - 1)/2;\n return temp[newNumber] + temp[newNumber + 1];\n } else {\n return temp[n/2];\n }\n }\nvar getMaximumGenerated = function(n) {\n let output = 0;\n\n for (let i = 1; i <= n; i++) {\n temp[i] = getValue(i);\n output = Math.max(output, temp[i]);\n }\n\n return output;\n};\n``` | 2 | 0 | ['Memoization', 'JavaScript'] | 0 |
get-maximum-in-generated-array | c++ | easy | fast | c-easy-fast-by-venomhighs7-t2q4 | \n\n# Code\n\n//from sne4kers\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n vector<int> dp(n + 1, 0);\n if(n == 0)\n | venomhighs7 | NORMAL | 2022-11-22T12:11:57.212366+00:00 | 2022-11-22T12:11:57.212395+00:00 | 972 | false | \n\n# Code\n```\n//from sne4kers\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n vector<int> dp(n + 1, 0);\n if(n == 0)\n return 0;\n dp[1] = 1;\n int maxi = 1;\n for(int i = 2; i <= n; ++i){\n if(i & 1)\n dp[i] = dp[i >> 1] + dp[(i >> 1) + 1];\n else \n dp[i] = dp[i >> 1];\n maxi = max(maxi, dp[i]);\n }\n return maxi;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
get-maximum-in-generated-array | Java Solution using DP | java-solution-using-dp-by-astiksarathe-nvsr | \nclass Solution {\n public int getMaximumGenerated(int n) {\n int[] arr = new int[n + 1];\n int m = 0;\n \n if(n<=1) return n;\n | astiksarathe | NORMAL | 2022-09-18T14:53:15.238771+00:00 | 2022-09-18T14:53:15.238811+00:00 | 611 | false | ```\nclass Solution {\n public int getMaximumGenerated(int n) {\n int[] arr = new int[n + 1];\n int m = 0;\n \n if(n<=1) return n;\n if(n == 2) return 1;\n \n arr[0] = 0; arr[1] = 1;\n for(int i = 2; i <= n; i++){\n \n if(i %2 == 0){\n arr[i] = arr[i / 2];\n }else{\n arr[i] = arr[i / 2] + arr[i / 2 + 1];\n }\n \n m = Math.max(m,arr[i]);\n \n }\n \n return m;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
get-maximum-in-generated-array | Java | 1ms | java-1ms-by-aashishjn-bvw9 | \nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0)\n return 0;\n if(n==1)\n return 1;\n int d | aashishjn | NORMAL | 2022-08-06T13:26:45.720291+00:00 | 2022-08-06T13:26:45.720329+00:00 | 115 | false | ```\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0)\n return 0;\n if(n==1)\n return 1;\n int dp[] = new int[n+1];\n int max=1;\n dp[0]=0;\n dp[1]=1;\n for(int i=2;i<=n;i++)\n {\n if(i%2 == 0)\n dp[i]=dp[i/2];\n else\n dp[i]=dp[i/2]+dp[i/2+1];\n max = Math.max(dp[i],max);\n }\n return max;\n }\n}\n``` | 2 | 0 | ['Dynamic Programming', 'Java'] | 0 |
get-maximum-in-generated-array | C++ Easy Well-Explained Solution (Dynamic Programming) | c-easy-well-explained-solution-dynamic-p-5l35 | We have to traverse from i=0 to i=n. Initialize vector m of size (n+1).\nm[0]=0, m[1]=1\nFor every iteration, \n\t1.Even i (i%2==0)\n\t\t m[i]= m[i/2];\n\t2.Odd | asmita010 | NORMAL | 2022-01-09T11:45:21.530726+00:00 | 2022-04-16T05:18:52.753892+00:00 | 177 | false | We have to traverse from i=0 to i=n. Initialize vector m of size (n+1).\nm[0]=0, m[1]=1\nFor every iteration, \n\t1.Even i (i%2==0)\n\t\t m[i]= m[i/2];\n\t2.Odd i\n\t\tex-9\n\t\tm[9]= m[(9-1)/ 2]+ m[((9-1)/ 2) +1] =m[8/2]+ m[8/2+1] =m[4]+m[5]\n\t3.Calculating Max value\nReturn Maximum value\n**Upvote** if the solution helps.\n\n```class Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n==0)\n return 0;\n vector< int > m(n+1);\n m[0]=0;\n m[1]=1;\n int max=1;\n for(int i=2; i<=n; i++) {\n if(i%2==0)\n m[i]= m[i/2];\n else\n m[i]=m[(i-1)/2]+m[(i-1)/2+1];\n if(m[i]>max)\n max=m[i];\n }\n return max;\n }\n}; | 2 | 1 | ['Array', 'Dynamic Programming', 'C'] | 1 |
get-maximum-in-generated-array | C++ || 0ms runtime || 100% faster | c-0ms-runtime-100-faster-by-yaswankar199-hv6j | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n==0) return 0;\n if(n==1) return 1;\n int max=0,d,q,i;\n vec | yaswankar1998 | NORMAL | 2022-01-04T11:20:48.581329+00:00 | 2022-01-04T11:20:48.581362+00:00 | 120 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n==0) return 0;\n if(n==1) return 1;\n int max=0,d,q,i;\n vector<int> nums(n+1,0);\n nums[0]=0;\n nums[1]=1;\n for(i=2; i<=n; i++) {\n d=i%2,q=i/2;\n if(d > 0) nums[i]= nums[q]+nums[q+d];\n else nums[i]=nums[q];\n if(nums[i]>max) max=nums[i];\n }\n return max;\n }\n};\n```\n**If you like the solution, pls upvote**\n**For queries, pls comment** | 2 | 0 | ['Dynamic Programming'] | 0 |
get-maximum-in-generated-array | c++/dp/100% fast and 99% fast used less space | cdp100-fast-and-99-fast-used-less-space-5o3x4 | \n if(n==0) return 0;\n int nums[n+1];\n nums[0]=0;\n nums[1]=1;\n for(int i=2;i<=n;i++){\n if(i%2==0) nums[i]=nums[i/2];\ | ravirkumar2422 | NORMAL | 2021-11-13T14:03:13.976464+00:00 | 2021-11-13T14:03:13.976511+00:00 | 148 | false | ```\n if(n==0) return 0;\n int nums[n+1];\n nums[0]=0;\n nums[1]=1;\n for(int i=2;i<=n;i++){\n if(i%2==0) nums[i]=nums[i/2];\n else nums[i]=nums[i/2]+nums[i/2+1];\n }\n int ans=0;\n for(int i=0;i<=n;i++){\n ans=max(ans,nums[i]);\n }\n return ans;\n\t\t``` | 2 | 0 | ['Dynamic Programming'] | 1 |
get-maximum-in-generated-array | C++ | Dynamic Programing | 100% faster | Simple Maths | c-dynamic-programing-100-faster-simple-m-89tg | There is just a easy patter you can observe and can store element in array. The patter is the number for index 0, 1, 2 are fixed that is 0, 1, 1 respectively. F | avinash1320 | NORMAL | 2021-08-02T12:47:19.415428+00:00 | 2021-08-02T12:47:19.415512+00:00 | 225 | false | There is just a easy patter you can observe and can store element in array. The patter is the number for index `0, 1, 2` are fixed that is `0, 1, 1` respectively. From 3 onward the patter is if the `index `is `even` then `ans[i]= ans[i/2]` and if the `index` is `odd` then the `ans[i] = ans[i/2] + ans[(i/2)+1]`.\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n int ans[n+1];\n if(n==0)\n return 0;\n if(n<=2)\n return 1;\n ans[0]=0;\n ans[1]=ans[2]=1;\n int m=0;\n for(int i=3;i<=n;i++){\n if(i%2==0)\n ans[i]=ans[i/2];\n else\n ans[i]=ans[i/2]+ans[i/2+1];\n m=max(ans[i],m);\n }\n return m;\n }\n};\n```\nPlease **upvote** | 2 | 1 | ['Math', 'Dynamic Programming', 'C', 'C++'] | 0 |
get-maximum-in-generated-array | Java beats 100% | java-beats-100-by-sanket100-frar | \nclass Solution {\n public int getMaximumGenerated(int n) {\n int[] arr = new int[n+1];\n if(n<=1)return n;\n arr[0]=0;\n arr[1] | Sanket100 | NORMAL | 2021-07-23T19:35:22.562334+00:00 | 2021-07-23T19:35:22.562364+00:00 | 94 | false | ```\nclass Solution {\n public int getMaximumGenerated(int n) {\n int[] arr = new int[n+1];\n if(n<=1)return n;\n arr[0]=0;\n arr[1]=1;\n int temp = 0,max=0;\n for(int i=2;i<=n;i++){\n if(i%2==0)arr[i]=arr[i/2];\n else {\n temp = (i-1)/2;\n arr[i]=arr[temp] + arr[temp+1];\n }\n max=max>arr[i]?max:arr[i];\n }\n \n return max;\n }\n}\n``` | 2 | 0 | [] | 0 |
get-maximum-in-generated-array | easy python | O(n) solution | easy-python-on-solution-by-abhishen99-dv23 | \nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n==0:\n return 0\n t=[0]*(n+1)\n t[1] = 1 \n | Abhishen99 | NORMAL | 2021-06-27T12:31:48.069125+00:00 | 2021-06-27T12:31:48.069202+00:00 | 260 | false | ```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n==0:\n return 0\n t=[0]*(n+1)\n t[1] = 1 \n for i in range(2,n+1):\n \n if i%2==0:\n t[i] = t[i//2]\n else:\n t[i] = t[i//2] +t[i//2 + 1]\n return max(t)\n \n \n \n``` | 2 | 0 | ['Python', 'Python3'] | 0 |
get-maximum-in-generated-array | Java faster than 100% | java-faster-than-100-by-otabek94_30-zrw3 | \nclass Solution {\n public int getMaximumGenerated(int n) {\n int i, j, max = -1000;\n int nums[] = new int[n + 1];\n \n for(i = | otabek94_30 | NORMAL | 2021-05-25T10:58:46.313738+00:00 | 2021-05-25T10:59:27.324135+00:00 | 58 | false | ```\nclass Solution {\n public int getMaximumGenerated(int n) {\n int i, j, max = -1000;\n int nums[] = new int[n + 1];\n \n for(i = 0; i <= n; i++){\n if(i == 0){\n nums[i] = 0;\n }\n else if(i == 1){\n nums[i]=1;\n }\n else if(i % 2 == 0 && i > 0){//i=2\n nums[i] = nums[i/2];\n }\n else if(i % 2 != 0 && i > 0){//i=3\n nums[i] = nums[i/2] + nums[i/2 + 1]; \n \n }\n \n }\n for(i = 0; i <= n; i++){\n if(max < nums[i]){\n max = nums[i];\n } \n }\n return max;\n }\n}\n``` | 2 | 0 | [] | 0 |
get-maximum-in-generated-array | Java || Array || beats 100% || 0ms || T.C - O(N) S.C - O(N) | java-array-beats-100-0ms-tc-on-sc-on-by-yx5i0 | \n // O(n) O(n)\n\tpublic int getMaximumGenerated(int n) {\n\n if(n == 0)\n return 0;\n \n\t\tint[] ans = new int[n + 1];\n\t\tans[0 | LegendaryCoder | NORMAL | 2021-04-24T17:22:08.777182+00:00 | 2021-04-24T17:22:08.777226+00:00 | 79 | false | \n // O(n) O(n)\n\tpublic int getMaximumGenerated(int n) {\n\n if(n == 0)\n return 0;\n \n\t\tint[] ans = new int[n + 1];\n\t\tans[0] = 0;\n\t\tans[1] = 1;\n\t\tint max = 1;\n\n\t\tfor (int i = 2; i <= n; i++) {\n\t\t\tans[i] = (i % 2 == 0) ? ans[i / 2] : ans[i / 2] + ans[i / 2 + 1];\n\t\t\tif (ans[i] > max)\n\t\t\t\tmax = ans[i];\n\t\t}\n\n\t\treturn max;\n\t} | 2 | 0 | [] | 0 |
get-maximum-in-generated-array | Python3 simple solution best 90% users | python3-simple-solution-best-90-users-by-yupt | \nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n == 0:\n return 0\n elif n == 1:\n return 1\n | EklavyaJoshi | NORMAL | 2021-04-14T17:05:28.938069+00:00 | 2021-04-14T17:05:28.938110+00:00 | 136 | false | ```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n == 0:\n return 0\n elif n == 1:\n return 1\n else:\n nums = [0,1]\n for i in range(2,n+1):\n if i%2 == 0:\n nums.append(nums[i//2])\n else:\n nums.append(nums[i//2]+nums[(i//2)+1])\n return max(nums)\n```\n**If you like this solution, please upvote for this** | 2 | 0 | ['Python3'] | 0 |
get-maximum-in-generated-array | [c++] easy to understand | c-easy-to-understand-by-super_cool123-zti2 | **simple c++ solution \ntime complexity = O(n)\nspace complexity = O(n)\n\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n <= 1) | super_cool123 | NORMAL | 2021-01-16T06:27:16.239662+00:00 | 2021-01-16T06:27:16.239713+00:00 | 147 | false | **simple c++ solution \ntime complexity = O(n)\nspace complexity = O(n)\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n <= 1) // base condition \n return n; \n vector<int>v(n+1); // create a vector of size+1\n v[0] = 0; // given in the problem statement\n v[1] = 1; // given in the problem statement\n for(int i=2; i<=n; i++) {\n if(i%2) {\n v[i] = v[i/2] + v[i/2 + 1]; // if a number is odd than we should add middle and middle+1 element\n }\n else {\n v[i] = v[i/2]; // if a number is even the we should put the middle element\n }\n }\n return *max_element(v.begin(),v.end()); // return the maximum of all the element\n \n }\n};\n``` | 2 | 0 | ['C', 'C++'] | 0 |
get-maximum-in-generated-array | [Scala] ❤ Get Maximum in Generated Array | good style | recursive and iterative | scala-get-maximum-in-generated-array-goo-0buz | Solution 1: recursive solution\n\nscala\nobject Solution {\n def getMaximumGenerated(n: Int): Int = {\n (0 to n).map(valueAtPositionK).max\n }\n\n def va | victoria_p_liao | NORMAL | 2021-01-16T01:26:24.979883+00:00 | 2021-01-16T02:41:05.892111+00:00 | 116 | false | Solution 1: recursive solution\n\n```scala\nobject Solution {\n def getMaximumGenerated(n: Int): Int = {\n (0 to n).map(valueAtPositionK).max\n }\n\n def valueAtPositionK(positionK: Int): Int = {\n if (positionK == 0) 0 else if (positionK == 1) 1 else {\n val kModTwo: Int = positionK % 2\n val kDivTwo: Int = positionK / 2\n if (kModTwo == 0) {\n valueAtPositionK(kDivTwo)\n } else {\n valueAtPositionK(kDivTwo) + valueAtPositionK(kDivTwo + 1)\n }\n }\n }\n}\n\n```\n\nSolution 2: iterative solution\n\n```scala\nobject Solution {\n\n def getMaximumGenerated(n: Int): Int = {\n generateValues(0, n, Seq.empty[Int]).max\n }\n\n def generateValues(currentPosition: Int, maxPosition: Int, currentValues: Seq[Int]): Seq[Int] = {\n if (currentPosition > maxPosition) {\n currentValues\n } else {\n val valueAtPositionK: Int = {\n if (currentPosition == 0) 0 else if (currentPosition == 1) 1 else {\n val positionModTwo: Int = currentPosition % 2\n val positionDivTwo: Int = currentPosition / 2\n if (positionModTwo == 0) {\n currentValues(positionDivTwo)\n } else {\n currentValues(positionDivTwo) + currentValues(positionDivTwo + 1)\n }\n }\n }\n generateValues(currentPosition + 1, maxPosition, currentValues :+ valueAtPositionK)\n }\n }\n}\n```\n | 2 | 0 | [] | 1 |
get-maximum-in-generated-array | Easy java solution | easy-java-solution-by-abhishekjain581-y4dm | \nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n<1) return 0;\n int nums[] = new int[n+1];\n \n nums[0]=0;\n | abhishekjain581 | NORMAL | 2021-01-15T18:21:30.035894+00:00 | 2021-01-15T18:21:30.035922+00:00 | 54 | false | ```\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n<1) return 0;\n int nums[] = new int[n+1];\n \n nums[0]=0;\n nums[1]=1;\n int i=1;\n int aa=1;\n while(aa<=n)\n {\n if((2*i)>n) break;\n nums[2*i]=nums[i];\n if((2*i+1)>n) break;\n nums[2 * i + 1] = nums[i] + nums[i + 1];\n aa=2*i+1;\n i++;\n }\n Arrays.sort(nums);\n return nums[nums.length-1];\n }\n}\n``` | 2 | 0 | [] | 2 |
get-maximum-in-generated-array | [c++] 0ms solution | better explanation stepwise | c-0ms-solution-better-explanation-stepwi-51ro | \n int getMaximumGenerated(int n) {\n if(n == 0) {\n return 0;\n }\n vector<int> v;\n v.push_back(0); // v[0] first ele | Ved_Prakash1102 | NORMAL | 2021-01-15T13:22:12.664069+00:00 | 2021-01-15T13:22:12.664092+00:00 | 176 | false | ```\n int getMaximumGenerated(int n) {\n if(n == 0) {\n return 0;\n }\n vector<int> v;\n v.push_back(0); // v[0] first element\n v.push_back(1); // v[1] second element\n int MAX = 1; // since 1 is max of v[0] & v[1]\n for(int i = 2; i <= n; i++) { // iterate from 2 to n as total n+1 elements are to push \n if(i%2 == 0) { // If i is divisible by 2 then val = v[i/2] \n int val = v[i/2];\n v.push_back(val);\n MAX = max(v[i], MAX); // update the max value\n }\n else {\n int val = v[i/2]+v[(i/2)+1]; // if i is not divided by 2 then push v[i/2] + v[(i/2)+1]\n v.push_back(val);\n MAX = max(v[i], MAX); // update max value\n }\n }\n return MAX;\n }\n``` | 2 | 0 | ['C', 'C++'] | 0 |
get-maximum-in-generated-array | Simple java solution with explaination, 0ms, 100% faster | simple-java-solution-with-explaination-0-8sle | just use the rules mentioned in question and compute the array. while computing array keep track of maximum element till that index.\n2. when loop finishes max | kushguptacse | NORMAL | 2021-01-15T08:48:07.838793+00:00 | 2021-01-15T08:48:07.838832+00:00 | 122 | false | 1. just use the rules mentioned in question and compute the array. while computing array keep track of maximum element till that index.\n2. when loop finishes max is the answer.\n3. if n==0 return 0. else create array of size n+1. and set arr[1]=1.\n4. now check if arr[i] element is even. if yes just assign arr[i]=arr[i/2].\n5. if it is odd assign arr[i]=arr[i/2]+arr[i/2+1];\n```\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0) {\n return 0;\n }\n int[] arr=new int[n+1];\n arr[1]=1;\n int max=arr[1];\n for(int i=2;i<arr.length;i++) {\n if(i%2==0) {\n arr[i]=arr[i/2];\n } else {\n arr[i]=arr[i/2]+arr[i/2+1];\n }\n max=Math.max(arr[i],max);\n }\n return max;\n }\n}\n``` | 2 | 1 | ['Java'] | 0 |
get-maximum-in-generated-array | C++| 0ms faster than 100% | c-0ms-faster-than-100-by-aparna_g-drre | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n<=1) return n;\n vector<int> nums(n+1);\n nums[0] = 0;\n num | aparna_g | NORMAL | 2021-01-15T08:36:03.042882+00:00 | 2021-01-15T08:36:03.042912+00:00 | 180 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n<=1) return n;\n vector<int> nums(n+1);\n nums[0] = 0;\n nums[1] = 1;\n int ans = INT_MIN;\n for(int i=2;i<=n;i++) {\n if(i%2 ==0 )\n nums[i] = nums[i/2];\n else\n nums[i] = nums[(i-1)/2] + nums[(i-1)/2 + 1];\n ans = max(ans,nums[i]); \n }\n return ans;\n }\n};\n``` | 2 | 0 | ['C', 'C++'] | 0 |
get-maximum-in-generated-array | C++ Super Simple Short Solution 0 ms faster than 100% | c-super-simple-short-solution-0-ms-faste-8dy6 | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if (n == 0 || n == 1) return n;\n \n vector<int> arr(n+1);\n arr | yehudisk | NORMAL | 2021-01-14T21:43:10.541400+00:00 | 2021-01-14T21:43:10.541428+00:00 | 87 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if (n == 0 || n == 1) return n;\n \n vector<int> arr(n+1);\n arr[0] = 0;\n arr[1] = 1;\n int maxi = 1;\n \n for (int i = 2; i <= n; i++) {\n arr[i] = i % 2 == 0 ? arr[i/2] : arr[i / 2] + arr[i / 2 + 1];\n maxi = max(maxi, arr[i]);\n }\n \n return maxi;\n }\n};\n```\n**Like it? please upvote...** | 2 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | Easy and Clear Solution C++ 0ms (100%) | easy-and-clear-solution-c-0ms-100-by-moa-ja5b | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n==0)return 0;\n if(n==1)return 1;\n vector<int>nums;\n nums. | moazmar | NORMAL | 2021-01-07T01:35:05.349983+00:00 | 2021-01-07T01:35:05.350021+00:00 | 160 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n==0)return 0;\n if(n==1)return 1;\n vector<int>nums;\n nums.push_back(0);\n nums.push_back(1);\n int max=1;\n for(int i=2;i<=n;i++){\n if(i%2==0){\n nums.push_back(nums[i/2]);\n }else{\n nums.push_back(nums[i/2]+nums[i/2+1]);\n }\n if(nums[i]>max)max=nums[i];\n }\n return max; \n }\n};\n``` | 2 | 0 | ['C', 'C++'] | 0 |
get-maximum-in-generated-array | Java 0ms, straight forward solution | java-0ms-straight-forward-solution-by-da-wvob | \npublic int getMaximumGenerated(int n) {\n int[] res = new int[n+1];\n if(n == 0) {\n return 0;\n } else if(n == 1) {\n | DanielYoung | NORMAL | 2020-12-22T05:14:03.371603+00:00 | 2020-12-22T05:14:03.371642+00:00 | 102 | false | ```\npublic int getMaximumGenerated(int n) {\n int[] res = new int[n+1];\n if(n == 0) {\n return 0;\n } else if(n == 1) {\n return 1;\n }\n \n res[0] = 0;\n res[1] = 1;\n int max = 1;\n for(int i = 1; i<=n/2; i++) {\n res[2*i] = res[i];\n max = Math.max(max, res[2*i]);\n if(2*i+1<=n){\n res[2*i+1] = res[i] + res[i+1];\n max = Math.max(max, res[2*i+1]);\n }\n }\n return max;\n \n }\n``` | 2 | 2 | ['Java'] | 0 |
get-maximum-in-generated-array | C# - Brute Force O(N) | c-brute-force-on-by-christris-5i5y | csharp\npublic int GetMaximumGenerated(int n) \n{\n\tint[] result = new int[n + 1];\n\n\tif(n < 2)\n\t{\n\t\treturn n;\n\t}\n\n\tresult[1] = 1;\n\n\tfor(int i = | christris | NORMAL | 2020-11-08T04:04:58.812952+00:00 | 2020-11-08T04:04:58.812998+00:00 | 151 | false | ```csharp\npublic int GetMaximumGenerated(int n) \n{\n\tint[] result = new int[n + 1];\n\n\tif(n < 2)\n\t{\n\t\treturn n;\n\t}\n\n\tresult[1] = 1;\n\n\tfor(int i = 1; 2 * i <= n; i++)\n\t{\n\t\tint next = 2 * i; \n\t\tresult[next] = result[i];\n\n\t\tif(next + 1 <= n)\n\t\t{\n\t\t\tresult[next + 1] = result[i] + result[i + 1];\n\t\t}\n\t}\n\n\treturn result.Max();\n}\n``` | 2 | 0 | [] | 0 |
get-maximum-in-generated-array | [Python3] create array as told | python3-create-array-as-told-by-ye15-55mx | \n\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if not n: return 0 # edge case \n ans = [0, 1]\n for i in range(2, | ye15 | NORMAL | 2020-11-08T04:02:16.721863+00:00 | 2021-01-15T18:33:55.290867+00:00 | 262 | false | \n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if not n: return 0 # edge case \n ans = [0, 1]\n for i in range(2, n+1): \n ans.append(ans[i>>1] + (ans[i+1>>1] if i&1 else 0))\n return max(ans) \n```\n\nAlternative implementation via funcitons\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n \n @lru_cache(None)\n def fn(k):\n """Return kth value."""\n if k <= 1: return k\n if k&1: return fn(k>>1) + fn(k+1>>1)\n return fn(k>>1)\n \n return max(fn(k) for k in range(n+1))\n``` | 2 | 0 | ['Python3'] | 1 |
get-maximum-in-generated-array | ⭐️ [ Kt, Js, Py3, Cpp ] 🎯 Max of Generated Array | kt-js-py3-cpp-max-of-generated-array-by-wr9la | Synopsis:\n\nReturn the maximum of the generated array A.\n\n---\n \nContest 214 Screenshare:\n\n\n\nhttps://www.youtube.com/watch?v=xk32NZbA4tM\n \n---\n | claytonjwong | NORMAL | 2020-11-08T04:00:22.519202+00:00 | 2020-11-08T04:16:05.731848+00:00 | 402 | false | **Synopsis:**\n\nReturn the maximum of the generated array `A`.\n\n---\n \n**Contest 214 Screenshare:**\n\n\n\nhttps://www.youtube.com/watch?v=xk32NZbA4tM\n \n---\n\n*Kotlin*\n```\nclass Solution {\n fun getMaximumGenerated(N: Int): Int {\n if (N < 2)\n return N\n var A = IntArray(N + 1)\n A[0] = 0\n A[1] = 1\n var i = 0\n while (2 * i <= N) {\n if (2 * i <= N) A[2 * i ] = A[i]\n if (2 * i + 1 <= N) A[2 * i + 1] = A[i] + A[i + 1]\n ++i\n }\n return A.max()!!\n }\n}\n```\n\n*Javascript*\n```\nlet getMaximumGenerated = N => {\n if (N < 2)\n return N;\n let A = Array(N + 1);\n A[0] = 0;\n A[1] = 1;\n for (let i = 1; 2 * i <= N; ++i) {\n if (2 * i <= N) A[2 * i ] = A[i];\n if (2 * i + 1 <= N) A[2 * i + 1] = A[i] + A[i + 1];\n }\n return Math.max(...A);\n};\n```\n\n*Python3*\n```\nclass Solution:\n def getMaximumGenerated(self, N: int) -> int:\n if N < 2:\n return N\n A = [0] * (N + 1)\n A[0] = 0\n A[1] = 1\n i = 1\n while 2 * i <= N:\n if 2 * i <= N: A[2 * i ] = A[i]\n if 2 * i + 1 <= N: A[2 * i + 1] = A[i] + A[i + 1]\n i += 1\n return max(A)\n```\n\n*C++*\n```\nclass Solution {\npublic:\n using VI = vector<int>;\n int getMaximumGenerated(int N) {\n if (N < 2)\n return N;\n VI A(N + 1);\n A[0] = 0;\n A[1] = 1;\n for (auto i{ 1 }; 2 * i <= N ; ++i) {\n if (2 * i <= N) A[2 * i ] = A[i];\n if (2 * i + 1 <= N) A[2 * i + 1] = A[i] + A[i + 1];\n }\n return *max_element(A.begin(), A.end());\n }\n};\n``` | 2 | 1 | [] | 1 |
get-maximum-in-generated-array | O(n) ☠️ | on-by-varuntyagig-tvu7 | Complexity
Time complexity:
O(n)
Space complexity:
O(1)
Code | varuntyagig | NORMAL | 2025-02-03T06:18:44.420711+00:00 | 2025-02-03T06:18:44.420711+00:00 | 83 | false | # Complexity
- Time complexity:
$$O(n)$$
- Space complexity:
$$O(1)$$
# Code
```cpp []
class Solution {
public:
int getMaximumGenerated(int n) {
if (n == 0) {
return 0;
}
vector<int> nums(n + 1);
nums[0] = 0;
nums[1] = 1;
for (int i = 1; i < n; i++) {
if (((2 * i) > n) || ((2 * i) + 1 > n)) {
break;
}
nums[2 * i] = nums[i];
nums[(2 * i) + 1] = nums[i] + nums[i + 1];
}
int maxi = INT_MIN;
for (int i = 0; i < nums.size(); i++) {
maxi = max(maxi, nums[i]);
}
return maxi;
}
};
``` | 1 | 0 | ['Array', 'Simulation', 'C++'] | 0 |
get-maximum-in-generated-array | Easy Solution in C | easy-solution-in-c-by-sathurnithy-gy8h | Code | Sathurnithy | NORMAL | 2025-01-19T12:02:32.292861+00:00 | 2025-01-19T12:02:32.292861+00:00 | 63 | false |
# Code
```c []
int getMaximumGenerated(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
int* arr = calloc(n+1, sizeof(int));
arr[1] = 1;
int max = 1;
for(int i=1; 2*i+1 <= n; i++){
arr[2*i] = arr[i];
if(arr[2*i] > max) max = arr[2*i];
arr[2*i+1] = arr[i] + arr[i+1];
if(arr[2*i+1] > max) max = arr[2*i+1];
}
return max;
}
``` | 1 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | Easy Solution in Java | easy-solution-in-java-by-sathurnithy-xy6h | Code | Sathurnithy | NORMAL | 2025-01-19T11:56:32.403252+00:00 | 2025-01-19T11:56:32.403252+00:00 | 110 | false | # Code
```java []
class Solution {
public int getMaximumGenerated(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
int[] nums = new int[n+1];
nums[0] = 0;
nums[1] = 1;
int max = 1;
for (int i = 2; i <= n; i++) {
if (i % 2 == 0) nums[i] = nums[i / 2];
else nums[i] = nums[i / 2] + nums[i / 2 + 1];
max = Math.max(max, nums[i]);
}
return max;
}
}
``` | 1 | 0 | ['Java'] | 0 |
get-maximum-in-generated-array | EASIST JAVA CODE IN 4-5 LINES BEATING 100% in 0ms | easist-java-code-in-4-5-lines-beating-10-3xd0 | Complexity
Time complexity: O(n)
Space complexity: O(n)
Code | arshi_bansal | NORMAL | 2025-01-17T15:44:21.531248+00:00 | 2025-01-17T15:44:21.531248+00:00 | 71 | 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
```java []
class Solution {
public int getMaximumGenerated(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int[] arr = new int[n + 1];
arr[0] = 0;
arr[1] = 1;
int max = 1;
for (int i = 2; i <= n; i++) {
if (i % 2 == 0) {
arr[i] = arr[i / 2];
} else {
arr[i] = arr[i / 2] + arr[i / 2 + 1];
}
max = Math.max(max, arr[i]);
}
return max;
}
}
``` | 1 | 0 | ['Array', 'Math', 'Java'] | 0 |
get-maximum-in-generated-array | Solution in C | solution-in-c-by-vickyy234-eeyh | Code | vickyy234 | NORMAL | 2025-01-10T19:46:23.198496+00:00 | 2025-01-10T19:46:23.198496+00:00 | 27 | false | # Code
```c []
int getMaximumGenerated(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
int* nums = malloc(sizeof(int) * (n + 1));
nums[1] = 1;
int max = 1;
for (int i = 2; i <= n; i++) {
if (i % 2 == 0)
nums[i] = nums[i / 2];
else
nums[i] = nums[i / 2] + nums[i / 2 + 1];
if (max < nums[i])
max = nums[i];
}
free(nums);
return max;
}
``` | 1 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | C solution | c-solution-by-pavithrav25-742f | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | pavithrav25 | NORMAL | 2025-01-07T04:35:45.210189+00:00 | 2025-01-07T04:35:45.210189+00:00 | 16 | 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
```c []
int getMaximumGenerated(int n) {
if(n==0)
return 0;
if(n==1)
return 1;
int nums[n+1];
nums[0]=0;
nums[1]=1;
for(int i=2;i<=n;i++){
if(i%2==0)
nums[i]=nums[i/2];
else
nums[i]=nums[i/2]+nums[i/2+1];
}
int max=nums[0];
for(int i=1;i<=n;i++){
if(nums[i]>max)
max=nums[i];
}
return max;
}
``` | 1 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | Beats 100%. 0ms. Three Lines Only Solution | beats-100-0ms-three-lines-only-solution-dwpfh | IntuitionThe constraints of this problem are small (only 100) and the maximum value returned is only 21. Moreover, all results are increase as n increases. Ther | charnavoki | NORMAL | 2025-01-03T14:50:15.658180+00:00 | 2025-01-03T14:50:15.658180+00:00 | 52 | false | # Intuition
The **constraints** of this problem are small (only **100**) and the maximum value returned is only **21**. Moreover, all results are increase as `n` increases. Therefore, it is very easy to precompute the values and **hard-code** the results. This guarantees the best performance.
```javascript []
var getMaximumGenerated = function(n) {
const ind = [0, 1, 3, 5, 9, 11, 19, 21, 35, 37, 43, 69, 73, 75, 83, 85],
val = [0, 1, 2, 3, 4, 5, 7, 8, 9, 11, 13, 14, 15, 18, 19, 21];
return val[ind.findLastIndex(v => v <= n)];
};
```
### Plase upvote, you motivate me to solve problems in original ways | 1 | 0 | ['JavaScript'] | 0 |
get-maximum-in-generated-array | Better Runtime using Constructor Working Method ! | better-runtime-using-constructor-working-6x92 | Intuition\n Describe your first thoughts on how to solve this problem. \nWe can pre-generate all possible arrays up to a given size and store them in a list. Th | NamedAdam | NORMAL | 2024-09-14T20:45:57.008479+00:00 | 2024-09-14T20:45:57.008516+00:00 | 17 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**We can pre-generate all possible arrays up to a given size and store them in a list. This allows us to quickly retrieve the maximum value for a given index in constant time.**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Pre-generate all possible arrays up to size 100 using a constructor.\n- Store the pre-generated arrays in a list.\n- For each query, simply return the maximum value in the pre-generated list up to the given index.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- Pre-generation phase: O(1) (constant time complexity)\n- getMaximumGenerated method: O(n) (where n is the input size)\n- Overall time complexity: O(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- O(1) (constant space complexity), since we\'re storing a fixed number of pre-generated arrays.\n\n# Code\n```python3 []\nclass Solution:\n @staticmethod\n def _pre_generated_():\n generated_list : list[int] = [0, 1]\n for index in range(2, 101):\n if index % 2 == 0:\n generated_list.append(generated_list[index // 2])\n continue\n\n generated_list.append(generated_list[index // 2] + generated_list[index // 2 + 1])\n \n return generated_list\n\n def __init__(self):\n self.generated_list : list[int] = self._pre_generated_()\n\n def getMaximumGenerated(self, n: int) -> int:\n if n <= 1:\n return n\n \n return max(self.generated_list[ : n + 1])\n``` | 1 | 0 | ['Python3'] | 0 |
get-maximum-in-generated-array | arey bhaisahaab n=0 case handle krnaa yaad se :D | arey-bhaisahaab-n0-case-handle-krnaa-yaa-65an | 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 | yesyesem | NORMAL | 2024-09-05T19:07:46.906308+00:00 | 2024-09-05T19:07:46.906340+00:00 | 135 | 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)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n==0)\n return 0;\n vector<int>nums(n+1);\n \n nums[0]=0;\n nums[1]=1;\n \n\n for(int i=2;i<=n;i++)\n {\n if(i%2==0)\n nums[i]=nums[i/2];\n else\n nums[i]=nums[i/2]+nums[(i/2)+1];\n \n }\n\n \n return *max_element(nums.begin(),nums.end());\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
get-maximum-in-generated-array | easy understand solution | easy-understand-solution-by-inshhadd-0imb | 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 | inshhadd | NORMAL | 2024-02-18T09:07:58.826690+00:00 | 2024-02-18T09:07:58.826719+00:00 | 306 | 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)$$ -->\n\n# Code\n```\n/**\n * @param {number} n\n * @return {number}\n */\nvar getMaximumGenerated = function(n) {\n \n if (n === 0) return 0;\n\n const nums = new Array(n + 1).fill(0);\n nums[1] = 1;\n\n for (let i = 2; i <= n; i++) {\n if (i % 2 === 0) {\n nums[i] = nums[i / 2];\n } else {\n const index = Math.floor(i / 2);\n nums[i] = nums[index] + nums[index + 1];\n }\n }\n\n return Math.max(...nums);\n\n\n};\n``` | 1 | 0 | ['JavaScript'] | 0 |
get-maximum-in-generated-array | 📈 1646. Get Maximum in Generated Array | 1646-get-maximum-in-generated-array-by-m-tl13 | Intuition\nThe problem follows a simple pattern where each element in the array is generated based on specific rules defined in the problem statement.\n\n# Appr | muqimjon | NORMAL | 2024-02-16T06:54:11.135969+00:00 | 2024-02-16T06:54:11.136008+00:00 | 14 | false | # Intuition\nThe problem follows a simple pattern where each element in the array is generated based on specific rules defined in the problem statement.\n\n# Approach\nTo solve the problem, we can iterate through the range [0, n], generating each element of the array according to the given rules. We maintain a variable to track the maximum element encountered during the generation process.\n\n# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```csharp\npublic class Solution {\n public int GetMaximumGenerated(int n) {\n if (n == 0) return 0;\n \n int[] nums = new int[n + 1];\n nums[1] = 1;\n int max = 1;\n \n for (int i = 2; i <= n; i++) {\n nums[i] = nums[i / 2] + i % 2 * nums[i / 2 + 1];\n max = Math.Max(max, nums[i]);\n }\n \n return max;\n }\n}\n```\n | 1 | 0 | ['C#'] | 0 |
get-maximum-in-generated-array | Simple java code 0 ms beats 100 % | simple-java-code-0-ms-beats-100-by-arobh-fcga | \n# Complexity\n- \n\n# Code\n\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0||n==1) return n;\n int arr[]=new int[n+1] | Arobh | NORMAL | 2024-01-17T14:20:13.996062+00:00 | 2024-01-17T14:20:13.996112+00:00 | 12 | false | \n# Complexity\n- \n\n# Code\n```\nclass Solution {\n public int getMaximumGenerated(int n) {\n if(n==0||n==1) return n;\n int arr[]=new int[n+1];\n arr[0]=0; arr[1]=1;\n int max=1;\n for(int i=1;i<=n;i++){\n if(2*i<=n){\n arr[2*i]=arr[i];\n }\n if(2*i + 1 <=n){\n arr[2*i + 1]=arr[i]+arr[i+1];\n }\n max=Math.max(max,arr[i]);\n }\n return max;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
get-maximum-in-generated-array | get-maximum-in-generated-array | get-maximum-in-generated-array-by-_suraj-4kms | Code\n\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n nums = []\n nums.append(0)\n nums.append(1)\n for i in | _suraj__007 | NORMAL | 2023-11-17T05:30:51.744494+00:00 | 2023-11-17T05:30:51.744527+00:00 | 505 | false | # Code\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n nums = []\n nums.append(0)\n nums.append(1)\n for i in range(2,n+1):\n if i%2==0:\n nums.append(nums[i//2])\n else:\n nums.append(nums[i//2] + nums[(i//2) + 1])\n if n==0:\n return 0\n return max(nums)\n\n \n``` | 1 | 0 | ['Python3'] | 0 |
get-maximum-in-generated-array | easy solution using only one for loop | easy-solution-using-only-one-for-loop-by-3h1s | 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 | leet1101 | NORMAL | 2023-10-25T09:23:50.000122+00:00 | 2023-10-25T09:23:50.000142+00:00 | 20 | 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:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n vector <int> nums ;\n if (n==0) return 0 ;\n if (n==1) return 1 ;\n // nums[0] = 0 ;\n // nums[1] = 1 ;\n nums.push_back(0) ;\n nums.push_back(1) ;\n int max=INT_MIN ;\n for (int i=2; i<=n; i++){\n if (i%2==0){\n // nums[i] = nums[i/2] ;\n nums.push_back(nums[i/2]) ;\n }\n else {\n // nums[i] = nums[i/2] + nums[i/2+1] ;\n int ele = nums[i/2] + nums[i/2+1] ;\n nums.push_back(ele) ;\n }\n if (nums[i] >= max){\n max = nums[i] ;\n }\n }\n return max ;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
get-maximum-in-generated-array | Simplest code with runtime 45 ms | simplest-code-with-runtime-45-ms-by-hris-qw3m | \n\n# Code\n\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n dp=[0,1]\n for i in range(2,n+1):\n dp += [dp[i//2] | hrishikeshprasadc | NORMAL | 2023-10-17T08:06:17.648110+00:00 | 2023-10-17T08:06:17.648136+00:00 | 176 | false | \n\n# Code\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n dp=[0,1]\n for i in range(2,n+1):\n dp += [dp[i//2] if i%2==0 else dp[i//2]+ dp[i//2+1]]\n return max(dp[:n+1])\n``` | 1 | 0 | ['Python3'] | 0 |
get-maximum-in-generated-array | DP | Python3 | dp-python3-by-joshua_mur-tlfm | 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, e.g. O(n) \n\n# Co | joshua_mur | NORMAL | 2023-07-31T19:32:08.875273+00:00 | 2023-07-31T19:32:08.875290+00:00 | 52 | false | # 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, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n dp = [0] * (n + 2)\n dp[1] = 1 \n ans = 0\n for i in range(1, n // 2 + 1):\n dp[i * 2] = dp[i]\n dp[i * 2 + 1] = dp[i] + dp[i + 1]\n ans = max(dp[:n + 1])\n return ans\n``` | 1 | 0 | ['Dynamic Programming', 'Python3'] | 0 |
get-maximum-in-generated-array | Beats 100% | Simple DP solution | beats-100-simple-dp-solution-by-anandsya-twln | Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n == | anandsyadav257 | NORMAL | 2023-07-10T09:19:48.552713+00:00 | 2023-07-10T09:19:48.552737+00:00 | 6 | false | # Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n == 0) return 0;\n if(n == 1) return 1;\n int dp[n+1];\n dp[0] = 0, dp[1] = 1;\n int ans = 1;\n for(int i=2;i<=n;i++)\n {\n if(i % 2 == 0)\n {\n dp[i] = dp[i/2];\n }\n else {\n dp[i] = dp[i/2] + dp[i/2 + 1];\n }\n ans = max(ans,dp[i]); \n }\n return ans;\n }\n};\n``` | 1 | 0 | ['Array', 'Dynamic Programming', 'Simulation', 'C++'] | 0 |
get-maximum-in-generated-array | bottom up approach easiest 9 lines code python | bottom-up-approach-easiest-9-lines-code-ja5f6 | 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 | Mohammad_tanveer | NORMAL | 2023-03-18T04:55:54.229024+00:00 | 2023-03-18T04:55:54.229068+00:00 | 931 | 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)$$ -->\n\n# Code\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n tb=[0,1]\n if n==0:\n return 0\n for i in range(2,n+1):\n if i%2==0:\n tb.append(tb[i//2])\n else:\n tb.append(tb[i//2]+tb[(i//2)+1])\n return max(tb)\n``` | 1 | 0 | ['Python3'] | 0 |
get-maximum-in-generated-array | JavaScript. Easy to understand code. | javascript-easy-to-understand-code-by-ze-fzmv | \n\n# Complexity\n- Time complexity:\nO(n)\n\n# Code\n\n/**\n * @param {number} n\n * @return {number}\n */\nvar getMaximumGenerated = function (n) {\n if (n | ZeemanFactor | NORMAL | 2023-02-12T18:34:05.088139+00:00 | 2023-02-12T18:34:30.923715+00:00 | 82 | false | \n\n# Complexity\n- Time complexity:\nO(n)\n\n# Code\n```\n/**\n * @param {number} n\n * @return {number}\n */\nvar getMaximumGenerated = function (n) {\n if (n == 0) return 0\n if (n == 1) return 1\n var dp = new Array(n + 1).fill(0)\n dp[1] = 1\n for (i = 2; i <= n; i++) {\n if (i % 2 == 0) {\n dp[i] = dp[i / 2]\n } else {\n dp[i] = dp[(i - 1) / 2] + dp[((i - 1) / 2) + 1]\n }\n }\n return Math.max(...dp)\n};\n``` | 1 | 0 | ['Array', 'Dynamic Programming', 'Simulation', 'JavaScript'] | 0 |
get-maximum-in-generated-array | [C++] 1646. Get Maximum in Generated Array | c-1646-get-maximum-in-generated-array-by-tpnk | 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 | pyCERN | NORMAL | 2023-01-27T06:02:52.434691+00:00 | 2023-01-27T06:02:52.434738+00:00 | 125 | 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)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int max3(int a, int b, int c) {\n return max(max(a, b), c);\n }\n\n int getMaximumGenerated(int n) {\n int nums[110] = { 0, };\n int maxValue = min(n, 1);\n nums[0] = 0; nums[1] = 1;\n\n for (int i = 1; i < (int)(n+1)/2; i++) {\n nums[2*i] = nums[i];\n nums[2*i+1] = nums[i] + nums[i+1];\n maxValue = max3(maxValue, nums[2*i], nums[2*i+1]);\n }\n\n return maxValue;\n }\n};\n``` | 1 | 0 | ['C++'] | 1 |
get-maximum-in-generated-array | Simple Python Solution | simple-python-solution-by-mahmudul-hasan-jh18 | 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 | Mahmudul-Hasan-Robin | NORMAL | 2023-01-27T04:09:54.176114+00:00 | 2023-01-27T04:09:54.176150+00:00 | 103 | 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)$$ -->\n\n# Code\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n temp=[]\n for i in range(n+1):\n if i==0:\n temp.append(0)\n elif i==1:\n temp.append(1)\n elif i%2==0:\n temp.append(temp[i//2])\n else:\n temp.append(temp[i//2]+temp[i//2+1])\n return max(temp)\n``` | 1 | 0 | ['Dynamic Programming', 'Python3'] | 0 |
get-maximum-in-generated-array | C++ | Short SImple solution | c-short-simple-solution-by-sarthak8008-t8dq | Did as directed !\n\n\nint getMaximumGenerated(int n) {\n // nums[2*i]=nums[i]\n // nums[even=i]=nums[i/2]\n // nums[2*i+1]=nums[i]+nums[i+ | sarthak8008 | NORMAL | 2022-10-11T15:41:54.550398+00:00 | 2022-10-11T17:17:42.216583+00:00 | 7 | false | Did as directed !\n\n```\nint getMaximumGenerated(int n) {\n // nums[2*i]=nums[i]\n // nums[even=i]=nums[i/2]\n // nums[2*i+1]=nums[i]+nums[i+1]\n // nums[odd=i]=nums[i/2]+nums[(i/2)+1]\n if(n==0) return 0;\n if(n==1) return 1;\n vector<int> nums;\n nums.push_back(0);\n nums.push_back(1);\n for(int i=2;i<=n;i++){\n if(i%2==0)\n nums.push_back(nums[i/2]);\n else\n nums.push_back(nums[i/2]+nums[(i/2)+1]);\n \n }\n int mx=INT_MIN;\n for(auto x:nums)\n mx=max(mx,x);\n \n return mx;\n \n }\n```\n\nPlease upvote if you found helpful! | 1 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | C++ Simple Code Beats 100% in Time and 93% in Space | c-simple-code-beats-100-in-time-and-93-i-aixw | ```\nclass Solution {\npublic:\n int ans=0;\n int recur(int n)\n {\n if(n==0 || n==1)\n return n;\n else if(n%2==0)\n | rishabh2806 | NORMAL | 2022-10-10T14:29:29.406333+00:00 | 2022-10-10T14:29:29.406380+00:00 | 361 | false | ```\nclass Solution {\npublic:\n int ans=0;\n int recur(int n)\n {\n if(n==0 || n==1)\n return n;\n else if(n%2==0)\n ans=recur(n/2);\n else\n ans=recur(n/2)+recur(n/2+1);\n return ans;\n }\n \n int getMaximumGenerated(int n) {\n int max=INT_MIN;\n for(int i=0;i<=n;i++)\n {\n int r=recur(i);\n if(max<r)\n max=r;\n }\n return max;\n }\n\n}; | 1 | 0 | ['Recursion', 'C++'] | 0 |
get-maximum-in-generated-array | Runtime: 0 ms, faster than 100.00% of C++ online submissions ! | runtime-0-ms-faster-than-10000-of-c-onli-e26f | If you find this helpful, Please Upvote\nThank You !\n\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n\n if (n == 0 || n == 1) \n | dhairyashiil | NORMAL | 2022-10-02T18:42:44.645833+00:00 | 2022-10-02T18:42:44.645873+00:00 | 1,109 | false | ***If you find this helpful, Please Upvote\nThank You !***\n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n\n if (n == 0 || n == 1) \n return n;\n \n vector<int> vec(n+1);\n \n vec[0] = 0; // For nums[0] = 0\n vec[1] = 1; // For nums[1] = 1\n \n int maxi = 1; // here we are initializing maxi by 1 bcoz, till now max value is 1 (Max value between 0 and 1 is 1)\n \n for (int i=2; i<=n; i++) {\n \n if(i%2 == 0) { // For nums[2 * i] = nums[i] when 2 <= 2 * i <= n\n vec[i] = vec[i/2];\n }\n else { // nums[2 * i + 1] = nums[i] + nums[i + 1] when 2 <= 2 * i + 1 <= n\n vec[i] = vec[i/2] + vec[i/2 + 1];\n }\n \n maxi = max(maxi, vec[i]); // Update Max Element\n }\n return maxi;\n }\n};\n``` | 1 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | ✔ C++ || Simple DP Solution || Beginner Friendly | c-simple-dp-solution-beginner-friendly-b-wmox | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n == 0 || n == 1)\n return n;\n vector<int> nums(n + 1);\n | BharatUpadhyay | NORMAL | 2022-09-10T09:05:00.764676+00:00 | 2022-09-10T09:05:00.764717+00:00 | 356 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if(n == 0 || n == 1)\n return n;\n vector<int> nums(n + 1);\n nums[0] = 0;\n nums[1] = 1;\n for(int i = 2; i <= n; i++)\n {\n if(i % 2 == 0)\n nums[i] = nums[i/2];\n else\n nums[i] = nums[i/2] + nums[(i/2) + 1];\n }\n return *max_element(nums.begin(), nums.end());\n }\n};\n``` | 1 | 0 | ['Dynamic Programming', 'C'] | 0 |
get-maximum-in-generated-array | C++ || Easy solution using DP | c-easy-solution-using-dp-by-shodydosh-jkdd | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n int nums[n+1];\n int maxVal = 1;\n \n if (n == 1) return 1;\n | Shodydosh | NORMAL | 2022-08-17T01:07:35.184376+00:00 | 2022-08-17T01:07:35.184405+00:00 | 242 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n int nums[n+1];\n int maxVal = 1;\n \n if (n == 1) return 1;\n if (n == 0) return 0;\n \n nums[0] = 0;\n nums[1] = 1;\n \n for(int i=0; i<=n; i++){\n if(i%2 == 0){\n nums[i] = nums[i/2];\n } else {\n nums[i] = nums[i/2]+nums[i/2 + 1];\n }\n \n maxVal = max(maxVal, nums[i]);\n }\n \n return maxVal;\n }\n}; | 1 | 0 | ['C', 'C++'] | 0 |
get-maximum-in-generated-array | Simple Python Solution | simple-python-solution-by-zip_demons-5fgd | Upvote if the solution if helpful. Comment for any doubts or suggestions.\n\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n < | zip_demons | NORMAL | 2022-08-16T08:08:40.922089+00:00 | 2022-08-16T08:08:40.922121+00:00 | 365 | false | **Upvote if the solution if helpful. Comment for any doubts or suggestions.**\n```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n if n < 2:\n return n\n arr = [0]*(n+1)\n arr[1] = 1\n max_num = 0\n for i in range(2, n+1):\n if i%2 == 0:\n arr[i] = arr[i//2]\n else:\n arr[i] = arr[i//2] + arr[i//2 + 1]\n max_num = max(max_num, arr[i])\n return max_num\n``` | 1 | 0 | ['Python', 'Python3'] | 1 |
get-maximum-in-generated-array | 0ms Faster than 100% Solution in C++, Easy to understand(O(N)) | 0ms-faster-than-100-solution-in-c-easy-t-3d38 | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n vector<int>dp(n+1, 0);\n if(n == 0 || n == 1) return n;\n dp[0] = 0;\n | NextThread | NORMAL | 2022-07-28T06:53:41.890903+00:00 | 2022-07-28T06:53:41.890943+00:00 | 37 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n vector<int>dp(n+1, 0);\n if(n == 0 || n == 1) return n;\n dp[0] = 0;\n dp[1] = 1;\n for (int i = 2; i <= n; i++)\n dp[i] = i % 2 == 0 ? dp[i/2] : dp[i / 2] + dp[i / 2 + 1];\n return *max_element(dp.begin(), dp.end());\n }\n};\n``` | 1 | 0 | ['Dynamic Programming', 'C'] | 0 |
get-maximum-in-generated-array | C++ : Simple Solution | c-simple-solution-by-beast_paw-u1q8 | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n int mx=INT_MIN;\n if(n==0)\n return 0;\n if(n==1)\n | beast_paw | NORMAL | 2022-07-24T10:40:04.357598+00:00 | 2022-07-24T10:40:04.357636+00:00 | 97 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n int mx=INT_MIN;\n if(n==0)\n return 0;\n if(n==1)\n return 1;\n vector<int>nums(n+6);\n nums[0]=0;\n nums[1]=1;\n for(int i=2;i<=n;i++)\n {\n if(i%2==0)\n nums[i]=nums[i/2];\n else\n nums[i]=nums[i/2]+nums[i/2+1];\n mx=max(mx,nums[i]);\n }\n \n return mx;\n }\n};\n``` | 1 | 0 | ['C'] | 0 |
get-maximum-in-generated-array | C++ || DP Memoization || 100 % Faster | c-dp-memoization-100-faster-by-vijay_cpp-yz5z | \nclass Solution {\npublic:\n int getMax(int ind, vector<int> &nums, vector<int> &dp)\n {\n \n if(dp[ind] != -1)\n return d | vijay_cpp | NORMAL | 2022-07-21T02:48:22.647527+00:00 | 2022-07-21T03:04:02.829529+00:00 | 120 | false | ```\nclass Solution {\npublic:\n int getMax(int ind, vector<int> &nums, vector<int> &dp)\n {\n \n if(dp[ind] != -1)\n return dp[ind];\n \n if(ind < 0)\n return 0;\n \n if(ind % 2 == 0)\n dp[ind] = getMax(ind / 2, nums, dp);\n else\n dp[ind] = getMax(ind / 2, nums, dp) + getMax((ind/2)+1, nums, dp);\n \n return dp[ind];\n }\n \n \n int getMaximumGenerated(int n) {\n if(n<=1)\n return n;\n \n vector<int> nums(n+1, -1); \n vector<int> dp(n+1, -1);\n dp[0] = nums[0] = 0;\n dp[1] = nums[1] = 1;\n \n \n for(int i=2; i<=n; ++i)\n nums[i] = getMax(i, nums, dp);\n \n int maxi = INT_MIN;\n for(auto it: nums)\n {\n maxi = max(maxi, it);\n }\n \n return maxi;\n }\n};\n``` | 1 | 0 | ['Dynamic Programming', 'Memoization', 'C'] | 0 |
get-maximum-in-generated-array | c++ || 100 % faster || DP SOLUTION | c-100-faster-dp-solution-by-universal_va-gv9l | class Solution {\npublic:\n\n\n int getMaximumGenerated(int n) {\n if(n==0){\n return 0;\n }\n vector dp(n+1,0);\n dp[ | universal_vaibhav | NORMAL | 2022-07-19T20:52:03.567874+00:00 | 2022-07-19T20:52:37.324661+00:00 | 51 | false | **class Solution {\npublic:**\n\n\n int getMaximumGenerated(int n) {\n if(n==0){\n return 0;\n }\n vector<int> dp(n+1,0);\n dp[0]=0;\n dp[1]=1;\n \n for(int i=2;i<=n;++i){\n if(i&1){\n dp[i]=dp[i/2]+dp[(i/2)+1];\n }\n else{\n dp[i]=dp[i/2];\n }\n }\n int maxi=0;\n for(int i=0;i<=n;++i){\n maxi=max(maxi,dp[i]);\n }\n return maxi;\n }\n**};** | 1 | 0 | ['Dynamic Programming', 'C'] | 0 |
get-maximum-in-generated-array | Solution ( 96.51% Faster) | solution-9651-faster-by-fiqbal997-hcrr | \nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n l1 = [0,1]\n if n == 2 or n == 1:\n return (1)\n elif n = | fiqbal997 | NORMAL | 2022-07-12T06:05:31.261130+00:00 | 2022-07-12T06:05:31.261176+00:00 | 273 | false | ```\nclass Solution:\n def getMaximumGenerated(self, n: int) -> int:\n l1 = [0,1]\n if n == 2 or n == 1:\n return (1)\n elif n == 0:\n return (0)\n for i in range(1,n):\n l1.append(l1[i])\n if (i * 2) == n:\n break\n l1.append((l1[i]) + (l1[i+1]))\n if (((i * 2)+1) == n):\n break\n return (max(l1))\n\n``` | 1 | 0 | ['Python', 'Python3'] | 0 |
get-maximum-in-generated-array | ✅C++ Solution | c-solution-by-ke4e-ctme | \n\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n vector<int> nums(n+1);\n if (n<=1) return n;\n nums[0]=0;\n nu | ke4e | NORMAL | 2022-07-06T12:06:03.720508+00:00 | 2022-07-06T12:06:03.720542+00:00 | 70 | false | \n```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n vector<int> nums(n+1);\n if (n<=1) return n;\n nums[0]=0;\n nums[1]=1;\n for (int i=2; i<=n; i++) {\n if (i%2==0) {\n nums[i]=nums[i/2];\n } else {\n nums[i]=nums[i/2]+nums[(i/2)+1];\n }\n }\n int max=0;\n for (int i=0; i<=n; i++) {\n if(max<nums[i])\n max=nums[i];\n }\n return max;\n }\n};\n```\n | 1 | 0 | ['C'] | 0 |
minimum-cost-to-reach-every-position | Python3 || 1 line, accumulate || T/S: 99% / 97% | python3-1-line-accumulate-by-spaulding-a5xq | The problem is is equivalent to this problem: For each element in cost, determine the minimum value in costs preceding that element.https://leetcode.com/problem | Spaulding_ | NORMAL | 2025-03-30T04:06:22.388883+00:00 | 2025-04-02T16:36:35.887774+00:00 | 740 | false | The problem is is equivalent to this problem: *For each element in cost, determine the minimum value in costs preceding that element.*
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
return list(accumulate(cost, min))
```
```cpp []
class Solution {
public:
std::vector<int> minCosts(std::vector<int>& cost) {
std::vector<int> result(cost.size());
std::partial_sum(cost.begin(), cost.end(), result.begin(), [](int a, int b) {
return std::min(a, b);
});
return result;}
};
```
```java []
class Solution {
public int[] minCosts(int[] cost) {
int[] res = new int[cost.length];
for (int i = 0; i < res.length; i++)
res[i] = Math.min(i > 0 ? res[i - 1] : Integer.MAX_VALUE, cost[i]);
return res;}
}
```
[https://leetcode.com/problems/minimum-cost-to-reach-every-position/submissions/1594582551/](https://leetcode.com/problems/minimum-cost-to-reach-every-position/submissions/1594582551/)
I could be wrong, but I think that time complexity is *O*(*N*) and space complexity is *O*(*N*), in which *N* ~ `len(cost)`. | 21 | 0 | ['C++', 'Java', 'Python3'] | 2 |
minimum-cost-to-reach-every-position | ⌚【2-liner】🚀💻✅BEATS 100%⌛⚡[C++/Java/Py3/JS]🍨| EASY n CLEAN EXPLANATION⭕💌 | 2-liner-beats-100cjavapy3js-easy-n-clean-eju9 | IntuitionThe problem requires computing the minimum cost up to each index in the given cost array. Essentially, for each index i, we need to find the minimum va | Fawz-Haaroon | NORMAL | 2025-03-31T09:01:27.755248+00:00 | 2025-04-01T07:32:53.645985+00:00 | 516 | false | # **Intuition**
The problem requires computing the minimum cost up to each index in the given `cost` array. Essentially, for each index `i`, we need to find the minimum value among all elements from the start to `i`.
To achieve this efficiently, we maintain a running minimum while iterating over the array.
# **Approach**
1. Initialize an answer array `ans[]` of the same size as `cost[]`.
2. Iterate over each index `i` in `cost[]`.
3. For each index `i`, compute `ans[i]` as the minimum of:
- The previous minimum stored in `ans[i-1]` (if `i > 0`).
- The current `cost[i]`.
4. The first element `ans[0]` is simply `cost[0]` because there's no previous element to compare with.
5. Return the resulting `ans[]`, which contains the minimum cost up to each index.
# **Complexity**
- **Time Complexity:** `O(n)`, as we traverse the `cost` array once.
- **Space Complexity:** `O(n)`, since we use an additional array of the same size as `cost`.
# **Code**
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
vector<int> ans(cost.size());
for (int i = 0; i < ans.size(); i++)
ans[i] = min((i ? ans[i - 1] : INT_MAX), cost[i]);
return ans;
}
};
```
```java []
class Solution {
public int[] minCosts(int[] cost) {
int[] ans = new int[cost.length];
for (int i = 0; i < ans.length; i++)
ans[i] = Math.min(i > 0 ? ans[i - 1] : Integer.MAX_VALUE, cost[i]);
return ans;
}
}
```
```python3 []
class Solution:
def minCosts(self, cost: List[int]) -> List[int]:
ans = [0] * len(cost)
for i in range(len(cost)):
ans[i] = min(ans[i - 1] if i > 0 else float('inf'), cost[i])
return ans
```
```javascript []
/**
* @param {number[]} cost
* @return {number[]}
*/
var minCosts = function(cost) {
let ans = new Array(cost.length);
for (let i = 0; i < ans.length; i++)
ans[i] = Math.min(i > 0 ? ans[i - 1] : Infinity, cost[i]);
return ans;
};
```
```
✨ AN UPVOTE WILL BE APPRECIATED ^_~ ✨
``` | 11 | 0 | ['Math', 'Simulation', 'Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 0 |
minimum-cost-to-reach-every-position | C++ partial_sum 2 lines|beats 100% | c-partial_sum-2-linesbeats-100-by-anwend-l2tj | IntuitionUse prefix sum with operation minApproach
Use partial_sum with lambda function [](int x, int y){return x=min(x, y);}
return cost
Complexity
Time compl | anwendeng | NORMAL | 2025-03-31T05:21:44.114295+00:00 | 2025-03-31T05:21:44.114295+00:00 | 289 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Use prefix sum with operation min
# Approach
<!-- Describe your approach to solving the problem. -->
1. Use `partial_sum` with lambda function `[](int x, int y){return x=min(x, y);}`
2. return `cost`
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(n)$$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$$O(1)$$
# Code||0ms
```cpp []
class Solution {
public:
vector<int> minCosts(vector<int>& cost) {
partial_sum(cost.begin(), cost.end(), cost.begin(), [](int x, int y){return x=min(x, y);});
return cost;
}
};
``` | 8 | 0 | ['Prefix Sum', 'C++'] | 1 |
minimum-cost-to-reach-every-position | 100% Beats || Simple Code | 100-beats-simple-code-by-kdhakal-tw3d | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | kdhakal | NORMAL | 2025-04-09T13:20:37.968768+00:00 | 2025-04-09T13:20:37.968768+00:00 | 36 | 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
```java []
class Solution {
public int[] minCosts(int[] cost) {
int prev = cost[0];
for(int i = 1; i < cost.length; i++) {
if(cost[i] > prev) cost[i] = prev;
else prev = cost[i];
}
return cost;
}
}
``` | 3 | 0 | ['Java'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.