Skip to content

Commit b35ac90

Browse files
committed
[LeetCode Sync] Runtime - 424 ms (91.67%), Memory - 17.8 MB (95.36%)
1 parent dac3e44 commit b35ac90

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, where <code>nums[i]</code> is a digit between <code>0</code> and <code>9</code> (<strong>inclusive</strong>).</p>
2+
3+
<p>The <strong>triangular sum</strong> of <code>nums</code> is the value of the only element present in <code>nums</code> after the following process terminates:</p>
4+
5+
<ol>
6+
<li>Let <code>nums</code> comprise of <code>n</code> elements. If <code>n == 1</code>, <strong>end</strong> the process. Otherwise, <strong>create</strong> a new <strong>0-indexed</strong> integer array <code>newNums</code> of length <code>n - 1</code>.</li>
7+
<li>For each index <code>i</code>, where <code>0 &lt;= i &lt;&nbsp;n - 1</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>(nums[i] + nums[i+1]) % 10</code>, where <code>%</code> denotes modulo operator.</li>
8+
<li><strong>Replace</strong> the array <code>nums</code> with <code>newNums</code>.</li>
9+
<li><strong>Repeat</strong> the entire process starting from step 1.</li>
10+
</ol>
11+
12+
<p>Return <em>the triangular sum of</em> <code>nums</code>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png" style="width: 250px; height: 250px;" />
17+
<pre>
18+
<strong>Input:</strong> nums = [1,2,3,4,5]
19+
<strong>Output:</strong> 8
20+
<strong>Explanation:</strong>
21+
The above diagram depicts the process from which we obtain the triangular sum of the array.</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [5]
27+
<strong>Output:</strong> 5
28+
<strong>Explanation:</strong>
29+
Since there is only one element in nums, the triangular sum is the value of that element itself.</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
36+
<li><code>0 &lt;= nums[i] &lt;= 9</code></li>
37+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def triangularSum(self, nums: List[int]) -> int:
3+
# n = len(nums)
4+
# sums = [[0] * (n - i) for i in range(n)]
5+
6+
# for j in range(n):
7+
# sums[0][j] = nums[j]
8+
9+
# for i in range(1, n):
10+
# for j in range(n - i):
11+
# sums[i][j] = sums[i - 1][j] + sums[i - 1][j + 1]
12+
13+
# return sums[-1][-1] % 10
14+
15+
result = 0
16+
n = len(nums)
17+
for i, num in enumerate(nums):
18+
result += num * comb(n - 1, i)
19+
20+
return result % 10

0 commit comments

Comments
 (0)