牛客网shopee 2019校招部分编程题汇总


周六晚上就要shopee笔试啦!刷完牛客网shopee 2019校招部分编程题汇总之后内心拔凉拔凉的。
https://github.com/seawood/Leetcode-JianzhiOffer

Shopee的办公室(二)

典型的动态规划

//牛客Shopee 2019校招部分编程题汇总
//编程题1:shopee的办公室(二)
#include <iostream>
using namespace std;
int main(){
    int x,y,n;
    cin >> x>>y>>n;
    bool** mark = new bool*[x+1];
    long long** re = new long long*[x+1];
    for(int i = 0; i < x+1;++i){
        mark[i] = new bool[y+1];
        re[i] = new long long[y+1];
    }
    for(int i = 0; i < x+1;++i)
        for(int j = 0; j < y+1;++j){
            mark[i][j] = false;
            re[i][j]=0;
        }
    for(int i = 0;i<n;++i){
        int xi,yi;
        cin>>xi>>yi;
        mark[xi][yi]=true;
    }
    for(int i = 0; i < x+1;++i)
        re[i][0]=1;
    for(int j = 0;j < y+1;++j)
        re[0][j] = 1;
    for(int i = 1; i < x+1;++i)
        for(int j = 1; j < y+1;++j)
            if(!mark[i][j])
                re[i][j] = re[i-1][j]+re[i][j-1];
    long long result = re[x][y];
    for(int i = 0;i < x+1;++i){
        delete[] mark[i];
        delete[] re[i];
    }
    cout<<result<<endl;
    return 0;
}

Shopee的零食柜

在maxNum和sumNum之间二分查找

//牛客Shopee 2019校招部分编程题汇总
//编程题2:shopee的零食柜
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool judge(const int& stepLen, const vector<int>& num, int steps) {
	int tempSum = 0, cur = 0, i = 0, len = num.size();
	for (i = 0; i < len; ++i) {
		tempSum += num[i];
		if (tempSum > stepLen) {
			cur++;
			tempSum = num[i];
		}
		if (cur >= steps)
			break;
	}
	return i == len;
}
int main() {
	int n, m;
	cin >> n >> m;
	if (n <= 0)
		return 0;
	vector<int> num;
	int maxNum = 0, sumNum = 0, temp;
	for (int i = 0; i < n; ++i) {
		cin >> temp;
		num.push_back(temp);
		maxNum = max(maxNum, temp);
		sumNum += temp;
	}
	int left = maxNum, right = sumNum, ans = right, mid;
	while (left <= right) {
		mid = (left + right) / 2;
		if (judge(mid, num, m)) {
			right = mid - 1;
			ans = min(ans, mid);
		}
		else
			left = mid + 1;
	}
	cout << mid << endl;  //cout<<ans<<endl通过率只有50%
	return 0;
}

实现字通配符*

递归

//牛客Shopee 2019校招部分编程题汇总
//编程题3:实现字通配符*
#include <iostream>
#include <set>
#include <string>
using namespace std;
void match(set<pair<int,int>>& ans, const string& pattern, const string& target,
          int pattern_cur_pos, int target_cur_pos, const int& start){
    if(pattern_cur_pos == (int)pattern.size()){
        if(target_cur_pos-start>0)
            ans.insert(make_pair(start, target_cur_pos-start));
        return;
    }
    if(target_cur_pos == (int)target.size())
        return;
    if(pattern[pattern_cur_pos] == '*'){
        match(ans, pattern, target, pattern_cur_pos+1, target_cur_pos+1, start);
        match(ans, pattern, target, pattern_cur_pos, target_cur_pos+1, start);
        match(ans, pattern, target, pattern_cur_pos+1, target_cur_pos, start);
    }else if(pattern[pattern_cur_pos] == target[target_cur_pos])
        match(ans, pattern, target, pattern_cur_pos+1, target_cur_pos+1, start);
}
int main(){
    string pattern, target;
    cin>>pattern>>target;
    set<pair<int,int>> ans;
    for(int i = 0; i < target.size(); ++i)
        match(ans, pattern, target, 0, i, i);
    if(ans.size() == 0)
        cout<<-1<<" "<<0<<endl;
    else{
        for(auto &i : ans)
            cout<<i.first<<" "<<i.second<<endl;
    }
    return 0;
}

建物流中转站

同一行/同一列的元素与某个元素的距离在行/列上是一样的

//牛客Shopee 2019校招部分编程题汇总
//编程题4:建物流中转站
#include <iostream>
#include <climits>
using namespace std;
int main(){
    int n;
    cin>>n;
    int** num = new int*[n];
    for(int i = 0; i < n;++i)
        num[i] = new int[n];
    int *rowSum = new int[n], *colSum = new int[n];
    for(int i = 0; i <n;++i)
        for(int j = 0; j <n ;++j){
            cin>>num[i][j];
            rowSum[i]+=num[i][j];
            colSum[j]+=num[i][j];
        }
    int *rowDis = new int[n],*colDis = new int[n];
    for(int i = 0; i < n; ++i){
        rowDis[i]=0;
        colDis[i]=0;
        for(int j = 0; j < n;++j){
            rowDis[i]+=rowSum[j]*abs(j-i);
            colDis[i]+=colSum[j]*abs(j-i);
        }
    }
    int res = INT_MAX;
    for(int i = 0; i < n; ++i)
        for(int j = 0; j < n;++j){
            if(num[i][j]==0)
                res = min(res, rowDis[i]+colDis[j]);
        }
    delete[] rowSum;
    delete[] colSum;
    delete[] rowDis;
    delete[] colDis;
    for(int i = 0; i < n;++i)
        delete[] num[i];
    cout<<res<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值