{"id":7616,"date":"2020-11-07T21:37:35","date_gmt":"2020-11-08T05:37:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7616"},"modified":"2020-11-07T21:38:29","modified_gmt":"2020-11-08T05:38:29","slug":"1646-get-maximum-in-generated-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/1646-get-maximum-in-generated-array\/","title":{"rendered":"\u82b1\u82b1\u9171 1646. Get Maximum in Generated Array"},"content":{"rendered":"\n<p>You are given an integer&nbsp;<code>n<\/code>. An array&nbsp;<code>nums<\/code>&nbsp;of length&nbsp;<code>n + 1<\/code>&nbsp;is generated in the following way:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>nums[0] = 0<\/code><\/li><li><code>nums[1] = 1<\/code><\/li><li><code>nums[2 * i] = nums[i]<\/code>&nbsp;when&nbsp;<code>2 &lt;= 2 * i &lt;= n<\/code><\/li><li><code>nums[2 * i + 1] = nums[i] + nums[i + 1]<\/code>&nbsp;when&nbsp;<code>2 &lt;= 2 * i + 1 &lt;= n<\/code><\/li><\/ul>\n\n\n\n<p>Return<em>the&nbsp;<strong>maximum<\/strong>&nbsp;integer in the array&nbsp;<\/em><code>nums<\/code>\u200b\u200b\u200b.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 7\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> According to the given rules:\n  nums[0] = 0\n  nums[1] = 1\n  nums[(1 * 2) = 2] = nums[1] = 1\n  nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2\n  nums[(2 * 2) = 4] = nums[2] = 1\n  nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3\n  nums[(3 * 2) = 6] = nums[3] = 2\n  nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3\nHence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> According to the given rules, the maximum between nums[0], nums[1], and nums[2] is 1.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> According to the given rules, the maximum between nums[0], nums[1], nums[2], and nums[3] is 2.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= n &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Generate the array by the given rules.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int getMaximumGenerated(int n) {\n    vector<int> nums(n + 1);\n    nums[0] = 0;\n    if (n > 0) nums[1] = 1;\n    for (int i = 1; i * 2 <= n; ++i) {\n      nums[2 * i] = nums[i];\n      if (i * 2 + 1 <= n) nums[2 * i + 1] = nums[i] + nums[i + 1];\n    }\n    return *max_element(begin(nums), end(nums));\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer&nbsp;n. An array&nbsp;nums&nbsp;of length&nbsp;n + 1&nbsp;is generated in the following way: nums[0] = 0 nums[1] = 1 nums[2 * i] =&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[20,222,179],"class_list":["post-7616","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-array","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7616"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7616\/revisions"}],"predecessor-version":[{"id":7618,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7616\/revisions\/7618"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}