[Swift] LeetCode1167 lowest cost connection bars |. Minimum Cost to Connect Sticks

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: to dare (WeiGanTechnologies)
➤ blog Park address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: HTTPS: //www.cnblogs. com / strengthen / p / 11407050.html 
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

You have some sticks with positive integer lengths.

You can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y.  You perform this action until there is one stick remaining.

Return the minimum cost of connecting all the given sticks into one stick in this way.

 

Example 1:

Input: sticks = [2,4,3]
Output: 14

Example 2:

Input: sticks = [1,8,3,5]
Output: 30

 

Constraints:

  • 1 <= sticks.length <= 10^4
  • 1 <= sticks[i] <= 10^4

To decorate the house, you need to process some of the length of the rod is a positive integer  sticks.

If you want length, respectively  X , and  Y the two bars together, you need to pay the  X + Y costs. Due to construction needs, you have to connect all the bars into one.

You return all the bars  sticks lowest rates even as a need. Note that you can choose the order of the bar connection.

 

Example 1:

Input: sticks = [2,4,3] 
Output: 14 
Explanation: 2 and 3 are connected to the first 5, it takes 5; and then 5 9 4 connected; total cost of 14.

Example 2:

Input: sticks = [1,8,3,5] 
Output: 30

 

prompt:

  • 1 <= sticks.length <= 10^4
  • 1 <= sticks[i] <= 10^4

Guess you like

Origin www.cnblogs.com/strengthen/p/11407050.html