{"id":9605,"date":"2022-04-02T14:46:58","date_gmt":"2022-04-02T21:46:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9605"},"modified":"2022-04-02T15:17:51","modified_gmt":"2022-04-02T22:17:51","slug":"leetcode-2221-find-triangular-sum-of-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2221-find-triangular-sum-of-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2221. Find Triangular Sum of an Array"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>, where&nbsp;<code>nums[i]<\/code>&nbsp;is a digit between&nbsp;<code>0<\/code>&nbsp;and&nbsp;<code>9<\/code>&nbsp;(<strong>inclusive<\/strong>).<\/p>\n\n\n\n<p>The&nbsp;<strong>triangular sum<\/strong>&nbsp;of&nbsp;<code>nums<\/code>&nbsp;is the value of the only element present in&nbsp;<code>nums<\/code>&nbsp;after the following process terminates:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Let&nbsp;<code>nums<\/code>&nbsp;comprise of&nbsp;<code>n<\/code>&nbsp;elements. If&nbsp;<code>n == 1<\/code>,&nbsp;<strong>end<\/strong>&nbsp;the process. Otherwise,&nbsp;<strong>create<\/strong>&nbsp;a new&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>newNums<\/code>&nbsp;of length&nbsp;<code>n - 1<\/code>.<\/li><li>For each index&nbsp;<code>i<\/code>, where&nbsp;<code>0 &lt;= i &lt;&nbsp;n - 1<\/code>,&nbsp;<strong>assign<\/strong>&nbsp;the value of&nbsp;<code>newNums[i]<\/code>&nbsp;as&nbsp;<code>(nums[i] + nums[i+1]) % 10<\/code>, where&nbsp;<code>%<\/code>&nbsp;denotes modulo operator.<\/li><li><strong>Replace<\/strong>&nbsp;the array&nbsp;<code>nums<\/code>&nbsp;with&nbsp;<code>newNums<\/code>.<\/li><li><strong>Repeat<\/strong>&nbsp;the entire process starting from step 1.<\/li><\/ol>\n\n\n\n<p>Return&nbsp;<em>the triangular sum of<\/em>&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/02\/22\/ex1drawio.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5]\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong>\nThe above diagram depicts the process from which we obtain the triangular sum of the array.<\/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> nums = [5]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong>\nSince there is only one element in nums, the triangular sum is the value of that element itself.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 9<\/code><\/li><\/ul>\n\n\n\n<p>Solution 1: Simulation<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int triangularSum(vector<int>& nums) {\n    while (nums.size() != 1) {\n      vector<int> next(nums.size() - 1);\n      for (size_t i = 0; i < nums.size() - 1; ++i)\n        next[i] = (nums[i] + nums[i + 1]) % 10;\n      swap(next, nums);\n    }\n    return nums[0];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums, where&nbsp;nums[i]&nbsp;is a digit between&nbsp;0&nbsp;and&nbsp;9&nbsp;(inclusive). The&nbsp;triangular sum&nbsp;of&nbsp;nums&nbsp;is the value of the only element present in&nbsp;nums&nbsp;after the following process terminates: Let&nbsp;nums&nbsp;comprise of&nbsp;n&nbsp;elements.&#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,177,179],"class_list":["post-9605","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-array","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9605","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=9605"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9605\/revisions"}],"predecessor-version":[{"id":9607,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9605\/revisions\/9607"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}