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 76 starting from zero "3. Longest Substring Without Repeating Characters" A
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.
** Technical Blog Started! !! ** ** I think the technology will write about LetCode, Django, Nuxt, and so on. ** This is faster to update **, so please bookmark it!
1502. Can Make Arithmetic Progression From Sequence The difficulty level is Easy.
This is a new issue that was recently added. It's not an easy problem, so please try to solve it.
The problem is that given an array of numbers ʻarr`, if the difference between two consecutive elements is the same, the sequence is called arithmetic progression.
Return true
if the array can be reorganized to form an arithmetic progression, otherwise return false
.
Example 1:
Input: arr = [3,5,1] Output: true Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
Example 2:
Input: arr = [1,2,4] Output: false Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
class Solution:
def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
arr.sort()
length = len(arr)
if length==2:
return True
difference = arr[1] - arr[0]
for i in range(1, length):
if arr[i] - arr[i - 1] != difference:
return False
return True
# Runtime: 32 ms, faster than 100.00% of Python3 online submissions for Can Make Arithmetic Progression From Sequence.
# Memory Usage: 13.9 MB, less than 100.00% of Python3 online submissions for Can Make Arithmetic Progression From Sequence.
Since it is a new question and easy, I was able to answer both 100%.
Sort the array first.
Exclude only when the length of the array is 2, otherwise difference
which takes the difference in value and False
when the contents when it is turned in each for statement are different, otherwise Returns True
.
Up to here for this time. Thank you for your hard work.
Recommended Posts