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 Day56 starting from zero "5453. Running Sum of 1d Array"
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.
I wrote an article because I thought it was a good problem to learn a certain algorithm.
The problem is given a sorted array and the target values. If the target is found, its index is returned. If not found, design an algorithm that returns the index if it was inserted in order.
You can assume that there are no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5 Output: 2 Example 2:
Input: [1,3,5,6], 2 Output: 1 Example 3:
Input: [1,3,5,6], 7 Output: 4 Example 4:
Input: [1,3,5,6], 0 Output: 0
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
low = 0
high = len(nums) -1
while low <= high:
mid = (low + high) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return low
# Runtime: 56 ms, faster than 49.71% of Python3 online submissions for Search Insert Position.
# Memory Usage: 14.6 MB, less than 16.72% of Python3 online submissions for Search Insert Position.
As anyone who knows it will know, it's a binary search problem!
In Python, there is a bisect
in the library, so you can easily write using it, but I think that the ability to know the principles is important, so I wrote it.
It might be helpful somewhere if you know it ...?
Up to here for this time. Thank you for your hard work.
Recommended Posts