Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of advantage shuffling algorithm by JavaScript

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to solve the advantage shuffle algorithm problem with JavaScript". In the operation of the actual case, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Topic description:

Given two equal-sized arrays An and B, the advantages of An and B over B can be described by the number of indexes that satisfy A [I] > B [I]. Return to any arrangement of A to maximize its advantage over B.

Example 1:

Input: a = [2, 7, 7, 11, 15], B = [1, 10, 10, 4, 11]

Output: [2, 11, 7, 15]

Example 2

Input: a = [122.24pr 8je 32], B = [13pr 25pr 32pr 11]

Output: [24pr 32pr 8pr 12]

We use example 2 to show that the two arrays An and B are of the same length, arrange the An array and compare it with the B array one by one, A [I] > B [I] to maximize the score.

It looks simple, but how do you think about it? Where do we start?

We can first sort the An array, sort from small to large, after sorting is completed, cycle through the B array, use each item in the B array to find data that is just a little larger than this item in the An array, and find it. Put it into the corresponding position, if the search can not extract the smallest from the sorted An array to the current position.

The principle is similar to Tianji horse racing, using the smallest data in the An array to hedge the larger data in the B array, and the data selection in the remaining An array is slightly larger than that in the B array. The code is as follows:

Function comp (arr1, arr2) {

Var arr = []

Arr1.sort (function (a, b) {

Return a-b

});

Arr2.forEach (function (item) {

Var index = arr1.findIndex (function (_ item) {

Return _ item > item

});

If (index >-1) {

Arr.push (arr1.splice (index, 1) [0])

} else {

Arr.push (arr1.splice (0,1) [0])

}

});

Return arr

} "JavaScript how to solve the advantage shuffle algorithm problem" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report