본문 바로가기
IT/알고리즘

18] Leetcode 1475. Final Prices With a Special Discount in a Shop

by 깻잎쌈 2020. 6. 29.
반응형
 

Final Prices With a Special Discount in a Shop - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

숫자 배열을 for문 돌면서 

다음 숫자부터 마지막까지 숫자들 중에 for문의 숫자보다 더 작은 게 있는지 확인하는 문제. 

class Solution {
public:
    vector<int> finalPrices(vector<int>& prices) {
        
        vector<int> ans; 
        bool flag = false;
        
        for(int i = 0;i<prices.size();i++){
            flag = false;
            for(int j = i+1;j<prices.size();j++)
                if(prices[i] >=prices[j]){
                    ans.push_back(prices[i] - prices[j]);
                    flag = true; // 작은 놈 찾앗음
                    break;
                }       
            
            // 못 찾았으면 그대로       
            if(!flag)
                ans.push_back(prices[i]);
        }
        
        return ans;
    }
};

더 작은 게 있으면 그걸 삽입하고 break, 없으면 for문의 값을 삽입한다.

반응형

댓글