Leetcode进阶之路——Weekly Contest 150

1160. Find Words That Can Be Formed by Characters
1160
给定一个单词数组words和一个字符串chars,若数组中的单词能由chars中的字母构成,则为good word
返回所有good word的长度之和
先用一个哈希表存储chars中所有字母出现的个数,之后遍历words,判断每个word是否都在chars中出现过,且出现次数小于等于chars

class Solution {
public:
    int countCharacters(vector<string>& words, string chars) {
        map<char, int> count;
        for(char c: chars) count[c]++;
        int cnt = 0;
        for(string s: words)
        {
            map<char, int> m;
            for(char c: s) m[c]++;
            bool flag = true;
            for(auto i: m) 
                if(count[i.first] < i.second)
                {
                    flag = false;
                    break;
                }
            if(flag) cnt += s.length();
        }
        return cnt;
    }
};

1161. Maximum Level Sum of a Binary Tree
1161
给定一棵树,求出各层数值之和,返回和最大的层数,若和相同,则返回深度最低的那层,根节点为第一层
直接层序遍历,求和即可

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxLevelSum(TreeNode* root) {
        if(root == NULL) return 0;
        queue<TreeNode*> q;
        int maxn = root->val, level = 1;
        q.emplace(root);
        int cur = 1;
        while(!q.empty())
        {
            cur++;
            int cnt = q.size();
            int sum = 0;
            for(int i = 0 ; i < cnt; ++i)
            {
                TreeNode* tn = q.front();
                q.pop();
                if(tn-> left)
                {
                    q.emplace(tn->left);
                    sum += tn->left->val;
                }
                if(tn->right)
                {
                    q.emplace(tn->right);
                    sum += tn->right->val;
                }
            }
            if(sum > maxn)
            {
                maxn = sum;
                level = cur;
            }
        }
        return level;
    }
};

https://leetcode.com/problems/as-far-from-land-as-possible/
1162
给定一个由0、1组成的二维数组,找到一个点,使其距离最近的1距离最远
若数组全0 或全1,则返回-1
先将所有的1存起来,之后广度优先搜索,更新各个0点距离最近的1的距离
最后遍历距离数组,返回最大的距离值

class Solution {
public:
    int maxDistance(vector<vector<int>>& grid)
	{
		vector<int> savepos;
		queue<pair<int, int>> q;
		int rows = grid.size(), cols = grid[0].size();
		vector<vector<int>> dp(rows, vector<int>(cols, 0));
		for (int i = 0; i < grid.size(); ++i)
		{
			for (int j = 0; j < grid[0].size(); ++j)
			{
				if (grid[i][j] == 1)
					q.emplace(make_pair(i, j));
			}
		}
		if (q.size() == rows * cols || q.size() == 0) return -1;
		int dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };
		while (!q.empty())
		{
			int cnt = q.size();
			for (int i = 0; i < cnt; ++i)
			{
				pair<int, int> p = q.front();
				q.pop();
				for (int j = 0; j < 4; ++j)
				{
					int x = p.first + dx[j], y = p.second + dy[j];
					if (x >= rows || x < 0 || y >= cols || y < 0) continue;
					if (grid[x][y] == 1) continue;
					if (dp[x][y]) continue;
					dp[x][y] = dp[p.first][p.second] + 1;
					q.emplace(make_pair(x, y));
				}
			}
		}
		
		int maxn = 0;
		for (int i = 0; i < rows; ++i)
		{
			for (int j = 0; j < cols; ++j)
			{
				maxn = max(maxn, dp[i][j]);
			}
		}
		return maxn;
	}
};

1163. Last Substring in Lexicographical Order
1163
给定一个字符串,返回所有字串中字典序最大的
既然字典序最大,因此首字母必定是以较大值开头,因此每次遍历数组,取当下的极大字母进行组合

class Solution {
public:
    string lastSubstring(string s) {
		if (s.length() == 0) return "";
		map<char, vector<int>> mpos;
		char maxc = 0;
		for (int i = 0; i < s.length(); ++i)
		{
			if (s[i] > maxc) maxc = s[i];
			mpos[s[i]].emplace_back(i);
		}
        if(mpos.size() == 1) return s;
        // 所有字母都相同
		if (mpos[maxc].size() == 1) return s.substr(mpos[maxc][0]);
		string res = "";
		res += maxc;
		vector<int> cur = mpos[maxc];
		int len = s.length();
		while (cur.size() > 1)
		{
			map<char, vector<int>> tmp;
			char temc = 0;
			for (int i = 0; i < cur.size(); ++i)
			{
				if (cur[i] == len - 1) continue;
				tmp[s[cur[i] + 1]].emplace_back(cur[i] + 1);
				temc = max(temc, s[cur[i] + 1]);
			}
			res += temc;
			cur = tmp[temc];
		}
		if (cur.size() == 1) res += s.substr(cur[0] + 1);
		return res;
	}
};
发布了109 篇原创文章 · 获赞 108 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/u013700358/article/details/99703835