{"id":5998,"date":"2019-12-28T09:46:35","date_gmt":"2019-12-28T17:46:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5998"},"modified":"2019-12-29T01:53:52","modified_gmt":"2019-12-29T09:53:52","slug":"leetcode-1299-replace-elements-with-greatest-element-on-right-side","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1299-replace-elements-with-greatest-element-on-right-side\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1299. Replace Elements with Greatest Element on Right Side"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>arr<\/code>,&nbsp;replace every element in that array with the greatest element among the elements to its&nbsp;right, and replace the last element with&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p>After doing so, return the array.<\/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> arr = [17,18,5,4,6,1]\n<strong>Output:<\/strong> [18,6,6,6,1,-1]<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^4<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Scan from right to left<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> replaceElements(vector<int>& arr) {\n    int g = -1;\n    for (int i = arr.size() - 1; i >= 0; --i)      \n      g = max(g, exchange(arr[i], g));    \n    return arr;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;arr,&nbsp;replace every element in that array with the greatest element among the elements to its&nbsp;right, and replace the last element with&nbsp;-1. After doing&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,376],"class_list":["post-5998","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5998","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=5998"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5998\/revisions"}],"predecessor-version":[{"id":6009,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5998\/revisions\/6009"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}