スポーツウェア


💪ジャージ
使用言語:C++
簡単な問題.
1.正解を解く
  • の学生配列の先頭と末尾を考慮するだけでよい.
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int solution(int n, vector<int> lost, vector<int> reserve) {
        vector<int> list(n, 1);
        int answer=0;
        for (int i = 0; i < lost.size(); i++) {
            list[lost[i] - 1]--;
        }
        for (int i = 0; i < reserve.size(); i++) {
            list[reserve[i] - 1]++;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list[i] == 0) {
                if (i != 0 && list[i - 1] == 2) {
                    list[i - 1]--;
                    list[i]++;
                }
                else if (i != list.size() - 1 && list[i + 1] == 2) {
                    list[i + 1]--;
                    list[i]++;
                }
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (list[i] !=0) {
                answer++;
            }
        }
        return answer;
    }
    2.初戦(75点)
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int solution(int n, vector<int> lost, vector<int> reserve) {
        int answer = 0;
        int clothes[32];
        fill_n(clothes,32,1);
        
        for(int i=0; i<lost.size(); i++){
        	clothes[lost[i]] = 0;
        }
        for(int i=0; i<reserve.size(); i++){
        	clothes[reserve[i]] = 2;
        }
        
         for(int l : lost){
            for(int r : reserve){
                if (l == r){
                    clothes[l] = 1;
                }
            }
        }
        
        for(int i=1; i<n+1; i++){
            if (clothes[i] > 1 && clothes[i+1] == 0){
                clothes[i]--;
                clothes[i+1]++;
            }else if (clothes[i] > 1 && clothes[i-1] == 0){
                clothes[i]--;
                clothes[i-1]++;
            }
        }
        
        for(int i=1; i<n+1; i++){
            if(clothes[i] > 0){
                answer++;
            }
        }
    
         
        return answer;
    }
    3.リンク
    https://programmers.co.kr/learn/courses/30/lessons/42862