In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to split the score group in LeetCode. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Given an array of length 2n, your task is to divide these numbers into n pairs, such as (A1, b1), (a2, b2),..., (an, bn), so that the sum of min (ai, bi) from 1 to n is maximum.
Example:
Input: [1, 4, 3, 2]
Output: 4
Explanation: n equals 2, and the maximum sum is 4 = min (1,2) + min (3,4).
Do you feel confused when you get this question? you may be wondering, what kind of algorithm do I have to use to find the group, the maximum sum of the minimum values of each group? You can think about it first.
If you haven't come up with a good solution, I can give you some hints.
1. If you want to use brute force to solve, it must be useless, as I said above, how do you know which combinations will do? So you need to think about it from a different point of view. For example, you can assume that the array is [1, 2, 3, 4, 5, 6].
After reading this hint, I wonder if you have any ideas? If not, I'll give you another hint.
two。 How do you know which combinations are better? So the array has to be made into some form that is easy to view.
At this point, you probably already feel a little bit, but you don't seem to know how to turn the array into some kind of so-called form. Then I'll give you another hint.
3. If you get the min of two values, you are sure to lose the larger one, so you need to find out the smaller one and the larger one.
At this point, I believe you should know what to do. What? You don't know? All right, let me be clear with you.
4. First sort the array, and after sorting, you can take the sum directly from each other.
At this point, you should be able to write the implementation code. Here is an example of the Java code I gave you:
Class Solution {
Public int arrayPairSum (int [] nums) {
Arrays.sort (nums)
Int result = 0
For (int I = 0; I < nums.length; I + = 2) {
Result + = nums [I]
}
Return result
}
}
About how to split the score group in LeetCode to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.