LeetCode(Weekly Contest 183)题解

0. 前言

1. 题解

1.1 5376. 非递增顺序的最小子序列(1403. Minimum Subsequence in Non-Increasing Order)

class Solution {
public:
    vector<int> minSubsequence(vector<int>& nums) {
        sort(nums.begin(), nums.end(), [](int x, int y) {
            return x > y;
        });
        int sum = 0;
        for (auto v : nums) {
            sum += v;
        }
        int cur = 0;
        vector<int> res;
        for (auto v : nums) {
            if (cur * 2 > sum)  break;
            cur += v;
            res.push_back(v);
        }
        return res;
    }
}; 

1.2 5377. 将二进制表示减到 1 的步骤数(1404. Number of Steps to Reduce a Number in Binary Representation to One)

class Solution {
public:
    int numSteps(string s) {
        int ans = 0, len = s.length(), cur = len-1;
        for (int i = len-1 ; i >= 1 ; ) {
            cout << i << endl;
            while (cur >= 0 && s[cur] == s[i])  cur--;
            ans += i-cur;
            if (s[i] == ‘1‘) {
                ans++;
                if (cur >= 0)   s[cur] = ‘1‘;
            }
            i = cur;
        }
        return ans;
    }
}; 

1.3 5195. 最长快乐字符串(1405. Longest Happy String)

  • 中文版题目描述:https://leetcode-cn.com/problems/longest-happy-string/
  • 英文版题目描述:https://leetcode.com/problems/longest-happy-string/
  • 思路:每次尽可能消耗个数最多的字符,如果和已构造的末尾不同,则消耗两个,否则选择次大的消耗一个
    • 第一次做没有用 map 结构保持个数的自动排序,选择所有情况特殊判断,代码长到怀疑人生,虽然过了
    • 结束后重新思考了一下,用一个关键词升序的 map 维护字符数量的排序关系
  • 代码如下:
class Solution {
public:
    string longestDiverseString(int a, int b, int c) {
        map<int, vector<char>, greater<int>> mp;
        if (a > 0)  mp[a].push_back(‘a‘);
        if (b > 0)  mp[b].push_back(‘b‘);
        if (c > 0)  mp[c].push_back(‘c‘);
        int last = a + b + c;
        string ans = "";
        char tail = ‘ ‘;
        int num = 0;
        while (last) {
            bool find = false;
            for (auto i = mp.begin() ; i != mp.end() ; i++) {
                int cnt = i->first;
                if (i == mp.begin()) {
                    for (auto j = i->second.begin() ; j != i->second.end() ; j++) {
                        char cur = *j;
                        if (tail != cur) {
                            if (cnt >= 2) {
                                ans += cur;
                                ans += cur;
                                cnt -= 2;
                                last -= 2;
                            } else {
                                ans += cur;
                                cnt -= 1;
                                last -= 1;
                            }
                            i->second.erase(j);
                            if (i->second.size() == 0) {
                                mp.erase(i);
                            }
                            if (cnt > 0) {
                                mp[cnt].push_back(cur);
                            }
                            find = true;
                            tail = cur;
                            break;
                        }
                    }
                } else {
                    for (auto j = i->second.begin() ; j != i->second.end() ; j++) {
                        char cur = *j;
                        if (tail != cur) {
                            if (cnt >= 1) {
                                ans += cur;
                                cnt -= 1;
                                last -= 1;
                            }
                            i->second.erase(j);
                            if (i->second.size() == 0) {
                                mp.erase(i);
                            }
                            if (cnt > 0) {
                                mp[cnt].push_back(cur);
                            }
                            find = true;
                            tail = cur;
                            break;
                        }
                    }
                }
                if (find)    break;
            }
            if (!find)    break;
        }
        return ans;
    }
}; 

1.4 5379. 石子游戏 III(1406. Stone Game III)

  • 中文版题目描述:https://leetcode-cn.com/problems/stone-game-iii/
  • 英文版题目描述:https://leetcode.com/problems/stone-game-iii/
  • 思路:本题属于信息透明的平等博弈,是博弈论中最基础的一种,思路就是倒着从游戏的最后一步开始反着算,对每个状态计算“玩家从该状态开始能不能获胜/最多能拿多少分”,用类似动态规划的思想一直算到第一步
    • dp[i] 表示第 i 到 n-1 堆石子,先手取,能获得的最大值
    • 从后往前计算,dp[n] = 0
    • dp[i] = max(sum-dp[i+1], sum-dp[i+2], sum-dp[i+3]),其中 sum 表示第 i 到 n-1 堆石子的分数之和
  • 代码如下:
class Solution {
public:
    string stoneGameIII(vector<int>& stoneValue) {
        int n = stoneValue.size();
        vector<int> dp = vector<int>(n+1, INT_MIN);
        dp[n] = 0;
        int sum = 0;
        for (int i = n-1 ; i >= 0 ; i--) {
            sum += stoneValue[i];
            for (int j = 1 ; j <= 3 && i+j <= n ; j++) {
                dp[i] = max(dp[i], sum-dp[i+j]);
            }
        }
        int alice = dp[0], bob = sum-dp[0];
        cout << alice << endl;
        cout << bob << endl;
        if (alice > bob)    return "Alice";
        else if (alice < bob)   return "Bob";
        else    return "Tie";
    }
};

2. 参考文献

原文地址:https://www.cnblogs.com/wangao1236/p/12636784.html

时间: 2024-09-30 10:51:04

LeetCode(Weekly Contest 183)题解的相关文章

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

Leetcode Weekly Contest 152

退役老人现在连leetcode都不会做了 = = 今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg 总结下教训就是 本渣现在做题连题目都看不清就开始做.开始写题之前应当把样例过一遍,然后自己再造1-2个例子,然后再开始做 A题:统计素数的个数(素数筛或者sqrt(n)判断都可以),然后分别计算count! class Solution { public: int numPrimeArrangements(int n) { vector<int> ha

[Leetcode Weekly Contest]174

链接:LeetCode [Leetcode]5328. 方阵中战斗力最弱的 K 行 给你一个大小为?m?* n?的方阵?mat,方阵由若干军人和平民组成,分别用 0 和 1 表示. 请你返回方阵中战斗力最弱的?k?行的索引,按从最弱到最强排序. 如果第?i?行的军人数量少于第?j?行,或者两行军人数量相同但 i 小于 j,那么我们认为第 i 行的战斗力比第 j 行弱. 军人 总是 排在一行中的靠前位置,也就是说 1 总是出现在 0 之前. 输入:\(mat = [[1,1,0,0,0], [1,

108th LeetCode Weekly Contest Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

108th LeetCode Weekly Contest Minimum Falling Path Sum

Given a square array of integers A, we want the minimum sum of a falling path through A. A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from t

113th LeetCode Weekly Contest Flip Equivalent Binary Trees

For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip opera

113th LeetCode Weekly Contest Largest Time for Given Digits

Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour time is 00:00, and the largest is 23:59.  Starting from 00:00, a time is larger if more time has elapsed since midnight. Return the answer as a string

113th LeetCode Weekly Contest Reveal Cards In Increasing Order

In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck. Now, you do the following steps repeatedly, until all cards are revealed: Take the

116th LeetCode Weekly Contest Maximum Width Ramp

Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j].  The width of such a ramp is j - i. Find the maximum width of a ramp in A.  If one doesn't exist, return 0. Example 1: Input: [6,0,8,2,1,5] Output: 4 Explanatio