It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.
Apparently, many engineers take measures on the site called LetCode.
It is a site that trains the algorithmic power that can withstand the coding test that is being done in the early story, and it is an inevitable path for those who want to build a career at an overseas tech company.
I wrote it in a big way, but I have no plans to have such an interview at the moment.
However, as an IT engineer, it would be better to have the same level of algorithm power as a person, so I would like to solve the problem irregularly and write down the method I thought at that time as a memo.
I'm solving it with Python3.
Leet Code Table of Contents Starting from Zero
Last time Leet Code Day 55 "Starting from Zero" 22. Generate Parentheses "
Right now, I'm prioritizing the Medium of the Top 100 Liked Questions. I solved all Easy, so if you are interested, please go to the table of contents.
Twitter I'm doing it.
The difficulty level is Easy.
The problem is given the array nums
. The problem is that you should return the sum of all the numbers in a list.
Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. Example 2:
Input: nums = [1,1,1,1,1] Output: [1,2,3,4,5] Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]. Example 3:
Input: nums = [3,1,2,10,1] Output: [3,4,6,16,17]
Constraints:
1 <= nums.length <= 1000 -10^6 <= nums[i] <= 10^6
It's a so-called cumulative sum problem. This problem itself is easy to understand, so I think it is important how to implement it.
Regarding the implementation, I think that the way of writing will change depending on whether you are familiar with Python functions.
For example, if you know itertools.accumulate, you can write:
class Solution:
def runningSum(self, nums: List[int]) -> List[int]:
return itertools.accumulate(nums)
# Runtime: 60 ms, faster than 33.33% of Python3 online submissions for Running Sum of 1d Array.
# Memory Usage: 14.1 MB, less than 33.33% of Python3 online submissions for Running Sum of 1d Array.
You can write it very concisely, so if you know it, or if you can freely look up documents, you can use this.
However, you also have to keep in mind how to write if you forget it and think from scratch.
Kencho's way of thinking about cumulative sum
Be able to write cumulative sums without thinking!
I think it's very easy to read and understand. As I read and learned, the cumulative sum has a wide range of applications even in AtCoader etc ...
class Solution:
def runningSum(self, nums: List[int]) -> List[int]:
i = 1
while i<len(nums):
nums[i] += nums[i-1]
i += 1
return nums
# Runtime: 44 ms, faster than 33.33% of Python3 online submissions for Running Sum of 1d Array.
# Memory Usage: 13.8 MB, less than 100.00% of Python3 online submissions for Running Sum of 1d Array.
This time I wrote it like this.
It's a basic part, so I want to be able to kill it instantly.
Up to here for this time. Thank you for your hard work.
Recommended Posts