Get into the habit of writing together! This is the 12th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
【Brush questions diary】1672. The total assets of the richest customers
The 32nd chapter of this diary is titled: 1672. The total assets of the richest customer , simple
1. Topic description:
Today is another day of working overtime. I still have to keep the habit when I come back. At first glance, leetcode is a simple question. I thought, is there a hole in it?
No matter what the question is, there will always be one's own way and technique . After working for a few years, I slowly discovered that many engineers or managers have a fighting spirit with skills but no way. Is the technique not summed up and refined ?
When we brushed the questions, I used to follow the topic, for example, starting from strings, then arrays, linked lists, other data structures, search, sorting, binary trees, graph theory, etc.
2. What idea does this question examine? What is your thinking?
Today's question continues to see what important and useful information the question has brought us?
- Although the title is bells and whistles, let's look at the essence directly. In this question, let's not pay attention to the total assets of the richest customers. Let's pay attention directly to the sum of each one-dimensional array in the two-dimensional array, and compare each one The sum of one-dimensional arrays , we can get the result of the largest sum of numbers
I don't know xdm, did you feel that when you read this question, if you don't look at the example, you seem to be sure that it is so simple and direct. It seems that after the example, there is really no twists and turns, you can just traverse the sum directly.
The idea is very simple, we also have to land on the coding, the real work is hard work, and those who play tricks are hooligans
3. Coding
According to the above logic and analysis, we can translate it into the following code. Note that when we traverse the two-dimensional array, we also need to traverse the one-dimensional array inside.
The encoding is as follows:
func maximumWealth(accounts [][]int) int {
// 定义一个变量,存储结果,也用于过程中的比较
var res int
for _, account := range accounts {
sum := 0
// 计算每一个 一维数组的 和
for _, val := range account {
sum += val
}
// 比较每一个一维数组的结果
if sum > res {
res = sum
}
}
return res
}
复制代码
The thinking is clear, the coding is also very clear, the process will not be repeated, just look at the code and comments
4. Summary:
The time complexity of this problem is also very clear, not O(n) nor O(m), but O(nm), n is the number of outer loops, m is the number of memory loops
Space complexity, we introduce constant-level space consumption, so it is O(1)
Original title address: 1672. Total assets of the wealthiest customers
I am here today, what I have learned, if there are any deviations, please correct me
Welcome to like, follow, favorite
Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality
Okay, here it is this time
Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.
I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~