Sword offer-Fifth chapter optimization of Time and space efficiency (total number of reversed pairs in an array)

Source: Internet
Author: User

Title: In an array if the preceding number of two digits is larger than the number that follows, it is called a pair of inverse pairs. Enter an array to find the total number of reverse pairs in the group.

Change Time by Space:
Idea: With an auxiliary array, copy the original array into the array. It then divides the array into sub-arrays, then counts the internal inverse of the subarray, and then counts the inverse pairs in the two connected sub-arrays, which uses the merge sort. The time complexity is O (NLOGN).

As shown in the following:

Sword offer-Fifth chapter optimization of Time and space efficiency (total number of reversed pairs in an array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.