2

And several friends discussed the question has always insisted brush brush for a moment the question of epiphany time, they made the most of a few good offer, such as Google, Microsoft, Amazon, BAT and so on.

Through communication with them, I found that everyone epiphany moments are similar. What are their similarities? Let's take a look together.

1. The same type has to be swiped a lot to get an epiphany

For example, if you want realize two points, then you need to do enough two points first.

And because the dichotomy is actually a big category. Therefore, in theory, if you want to have an epiphany on the dichotomous category, it is essential that first do enough dichotomous questions, and these questions can cover all the dichotomous types .

For example, the basic dichotomy summarized by Western Law, the leftmost dichotomy, and the ability test dichotomy. Most of the difficult topics are the ability test dichotomy.

For those who are not familiar with dichotomy, please refer to the "Dichotomy Topics" summarized by Western France:

By extension then, if you want to brush the whole algorithm problem epiphany , then we have to first do enough problems, and these problems can cover all test sites you want insight .

That is to say, the reason why the big guys you read have written thousands of questions. Because there is no accumulation of thousands of questions, it is difficult for you to have epiphany for all question types. Of course, if you just deal with most of the test sites and do not participate in the competition, maybe a few hundred is ok.

2. Review the topics you have done

Some students are more direct, they just review the topics they have done directly. Some students recalled some of the questions they had done before by doing new topics, so as to achieve the role of review.

No matter what type. They all have to go through a stage, that is, and the problem that has been done to establish a connection . If you just do the questions blindly, the efficiency will definitely not improve.

When I first started writing questions, I would create some anki cards. In fact, this is to mandatory review done subject . In addition, when doing new topics, I force myself to think:

  • What knowledge does this question examine?
  • Which questions can you establish a connection with before?
  • Is it possible to use the solution set of the previous problem?
  • What are the corner cases?
  • 。。。

After these reflections, and slowly they will have done the subject organically combined , instead of letting these problems become information silos each other.

3. Abstract the topics that have been done

This is the last point I want to talk about, but this point is particularly important. It is not too much to say that it is the most important.

On the one hand, if a topic is not abstracted, it will be difficult for us to remember and recall in the future. On the other hand, if a topic can be abstracted into a pure topic, then it means that you have a more thorough view of the topic. In the future, when you encounter a skin-changing question, once you abstract, you will find: isn't this just the skin-changing question of xxxx before?

Classmates who often read my questions and articles know that I have written a lot of skin-changing questions before. This is my style of writing questions and writing articles.

Here, I will give another example.

Note: The following three examples of questions all require you to master the dichotomy of . If you don’t understand, I suggest you read my article above.

Shopee's snack cabinet

This is shopee's school recruitment programming question.

Title description

shopee的零食柜,有着各式各样的零食,但是因为贪吃,小虾同学体重日益增加,终于被人叫为小胖了,他终于下定决心减肥了,他决定每天晚上去操场跑两圈,但是跑步太累人了,他想转移注意力,忘记痛苦,正在听着音乐的他,突然有个想法,他想跟着音乐的节奏来跑步,音乐有7种音符,对应的是1到7,那么他对应的步长就可以是1-7分米,这样的话他就可以转移注意力了,但是他想保持自己跑步的速度,在规定时间m分钟跑完。为了避免被累死,他需要规划他每分钟需要跑过的音符,这些音符的步长总和要尽量小。下面是小虾同学听的歌曲的音符,以及规定的时间,你能告诉他每分钟他应该跑多少步长?



输入描述:
输入的第一行输入 n(1 ≤ n ≤ 1000000,表示音符数),m(1<=m< 1000000, m <= n)组成,

第二行有 n 个数,表示每个音符(1<= f <= 7)


输出描述:
输出每分钟应该跑的步长
示例1
输入
8 5 6 5 6 7 6 6 3 1
输出
11

Link: https://www.nowcoder.com/questionTerminal/24a1bb82b3784f86babec24e4a5c93e0?answerType=1&f=discussion
Source: Niuke.com

Ideas

After abstraction, this question essentially means that gives you an array (the array value range is an integer from 1 to 7), and lets you divide the array into at most m sub-arrays, and find the minimum m sub-arrays 1616f9b5ba295f.

It is difficult to directly answer the sub-array and minimum value, but it is relatively easy to answer whether a specific value can be reached.

For example, it is relatively easy to answer whether the sub-array and the minimum value is 100. Because we only need to traverse the array once, if the contiguous sub-array is greater than 100, we will split a new block, so that the number of blocks finally split is less than or equal to m, which means 100 is OK.

Another key point is that this detection is monotonic. For example, 100 is ok, then any number greater than 100 (such as 101) is definitely ok. If you have read my "Dichotomous Topics" above or have done a lot of ability test dichotomies, it is not difficult to think that you can use this monotonicity to do ability test dichotomies to get the answer. And we need to find the smallest number that satisfies the condition, so we can apply the leftmost ability test to get the answer.

Code

Don't write it for now, because this question is the same as the next one.

410. Split the maximum value of the array

Title description

给定一个非负整数数组 nums 和一个整数 m ,你需要将这个数组分成 m 个非空的连续子数组。

