176 Race Course Week

Portal

Statistics orderly matrix of negative

Meaning of the questions: As shown in the title.

Ideas: Analog enough.

class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int ans=0;
        for(auto x:grid)for(auto y:x)if(y<0)ans++;
        return ans;
    }
};
Code

K number of final product

Meaning of the questions:

You achieve a 'digital product category, "ProductOfNumbers, required to support the following two methods:

1. add(int num)

The num add numbers to the end face of the current list of numbers.
2. getProduct (int k)

Returns the list, the last k digits of the current product.

Ideas: vector stored-value, traversing the number k statistics on the line.

class ProductOfNumbers {
public:
    vector<int> vt;
    ProductOfNumbers() {
    }
    
    void add(int num) {
        vt.push_back(num);
    }
    
    int getProduct(int k) {
        int len = vt.size();
        int ans =1,i=len-1;
        while(k--){
            ans*=vt[i];
            i--;
        }
        return ans;
    }
};
Code

The number of meetings up to participate

Meaning of the questions:

Give you an array of events, which events [i] = [startDayi, endDayi], i represents the meeting begins at startDayi, ending endDayi.

You can meet startDayi <= d <= d any endDayi of the day to attend the meeting i. Note that only one day to attend a conference.

Please return the maximum number of meetings you can attend.

Ideas: the idea of greedy, position ordering the end of the press conference , and then use a vis array record whether the day has been used to traverse the meeting, from the start to the end of the day using a front for the meeting (greedy thoughts)

class Solution {
public:
    int maxEvents(vector<vector<int>>& events) {
        sort(events.begin(), events.end(), [](vector<int>& v1, vector<int>& v2){
            return v1[1]==v2[1]?v1[0]<v2[0]:v1[1]<v2[1];
        });
        int res = 0;
        vector<bool> vis(100001, false);
        for(auto& v: events) {
            for(int i=v[0];i<=v[1]; i++) {
                if(!vis[i]) { vis[i]=true, res++; break; }
            }
        }
        return res;
    }
};
Code

Repeatedly summed target array structure

Meaning of the questions:

To give you an array of integers target. At first, you have an array A, all of its elements are 1, you can do the following:

Let x your array and all the elements
that satisfies 0 <= i <i target.size of any index, and so the array A at the index i to the value x.
You can repeat this process as many times as
if constructed from A to start destination array target, you return True, otherwise False.

Ideas: Find law problem
since it is the sum added to a location, then the position must be the maximum value of the array
so that each round to find an array of maximum max and the sum of the maximum sum that position in the array is changed to max- ( sum-max) on an array is operated
backwards forward operation until == n && max == 1 returns sum true or max- (sum-max) is less than or equal to 0 return false;

class Solution {
public:
     bool isPossible(vector<int>& target) {
        long long sum = 0;
        priority_queue<long long >pq;
        for(auto i:target){
            sum += i;
            pq.push(i);
        }
        while(pq.top() > 1){
            auto mx = pq.top();
            pq.pop();
            long long  delta = sum-mx;
            if(mx <= delta)return 0;
            mx -= delta;
            sum -= delta;
            pq.push(mx);
        }
        return true;
    }
};
Code

 

Guess you like

Origin www.cnblogs.com/NukezY/p/12316561.html