leetcode 双周赛 Biweekly Contest 6

        最后一题比赛快结束的时候想到怎么做了(通过WA的数据猜出来的),比赛后10分钟做出来的。最终做了3题,时间1个小时左右吧。

1150. Check If a Number Is Majority Element in a Sorted Array

        这道题理论应该用二分,但是数据量很小(1000),所以就直接暴力过了:

class Solution {
public:
    bool isMajorityElement(vector<int>& nums, int target) {
        int count = 0;
        for(int i = 0;i < nums.size();i++)
        {
            if(nums[i] == target)
            {
                count++;
            }
            else if(nums[i] > target) break;
        }
        return count > (nums.size() / 2);
    }
};

1151 Minimum Swaps to Group All 1's Together

       这道题是滑动窗口题。找到包含最少0的窗口(窗口长度等于数组里1的个数)

class Solution {
public:
    int minSwaps(vector<int>& data) {
        int count = 0;
        int n = data.size();
        for(int i = 0;i < n;i++)
        {
            if(data[i] == 1) count++;
        }
        
        int begin = 0;
        int zeros = 0;
        int res = INT_MAX;
        for(int i = 0;i < n;i++) // end
        {
            int len = i - begin + 1;
            if(len > count)
            {
                if(data[begin] == 0) zeros--;
                begin++;
                len = count;
            }
            if(data[i] == 0) zeros++;
            if(len == count)
            {
                res = min(res, zeros);
            }
        }
        return res;
    }
};

1152. Analyze User Website Visit Pattern

        这道题可能是哈希和排序吧。WA了三次才过(有一次是没看清楚WA的点在哪,为了看错误点又提交了一次)。写的非常恶心。但是每次我洋洋洒洒写完100行的答案,总能在评论区看见10行搞定的 = =

class Solution {
public:
    vector<string> get(const string& s)
    {
        string str;
        vector<string> res;
        for(int i = 0;i < s.size();i++)
        {
            if(s[i] != ' ')
            {
                str.push_back(s[i]);
            }
            else
            {
                res.push_back(str);
                str.clear();
            }
        }
        return res;
    }
    bool IsSmaller(const string& s1, const string& s2)
    {
        vector<string> res1 = get(s1);
        vector<string> res2 = get(s2);
        for(int i = 0;i < res1.size();i++)
        {
            if(res1[i] == res2[i]) continue;
            return res1[i] < res2[i];
        }
        return false;
    }
    struct message
    {
        string username;
        int timestamp;
        string website;
        message() { }
        message(string& u, int& t, string& w) : username(u), timestamp(t), website(w){ }
        friend bool operator<(const message& m1, const message& m2)
        {
            return m1.timestamp < m2.timestamp;
        }
    };
    vector<string> mostVisitedPattern(vector<string>& username, vector<int>& timestamp, vector<string>& website) {
        unordered_map<string, int> count;
        int n = username.size();
        vector<message> msg(n);
        for(int i = 0;i < n;i++)
        {
            msg[i] = message(username[i], timestamp[i], website[i]);
        }
        sort(msg.begin(), msg.end());
        unordered_map<string, vector<string>> web;
        for(int i = 0;i < n;i++)
        {
            web[msg[i].username].push_back(msg[i].website);
        }
        for(auto& w : web)
        {
            string name = w.first;
            vector<string>& site = w.second;
            if(site.size() < 3) continue;
            unordered_set<string> unique;
            for(int i = 0;i < site.size(); i++)
            {
                for(int j = i + 1; j < site.size(); j++)
                {
                    for(int k = j + 1;k < site.size(); k++)
                    {
                        string str = site[i] + " " + site[j] + " " + site[k] + " ";
                        if(unique.find(str) == unique.end())
                        {
                            count[str]++;
                            unique.insert(str);
                        }
                    }
                }
            }
        }
        int maxtimes = 0;
        vector<string> res;
        string maxstr;
        for(auto& c : count)
        {
            int times = c.second;
            if(times > maxtimes)
            {
                maxtimes = times;
                maxstr = c.first;
            }
            else if(times == maxtimes)
            {
                if(IsSmaller(c.first, maxstr))
                {
                    maxstr = c.first;
                }
            }
        }
        return get(maxstr);
    }
};

1153. String Transforms Into Another String

        这道题首先不能一对多,存在就返回错误。然后如果存在转换存在闭环,如:

        abc -> bca

       相当于转换为a -> b -> c -> a,形成了一个回路,这种情况是可以转换的,因为可以借助不是abc的一个字符作为桥梁。

       但是如果闭环涵盖了26个小写字符,那么就没办法找到桥梁了,返回false。最终相当于判断所有形成闭环的字符是不是为26。(以上都是我从WA的答案中得到提示,才想到的

       总感觉求闭环数写的不是太对,我的方法是访问到已经访问过的字符后,把两个访问下标相减得到环路大小的,(我刚刚编的方法)。但是勉勉强强也AC了:

class Solution {
public:
    vector<int> visit;
    vector<int> graph;
    int res = 0;
    int count = 0;
    void dfs(int i) // find a loop
    {
        if(visit[i] != -1) 
        {
            res += count - visit[i];
            return;
        }
        visit[i] = count;
        if(graph[i] != -1)
        {
            count++;
            dfs(graph[i]);
        }
    }
    bool canConvert(string str1, string str2) {
        if(str1 == str2) return true;
        int n = str1.size();
        graph.resize(26, -1);
        for(int i = 0;i < n;i++)
        {
            if(graph[str1[i] - 'a'] != -1 && graph[str1[i] - 'a'] != str2[i] - 'a')
            {
                return false;
            }
            graph[str1[i] - 'a'] = str2[i] - 'a';
        }
        int count = 0;
        visit.resize(26, -1);
        for(int i = 0;i < 26;i++)
        {
            if(visit[i] == -1)
            {
                count = 0;
                dfs(i);
            }
        }
        if(res == 26) return false;
        return true;
    }
};

猜你喜欢

转载自www.cnblogs.com/fish1996/p/11333624.html