Hyoseo Lee
01 Index 02 Current page Writing 03 Work 04 Notes 05 Games 06 Author
leetcode

2221. Find Triangular Sum of an Array

Topic Math
Area Algorithms
Summary
i used recursion, if the array length is 1, it returns the number. and if else, we do the calculation and reduce the size of array by one.

Problem

View on LeetCode →

Difficulty: Medium
Tags: Array, Math, Simulation, Combinatorics, Number Theory

Intuition

the intuition to this problem was too easy. the algorithm was written on the problem, so it was really easy. I could solve it without recursion but I thought this way is much better.

Approach

i used recursion, if the array length is 1, it returns the number. and if else, we do the calculation and reduce the size of array by one.

Solution

class Solution:
    def triangularSum(self, nums: List[int]) -> int:
        if(len(nums) == 1):
            return nums[0]
        else:
            newNum = []
            for i in range(len(nums)-1):
                newNum.append((nums[i] + nums[i+1]) % 10)
            return self.triangularSum(newNum)

Complexity

  • Time: O(n2)O(n^2)

  • Space: O(n2)O(n^2)

Thoughts

using python really make me focus more on the problem, and it made me solve medium question really quickly.