算法菜鸡备战4月27日蓝桥杯省赛----0311

1

2012. 数组美丽值求和 - 力扣(LeetCode)

class Solution {
public:
    int sumOfBeauties(vector& nums) {
        int n = nums.size();
        int ans = 0;
        for(int i = 2;i < n;i++){
            if(nums[i - 2] < nums[i - 1] && nums[i - 1] < nums[i]){
                // cout << i - 1 << endl;
                ans++;
            }
        }
        vector tmp1(n),tmp2(n);
        tmp1[0] = nums[0];
        tmp2[n - 1] = nums[n - 1];
        for(int i = 1;i < n - 1;i++){
            tmp1[i] =max(nums[i],tmp1[i - 1]);
        }
        for(int i = n - 2;i >= 0;i--){
            tmp2[i] =min(nums[i],tmp2[i + 1]);
        }
        for(int i = 1;i < n - 1;i++){
            if(nums[i] > tmp1[i - 1] && nums[i] < tmp2[i + 1])
            {
                cout <

2

901. 股票价格跨度 - 力扣(LeetCode)

class StockSpanner {
public:
    stack st1,st2;
    vector vi1,vi2;
    StockSpanner() {
        vi1.clear(),vi2.clear();
    }
    
    int next(int price) {
        int ans = 0;
        int pos = vi1.size() - 1;
        while(pos >= 0 && price >= vi1[pos]){
            pos -= vi2[pos];
        }
        vi1.push_back(price);

        ans = vi1.size() - pos - 1;
        vi2.push_back(ans);
        // ans = max(ans,1);
        // cout << ans << endl;
        return ans;
    }
};

/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner* obj = new StockSpanner();
 * int param_1 = obj->next(price);
 */

3

853. 车队 - 力扣(LeetCode)

class Solution {
public:
    int carFleet(int target, vector& position, vector& speed) {
        int n = position.size();
        vector > vv(n,vector(2));
        for(int i = 0;i < n;i++){
            vv[i][0] = position[i],vv[i][1] = speed[i];
        }
        double oldtime = -1;
        int ans = 0;
        sort(vv.begin(),vv.end());
        for(int i = n - 1;i >=0;i--){
            double len = target - vv[i][0];
            double v   = vv[i][1];
            // cout << len/v <<':' << endl;
            if(oldtime >= len/v){
                // 赶上了
                ;
            }else{
                // cout <

你可能感兴趣的:(算法)