Published: Sep 10, 2022
Introduction
This is a math problem. The brute force solution barely passes all tests, however, it is very slow. To fins an optimal solution, some mathematical insight is required.
Problem Description
You are given a 0-indexed integer array
nums,wherenums[i]is a digit between 0 and 9 (inclusive). The triangular sum ofnumsis the value of the only element present in nums after the following process terminates:
- Let nums comprise of
nelements. Ifn == 1, end the process. Otherwise, create a new 0-indexed integer arraynewNumsof lengthn - 1.- For each index
i, where0 <= i < n - 1, assign the value ofnewNums[i]as(nums[i] + nums[i+1]) % 10, where%denotes modulo operator.- Replace the array
numswithnewNums.- Repeat the entire process starting from step 1. Return the triangular sum of
nums.Constraints:
1 <= nums.length <= 10000 <= nums[i] <= 9https://leetcode.com/problems/find-triangular-sum-of-an-array/
Examples
Example 1:
Input: nums = [1,2,3,4,5]
Output: 8
1, 2, 3, 4, 5
3, 5, 7, 9
8, 2, 6
0, 8,
8
Example 2:
Input: nums = [5]
Output: 5
Analysis
Here, the mathematical, optimal solution and brute force solutions are.
The mathematical solution is from combination idea:
each number is multiplied by the number of routes to the top.
In the solution, this number is “cnk”.
Additionally, the calculation uses: (a % 10 + b % 10) % 10 = (a + b) % 10.
Solution
class FindTriangularSumOfAnArrayMath:
def triangularSum(self, nums: List[int]) -> int:
n = len(nums)-1
total = 0
cnk = 1
for i in range(len(nums)):
total += nums[i] * cnk
total %= 10
cnk = cnk * (n-i) // (i+1)
return total
class FindTriangularSumOfAnArrayBF:
def triangularSum(self, nums: List[int]) -> int:
n, prev = len(nums), nums
for _ in range(n - 1):
cur = []
for i in range(len(prev) - 1):
cur.append((prev[i] + prev[i + 1]) % 10)
prev = cur
return prev[0]
Complexities
- Time:
O(n)– math solution,O(n^2)– brute force solution - Space:
O(1)– math solution,O(n)– brute force solution