⭐ Introduction to Algorithms ⭐ Binary Enumeration Simple 14 - LeetCode LCP 28. Purchasing Scheme

Keywords: C++ Algorithm leetcode

1. Title

1. Title Description

_Xiaoli will n n Quotations for n parts are stored in an array n u m s nums nums. Small budget is t a r g e t target target, assuming Xiaoli purchases only two parts, requires that the cost of purchasing the parts do not exceed the budget, ask him how many purchasing options he has.
_Sample input: nums = [2,5,3,5], target = 6
_Sample output: 8

2. Infrastructure

  • The basic framework code given in the C language version is as follows:
int purchasePlans(int* nums, int numsSize, int target){}

3. Topic Link

LeetCode LCP 28. Purchase proposal

2. Problem-solving Report

1. Idea Analysis

_1) First, sort all purchasing options;
_2) Enumerate one of them i i i, marked as n o w = n u m s [ i ] now = nums[i] now=nums[i];
_3) The goal is to find n o w + x ≤ t a r g e t now + x \le target now+x < maximum of target x x x, so here we can make x ≤ t a r g e t − n o w x \le target - now x= target_now is the red part, x > t a r g e t − n o w x \gt target - now X>target_now is the green part;
_4) Find the right red boundary using the binary enumeration r r r, cumulative r − i r - i r−i;

2. Time Complexity

_Total time complexity is O ( n l o g 2 n ) O(nlog_2n) O(nlog2​n).

3. Code Details

/************** Binary Find Array Template************/
/*

  1)The array of parameters is satisfied: red, red, red, red, green, green, green; 
  2)Return value: left boundary of green section; 
*/

int isGreen(int val, int x);

int binarySearch(int *arr, int arrSize, int x) {
    int l = -1, r = arrSize;
    int mid;
    while(l + 1 < r) {
        mid = l + (r - l) / 2;
        if( isGreen(arr[mid], x) )
            r = mid;
        else
            l = mid;
    }
    return r;
}
/************** Binary Find Array Template************/

int isGreen(int val, int x) {
    return val > x;
}

#define mod 1000000007

int cmp(const void *a, const void *b) {
    return *((int *)a) - *((int *)b);
}
int purchasePlans(int* nums, int numsSize, int target){
    int i, base, size, green, red;
    int sum = 0;
    qsort(nums, numsSize, sizeof(int), cmp);              // (1)
    for(i = 0; i < numsSize - 1; ++i) {                   // (2)
        base = i+1;                                       
        size = numsSize - base; 
        green = binarySearch(nums + base, size, target - nums[i]); // (3)
        red = green - 1;                                 // (4)
        sum += red + 1;                                  // (5)
        if( sum >= mod ) sum -= mod;
    } 
    return sum;
}

  • ( 1 ) (1) (1) Sorting becomes ordered;
  • ( 2 ) (2) (2) Enumerate the small number;
  • ( 3 ) (3) (3) Adding an offset to the array search;
  • ( 4 ) (4) (4) Calculate the red boundary;
  • ( 5 ) (5) (5) The red boundary + 1 is the number of numbers that satisfy the condition;

3. Small knowledge of the subject

Linear enumeration with binary enumeration is a common method to solve problems.

4. Notice for Adding Groups

_I believe most of my articles are "college students" and "elites" who can go to university, so we naturally want to "lean". If you are still a freshman, that's great. You have plenty of time. Of course, you can choose "brush opera". However, after "learning algorithms", three years you will naturally "not be in the same language".
_Then here, I have sorted out the classification of "dozens of basic algorithms" and click on Open:

🌌 Guidelines to Getting Started with Algorithms 🌌
_If the link is blocked or you have permission issues, you can chat with the author privately to resolve them.

_Overview of the general problem set:





_To make this fun and to take care of beginners, the current title only opens the simplest algorithm, Enumeration Series (including linear enumeration, double pointer, prefix and, dichotomous enumeration, three-thirds enumeration). When half of the members have finished brushing all the questions of Enumeration Series, the next chapter will be opened, and so on, the set will be completely brushed. If you're still in the group, then you'll be a member of the Expert Group on Late Night Still Writing Algorithms.
_Don't underestimate this panel of experts. After three years, you will be a presence that others can't expect. If you want to join, you can contact me, considering that everyone is a student and that there is no "primary source of income", "you will not ask for anything" on your way to becoming God.
_ 🔥 Contact the author, or sweep the 2-D code of the author's home page into groups, so join the list of brushing titles 🔥

🔥 Let the world have no hard-to-learn algorithms 🔥
C language free animation tutorial, punch in with me! 🌞 Guangtianhua Japanese Language C 🌞
A Summary of C Language Topics at the Beginner Level 🧡 100 Cases of Introduction to C Language 🧡
Several kinematics learn a data structure 🌳 Understanding Data Structure 🌳
Group Learning, Group Growth 🌌 Guidelines to Getting Started with Algorithms 🌌
Competitor Golden Text Tutorial 💜 Late Night Still Writing Algorithm 💜

Posted by hakmir on Mon, 18 Oct 2021 09:01:36 -0700