반응형
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문의 값을 삽입한다.
반응형
'IT > 알고리즘' 카테고리의 다른 글
20] Leetcode 1450. Number of Students Doing Homework at a Given Time (0) | 2020.07.01 |
---|---|
19★] Leetcode 1436. Destination City (0) | 2020.06.30 |
17] Leetcode 1323. Maximum 69 Number (0) | 2020.06.29 |
16] Leetcode 1252. Cells with Odd Values in a Matrix (0) | 2020.06.27 |
15] Leetcode 1464. Maximum Product of Two Elements in an Array (0) | 2020.06.25 |
댓글