[笔试训练](十八)052:字符串压缩053:chika和蜜柑054:01背包

目录

052:字符串压缩

053:chika和蜜柑

054:01背包


052:字符串压缩

压缩字符串(一)_牛客题霸_牛客网 (nowcoder.com)

题目: 

题解:

双指针模拟

class Solution {
public:
    string compressString(string param) 
    {
        int n=param.size();
        string ret;
        int num=1;
        int left=0,right=0;
        ret+=param[0];
        while(left<n-1)
        {
            right+=1;
            if(param[left]==param[right])
            {
                num++;
            }
            else if(param[left]!=param[right] || right==n-1)
            {
                if(num!=1)
                {
                    if(num<=9)
                    {
                        ret+=num+'0';
                    }
                    else
                    {
                        ret+=to_string(num);
                    }     
                    num=1;
                }
                ret+=param[right];
                left=right;
            }        
        }
        return ret;       
    }
};

053:chika和蜜柑

chika和蜜柑 (nowcoder.com)

题目:

题解:

将每个橘子按甜度从大到小排序,相同甜度的橘子按酸度从小到大排序,从排序好的橘子中取前k个就是最优解。

有两个影响元素的排序需要二元组的思想排序:

// 使用lambda表达式对二元组进行排序  
// 排序规则:先按第二个元素从大到小排序,如果第二个元素相同,则按第一个元素从小到大排序  

sort(arr, arr + n, [&](const PII & a, const PII & b) {
        if (a.second != b.second) return a.second > b.second;
        else return a.first < b.first;
    });

#include <iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 2e5 + 10;
PII arr[N];
int n = 0, k = 0;
int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) cin >> arr[i].first;
    for (int i = 0; i < n; i++) cin >> arr[i].second;
    sort(arr, arr + n, [&](const PII & a, const PII & b) {
        if (a.second != b.second) return a.second > b.second;
        else return a.first < b.first;
    });
    long long s = 0, t = 0;
    for (int i = 0; i < k; i++) {
        s += arr[i].first;
        t += arr[i].second;
    }
    cout << s << " " << t << endl;
    return 0;

}

054:01背包

01背包_牛客题霸_牛客网 (nowcoder.com)

题目:

题解:

01背包模板题:
1. 状态表⽰:
dp[i][j] 表⽰从前 i 个物品中挑选,总体积不超过 j 的情况下,最⼤重量是多少。
2. 状态转移⽅程:
根据「最后⼀步」的状况,来分情况讨论:
i. 不选第i 个物品:相当于就是去前 i - 1 个物品中挑选,并且总体积不超过 j 。此时dp[i][j] = dp[i - 1][j] ;

ii. 选择第 i 个物品:那么我就只能去前 i - 1 个物品中,挑选总体积不超过 j - v[i] 的物品。此时 dp[i][j] = dp[i - 1][j - v[i]] + w[i] 。但是这种状态不⼀定存在,因此需要特判⼀下。
综上,状态转移⽅程为: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i])

 

class Solution {
public:

    int knapsack(int V, int n, vector<vector<int> >& vw) 
    {    
        int dp[1010][1010];
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=V;j++)
            {
                dp[i][j]=dp[i-1][j];
                if(j>=vw[i-1][0]) dp[i][j]=max(dp[i][j],dp[i-1][j-vw[i-1][0]]+vw[i-1][1]);
            } 
        }
        return dp[n][V];
    }
};


//**空间优化版**
class Solution {
public:
    int knapsack(int V, int n, vector<vector<int> >& vw) 
    {
        int dp[1010]={0};
        
        
        for(int i=0;i<n;i++)
        {
            for(int j=V;j>=vw[i][0];j--)
            {
                dp[j]=max(dp[j],dp[j-vw[i][0]]+vw[i][1]);
            }
            
        }
        
        return dp[V];
    }
};
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. "Open Source Software for the Internet of Things: A Review" by Souvik Pal and Bivas Mitra, published in Journal of Computer Science and Technology, 2018. 2. "Open Source Software in Education: A Review of Research" by David Wiley, published in Journal of Educational Technology Development and Exchange, 2017. 3. "Open Source Software and Open Standards in e-Government: A Review" by Osman Balci, published in Journal of e-Government Studies and Best Practices, 2016. 4. "Open Source Software and Its Impact on Innovation: A Review of Research" by Eric von Hippel, published in Journal of Technology Transfer, 2015. 5. "Open Source Software for Disaster Management: A Review" by Amir Khorram-Manesh, published in Journal of Disaster Medicine and Public Health Preparedness, 2014. 6. "Open Source Software and the Future of Healthcare: A Review" by Aiden Gregg and John Fox, published in Journal of Healthcare Information Management, 2013. 7. "Open Source Software and Its Role in Sustainable Development: A Review" by Chika Ezeanya-Esiobu, published in Journal of Sustainable Development, 2012. 8. "Open Source Software in Government: A Review of Research" by Marijn Janssen and Jan Grijpink, published in Journal of e-Government, 2011. 9. "Open Source Software for Business: A Review of Research" by Brian Fitzgerald, published in Journal of Information Technology, 2010. 10. "Open Source Software and Intellectual Property: A Review" by Lawrence Rosen, published in Journal of Intellectual Property Law & Practice, 2009.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲敲er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值