当前位置 博文首页 > LJ的博客:石器时代 —— Leetcode刷题日记 (三 面试题相关)

    LJ的博客:石器时代 —— Leetcode刷题日记 (三 面试题相关)

    作者:[db:作者] 时间:2021-09-04 09:21

    随机刷题

    vivo2020 算法题B卷

    运矿石

    • 每次可以挖到多个矿石,每个矿石的重量都不一样,挖矿结束后需要通过一款平衡矿车运送下山;
    • 平衡矿车有左右2个车厢,中间只有1个车轮沿着导轨滑到山下,且矿车只有在2个车厢重量完全相等且矿石数量相差不超过1个的情况下才能成功运送矿石,否则在转弯时可能出现侧翻
    • 假设小v挖到了n(n<100)个矿石,每个矿石重量不超过100,为了确保一次性将n个矿石都运送出去,一旦矿车的车厢重量不一样就需要购买配重砝码。请问小v每次最少需要购买多少重量的砝码呢? (假设车厢足够放下这些矿石和砝码,砝码重量任选)
    • 思路:恰好装满的01背包问题的变形,多了一个且矿石数量相差不超过1个条件!
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <vector>
    #include <limits.h> // INT_MIN
    using namespace std;
    #define MAX_NUM 101
    int solution(int n, int weight[]) {
        int sum = 0, h_sum, h_n = (n + 1) / 2;
        for (int i = 0; i < n; i++) sum += weight[i];
        h_sum = sum / 2;
        vector<vector<int>> dp(h_sum + 1, vector<int>(h_n + 1, INT_MIN));
        for (int i = 0; i <= h_sum; i++) dp[i][0] = 0;
        for (int i = 0; i < n; i++) {
            auto tmp = dp; // 避免覆盖!
            for (int j = weight[i]; j <= h_sum; j++) {
                for (int k = 1; k <= h_n; k++)
                    tmp[j][k] = max(tmp[j][k], dp[j-weight[i]][k-1]+weight[i]);
            }
            dp = tmp;
        }
        if (n % 2) return sum - 2 * max(dp[h_sum][h_n], dp[h_sum][h_n-1]);
        else return sum - 2 * dp[h_sum][h_n];
    }
    
    int main()
    {    
    	string str("");
    	getline(cin, str);
    	int a[MAX_NUM];
    	int i = 0;
    	char *p;
    	int count = 0;
    	const char* strs = str.c_str();
    	p = strtok((char *)strs, " ");
    	while(p) {
    		a[i] = atoi(p);
    		count++;
    		p = strtok(NULL, " ");
    		i++;
    		if(i >= MAX_NUM)
    			break;
    	}
    	int result = solution(count, a);
    	cout << result << endl;
    	return 0;
    }
    

    报数

    • 将N(N<10000)个人排成一排,从第1个人开始报数;如果报数是M的倍数就出列,报到队尾后则回到队头继续报,直到所有人都出列;
    • 按照出列顺序为每个人依次分配工号
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string.h>
    #include <vector>
    using namespace std;
    typedef struct _node
    {
        int num;
        struct _node * next;
    }node;
    void solution(int N, int M)
    {
    	if (N < 1 || M < 1) return;
        vector<int> rows(N, 0);
        for (int i = 0; i < N; i++) rows[i] = i + 1;
        int pos = 0;
        while (rows.size() > 0) {
            pos += M - 1;
            pos %= rows.size();
            cout << rows[pos] << " ";
            rows.erase(rows.begin() + pos);
        } 
        cout << endl;
    }
    int main()
    {
    	int N;
    	int M;
    	string str("");
    	getline(cin, str);
    	char *p;
    	const char* strs = str.c_str();
    	p = strtok((char *)strs, " ");
    	N = atoi(p);
    	p = strtok(NULL, " ");
    	M = atoi(p);
    	solution(N, M);
    	return 0;
    }
    

    跳盒子

    • 有n个盒子排成了一行,每个盒子上面有一个数字a[i],表示在该盒子上的人最多能向右移动a[i]个盒子(比如当前所在盒子上的数字是3,则表示可以一次向右前进1个盒子,2个盒子或者3个盒子)
    • 小v从左边第一个盒子上开始体验游戏,请问最少需要移动几次能到最后一个盒子上?
    • 思路:贪心,Leetcode45;但是不一定是能到达最后的盒子!
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    int solution(int a[], int N)
    {
        int pre = 0, cur = 0;
        int steps = 0;
        for (int i = 0; i < N - 1; i++) {
            cur = max(cur, i+a[i]);
            if (pre == i) {
                pre = cur;
                steps++;
                if (cur >= N - 1) 
                    break;
            }
        }
        return pre >= N - 1 ? steps : -1;
    }
    int main()
    {
    	string str("");
    	getline(cin, str);
    	int a[2000];
    	int i = 0;
    	char *p;
    	int count = 0;
    	const char* strs = str.c_str();
    	p = strtok((char *)strs, " ");
    	while(p)
    	{
    		a[i] = atoi(p);
    		count++;
    		p = strtok(NULL, " ");
    		i++;
    		if(i >= 2000)
    			break;
    	}
    	int num = solution(a, count);
    	cout << num << endl;
    	return 0;
    }
    

    VIVO2020 算法题A卷

    服务部署

    • 三维背包问题!
    //write code here
    vector<vector<vector<int>>> dp(countOfApp + 1,vector<vector<int