设计一个算法使得这 m 个子数组各自和的最大值最小。

 

示例 1:

输入:nums = [7,2,5,10,8], m = 2
输出:18
解释:
一共有四种方法将 nums 分割为 2 个子数组。 其中最好的方式是将其分为 [7,2,5] 和 [10,8] 。
因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。
示例 2:

输入:nums = [1,2,3,4,5], m = 2
输出:9
示例 3:

输入:nums = [1,4,4], m = 3
输出:4
 

提示:

1 <= nums.length <= 1000
0 <= nums[i] <= 106
1 <= m <= min(50, nums.length)

Source: LeetCode
Link: https://leetcode-cn.com/problems/split-array-largest-sum

Ideas

The official difficulty of this question is hard. It's exactly the same as the abstract in the previous question, so I don't need to explain it more, right?

Do you think that after such abstraction, do you have a sense of epiphany of the same goal by different paths?

Code

Code support: Python3

Python3 Code:


class Solution:
    def splitArray(self, nums: List[int], m: int) -> int:
        lo, hi = max(nums), sum(nums)
        def test(mid):
            cnt = acc = 0
            for num in nums:
                if acc + num > mid:
                    cnt += 1
                    acc = num
                else:
                    acc += num
            return cnt + 1 <= m

        while lo <= hi:
            mid = (lo + hi) // 2
            if test(mid):
                hi = mid - 1
            else:
                lo = mid + 1
        return lo

Do you think this is over? There are simply not too many similar topics. Xifa will give you another example.

LCP 12. Xiao Zhang's Problem Rewriting Plan

Title description

为了提高自己的代码能力,小张制定了 LeetCode 刷题计划,他选中了 LeetCode 题库中的 n 道题,编号从 0 到 n-1,并计划在 m 天内按照题目编号顺序刷完所有的题目(注意,小张不能用多天完成同一题)。

在小张刷题计划中,小张需要用 time[i] 的时间完成编号 i 的题目。此外,小张还可以使用场外求助功能,通过询问他的好朋友小杨题目的解法,可以省去该题的做题时间。为了防止“小张刷题计划”变成“小杨刷题计划”,小张每天最多使用一次求助。

我们定义 m 天中做题时间最多的一天耗时为 T(小杨完成的题目不计入做题总时间)。请你帮小张求出最小的 T是多少。

示例 1:

输入:time = [1,2,3,3], m = 2

输出:3

解释:第一天小张完成前三题,其中第三题找小杨帮忙;第二天完成第四题,并且找小杨帮忙。这样做题时间最多的一天花费了 3 的时间,并且这个值是最小的。

示例 2:

输入:time = [999,999,999], m = 4

输出:0

解释:在前三天中,小张每天求助小杨一次,这样他可以在三天内完成所有的题目并不花任何时间。

 

限制:

1 <= time.length <= 10^5
1 <= time[i] <= 10000
1 <= m <= 1000

Source: LeetCode
Link: https://leetcode-cn.com/problems/xiao-zhang-shua-ti-ji-hua

Ideas

Similar to the previous topic. After abstraction, this question is essentially that gives you an array (the array value range is an integer from 1 to 10000), and lets you divide the array into up to m sub-arrays, and each sub-array can delete up to one number. Find m sub-arrays The minimum value of the .

The only difference from the above question is that this question allows us to delete a number from the sub-array. Obviously, we should greedily delete the largest number in the sub-array.

Therefore, my idea is to detect the maximum value of the sub-array in the ability detection part, and add a judgment during each traversal process: if deletes the maximum value of the sub-array, then can satisfy the sub-array and be less than the detection value (that is, mid).

Code

Code support: Python3

Python3 Code:

class Solution:
    def minTime(self, time: List[int], m: int) -> int:
        def can(mid):
            k = 1 # 需要多少天
            t = 0 # 当前块的总时间
            max_time = time[0]
            for a in time[1:]:
                if t + min(max_time, a) > mid:
                    t = 0
                    k += 1
                    max_time = a
                else:
                    t += min(max_time, a)
                    max_time = max(max_time, a)
            return k <= m

        l, r = 0, sum(time)

        while l <= r:
            mid = (l+r)//2
            if can(mid):
                r = mid - 1
            else:
                l = mid + 1
        return l

In terms of time complexity, the three questions are the same, let's analyze it.

We know that the time complexity analysis depends on the code with the most execution times. Obviously, this question is the code in the capability detection function. Since we need to traverse the array once in the ability detection part, the time is $O(n)$, and the number of executions of the ability detection function is $logm$. Therefore, the time complexity is all $nlogm$, where n is the length of the array and m is the sum of the array.

Summarize

Enlightenment is really a very wonderful feeling. I found through interviews with several big guys that everyone’s experience of enlightenment is similar, that is:

  1. The same type has to be swiped a lot to get an epiphany
  2. Review the topics that have been done
  3. Abstract the topics that have been done

I gave you a detailed explanation on the third point of Western Fa through three questions. I hope you can master the rhythm when doing the questions and draw inferences from one another. Finally, I wish you all happy writing questions and lots of offers.


lucifer
5.3k 声望4.6k 粉丝