leetcode解题思路分析一百二十七1046 - 1052 题

  1. 最后一块石头的重量
    有一堆石头,每块石头的重量都是正整数。每一回合,从中选出两块 最重的 石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 x <= y。那么粉碎的可能结果如下:如果 x == y,那么两块石头都会被完全粉碎;如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x。最后,最多只会剩下一块石头。返回此石头的重量。如果没有石头剩下,就返回 0。

直接用Prorotiy_queue构建最大堆然后挨个处理完事。

class Solution {
public:
    int lastStoneWeight(vector<int>& stones) 
    {
         priority_queue<int> stoneHeap;

         for (auto s : stones)
         {
             stoneHeap.push(s);
         }

         while (stoneHeap.size() > 1)
         {
             int a = stoneHeap.top();
             stoneHeap.pop();

             int b = stoneHeap.top();
             stoneHeap.pop();

             if (a > b) stoneHeap.push(a - b);
         }

         return stoneHeap.size() ? stoneHeap.top() : 0;
    }
};
  1. 删除字符串中的所有相邻重复项
    给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在 S 上反复执行重复项删除操作,直到无法继续删除。在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。

利用string的类似栈的操作即可

class Solution {
public:
    string removeDuplicates(string s) {
        string stk;
        for (char ch : s) {
            if (!stk.empty() && stk.back() == ch) {
                stk.pop_back();
            } else {
                stk.push_back(ch);
            }
        }
        return stk;
    }
};

  1. 最长字符串链
    给出一个单词数组 words ,其中每个单词都由小写英文字母组成。如果我们可以 不改变其他字符的顺序 ,在 wordA 的任何地方添加 恰好一个 字母使其变成 wordB ,那么我们认为 wordA 是 wordB 的 前身 。例如,“abc” 是 “abac” 的 前身 ,而 “cba” 不是 “bcad” 的 前身词链是单词 [word_1, word_2, …, word_k] 组成的序列,k >= 1,其中 word1 是 word2 的前身,word2 是 word3 的前身,依此类推。一个单词通常是 k == 1 的 单词链 。从给定单词列表 words 中选择单词组成词链,返回 词链的 最长可能长度 。

本题思路很简单:动态规划去判断每一个位置和前面所有位置的关系。注意点有3:1. 注意需要先排序;2. 注意需要写辅助函数;3. 辅助函数必须用引用

class Solution {
public:
    int longestStrChain(vector<string>& words) 
    {
        int nMax = 1;
        int nSize = words.size();

        sort(words.begin(), words.end(), [](const string &s1, const string &s2) {return s1.size() < s2.size();});

        vector<int> dp(nSize, 1);

        for (int i = 1; i < nSize; ++i)
        {
            for (int j = 0; j < i; ++j)
            {
                if (isBefore(words[i], words[j]))
                {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            nMax = max(nMax, dp[i]);
        }

        return nMax;
    }

    int isBefore(string &a, string &b)
    {
        if (a.size() != b.size() + 1)
        {
            return false;
        }

        int j = 0;
        for (int i = 0; j < b.size() && i < a.size(); ++i)
        {
            if (a[i] == b[j])
            {
                ++j;
            }
        }

        return j == b.size();
    }

};
  1. 合作过至少三次的演员和导演
    写一条SQL查询语句获取合作过至少三次的演员和导演的 id 对 (actor_id, director_id)
# Write your MysqL query statement below
select actor_id, director_id from (
    select actor_id, director_id, count(timestamp) as count
    from ActorDirector 
    group by actor_id, director_id
    having count >= 3
)t

  1. 高度检查器
    学校打算为全体学生拍一张年度纪念照。根据要求,学生需要按照 非递减 的高度顺序排成一行。排序后的高度情况用整数数组 expected 表示,其中 expected[i] 是预计排在这一行中第 i 位的学生的高度(下标从 0 开始)。给你一个整数数组 heights ,表示 当前学生站位 的高度情况。heights[i] 是这一行中第 i 位学生的高度(下标从 0 开始)。返回满足 heights[i] != expected[i] 的 下标数量

有手就行

class Solution {
public:
    int heightChecker(vector<int>& heights) 
    {
        int         nRet = 0;
        vector<int> sortHeight(heights);

        sort(sortHeight.begin(), sortHeight.end());

        for (int i = 0; i < heights.size(); ++i)
        {
            if (heights[i] != sortHeight[i]) nRet++;
        }

        return nRet;

    }
};
  1. 爱生气的书店老板
    一个书店老板,他的书店开了 n 分钟。每分钟都有一些顾客进入这家商店。给定一个长度为 n 的整数数组 customers ,其中 customers[i] 是在第 i 分钟开始时进入商店的顾客数量,所有这些顾客在第 i 分钟结束后离开。在某些时候,书店老板会生气。 如果书店老板在第 i 分钟生气,那么 grumpy[i] = 1,否则 grumpy[i] = 0。当书店老板生气时,那一分钟的顾客就会不满意,若老板不生气则顾客是满意的。书店老板知道一个秘密技巧,能抑制自己的情绪,可以让自己连续 minutes 分钟不生气,但却只能使用一次。请你返回 这一天营业下来,最多有多少客户能够感到满意 。

滑动窗口

class Solution {
public:
    int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) {
        int total = 0;
        int n = customers.size();
        for (int i = 0; i < n; i++) {
            if (grumpy[i] == 0) {
                total += customers[i];
            }
        }
        int increase = 0;
        for (int i = 0; i < minutes; i++) {
            increase += customers[i] * grumpy[i];
        }
        int maxIncrease = increase;
        for (int i = minutes; i < n; i++) {
            increase = increase - customers[i - minutes] * grumpy[i - minutes] + customers[i] * grumpy[i];
            maxIncrease = max(maxIncrease, increase);
        }
        return total + maxIncrease;
    }
};

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...