Coding Practice,48天强训(13)

Topic 1:牛牛冲钻五(模拟)

比较简单,但是可以复习一下一些输入输出的细节,某些测试或者考试环境比较老,输入输出流都需要手动写,所以一定要有处理老式环境的能力

//#include <iostream>
#include <bits/stdc++.h>// 万能头文件 GCC/G++编译器的扩展
using namespace std;
int main()
{
    ios::sync_with_stdio(false);//关闭C++和C的I/O同步,加速 cin/cout。
    cin.tie(0);//解除 cin 和 cout 的绑定,减少不必要的刷新
    //竞赛专用
    
    int T;//T测试样本数,n比赛数,k连胜奖励
    cin >> T;
    // 模拟试试
    while(T--)
    {
        int n, k;
        cin >> n >> k;
        string str;
        cin >> str;
        int res = 0;   
        for(int i = 0; i < n; ++i)
        {
            if(str[i] == 'W')
            {
                if(i >= 2 && str[i - 1] == 'W' && str[i - 2] == 'W')
                {
                    res += k;
                }
                else res += 1;
            }
            else res -= 1; 
        }
        cout << res << endl;
    }
    return 0; 
}

#include <bits/stdc++.h>// 万能头文件 GCC/G++编译器的扩展,只需写一行即可使用大部分STL功能

ios::sync_with_stdio(false);  //关闭C++和C的I/O同步,加速 cin/cout。
cin.tie(0);  //解除 cin 和 cout 的绑定,减少不必要的刷新


Topic 2:最长无重复子数组(滑动窗口)

class Solution {
public:
    int maxLength(vector<int>& arr) 
    {
        //遍历元素,暴力和前连续元素找不同,用哈希判断,复杂度N方,可以尝试用滑动窗口降低
       if(arr.empty()) return 0;
        
        int i = 0, j = 0, res = 0;
        unordered_map<int, int> posMap;  // 记录元素最近出现位置
        
        while(j < arr.size())
        {
            // 如果当前元素已经出现过,并且在当前窗口内
            if(posMap.find(arr[j]) != posMap.end() && posMap[arr[j]] >= i)
            {
                res = max(res, j - i);  // 更新最大长度
                i = posMap[arr[j]] + 1; // 移动左边界到重复元素的下一个位置
            }
            posMap[arr[j]] = j;  // 更新元素位置
            ++j;
        }
        res = max(res, j - i);  // 最后再比较一次
        
        return res;
    }
};

用个unordered,时间上会慢一些,排行榜前几的都是手动用vector去做的,会快一倍左右


Topic 3:重排字符串

贪心做一做,有几个贪心策略——优先处理最多的字符,优先批量处理,间隔1来放置,可以最大化利用格子,避免相邻,和leetcode 1054.距离相等的条形码一样的题目

#include <vector>
#include <queue>
#include <unordered_map>
#include <string>
using namespace std;

class Solution {
  public:
    string rearrangestring(string str) {
        unordered_map<char, int> freq;
        for (char c : str) {
            freq[c]++;
        }

        priority_queue<pair<int, char>> maxHeap;
        for (auto& entry : freq) {
            maxHeap.push({entry.second, entry.first});
        }

        string res;
        while (maxHeap.size() >= 2) {
            auto first = maxHeap.top();
            maxHeap.pop();
            auto second = maxHeap.top();
            maxHeap.pop();

            res += first.second;
            res += second.second;

            if (--first.first > 0) {
                maxHeap.push(first);
            }
            if (--second.first > 0) {
                maxHeap.push(second);
            }
        }

        if (!maxHeap.empty()) {
            auto last = maxHeap.top();
            if (last.first > 1) {
                return "";
            }
            res += last.second;
        }

        return res;
    }
};

但是这题实测试时,通过率有点问题,应该是某些条件没有写清楚,之后再来做修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值