ARTS Week 30

Algorithm

This week’s LeetCode problem is 162. Find Peak Element

A peak element is an element that is strictly greater than its neighbors.

Given an integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

You may imagine that nums[-1] = nums[n] = -∞.

You must write an algorithm that runs in O(log n) time.

Because the problem requires that the algorithm complexity is O(log n), then it cannot be traversed, and a binary search is required. In order to avoid peak elements on the left and right boundaries, add a negative infinity to the left and right boundaries of the original array. Next, perform a binary search to determine whether the mid meets the requirements of the question. If so, update the result. Next, search for the qualified numbers in the range of left ~ mid and mid ~ right respectively. If the number found is greater than the result found before , then update the result.

Review

This week’s Review is for the following article: Everything Must Be Paid for Twice

The author’s point may sound strange at first, we pay once for everything, why pay twice? For example, if you spend 20 yuan to buy a book, but when you read it, it may take 10 hours to read it, which is the second price; for example, mobile phones, furniture, etc., when you spend money to buy them, It also takes time to learn to use them so they can work to their effect, and these are second prices.

The author believes this is one of the reasons why we sometimes feel self-delusional in our modern life. We are constantly paying the first price and incurring huge second-price debts accordingly, but buying anything to get a return requires two All prices are paid. Among the second-price debts, cell phone apps, streaming services, and processed foods, etc., they require very little effort to enjoy them, so it’s easy to get addicted to them, but it doesn’t help us grow.

The only solution that comes to the author’s mind is to avoid paying unnecessarily the first price, so you don’t add debt to the second price, and you’ll have time to enjoy a good book, learn a musical instrument, etc.

It’s not difficult to figure out what a second price is, the important thing is that you can stick with it and pay the second price, and slowly, the rewards will come at unfamiliar times.

Tip

In C language, sizeof is calculated at compile time, so for pointer p, sizeof(p) gets the size of the pointer, and sizeof(*p) gets the type of the pointer the size of. An example is as follows:

The result of execution is:

Share

I stayed in the dormitory for a week in isolation (it is estimated that I will stay in the dormitory for a while in the future). At first, I was not quite used to the life in the dormitory. I couldn’t find the status, and the efficiency was relatively low. In the future, it is necessary to gradually adapt to the life in the dormitory and get back to the original state.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KeepNewbie_yan

KeepNewbie_yan

5 Followers

A programmer. Share knowledge of programming, operating system, Linux kernel, and reading, thinking etc. Let us maintain a beginner mend and grow together!