모든 챌린지
쉬움 2명 해결 정답률 25.0%

Cart Total With Discount

Total a shopping cart and apply a discount above a threshold.

배우게 될 내용

  • Total prices with quantities
  • Apply a discount above a threshold
  • Organize code into helper functions

문제

번역본 보기

Add up the prices in the cart. If the total is at least discount_threshold, take discount_percent percent off the whole total.

Implement cart_total(prices, discount_threshold, discount_percent).

Input

prices: The item prices. discount_threshold: The total that unlocks the discount. discount_percent: The discount in percent.

Output

Return the amount to pay.

제약 조건

0 ≤ length ≤ 1,000; 0 ≤ each price ≤ 100,000; 0 ≤ discount_percent ≤ 100

예시

예시 1
입력:
prices = [40, 35, 25]
discount_threshold = 100
discount_percent = 10

예상 출력:
expected = 90.0
입력:
std::vector<long long> prices = {40, 35, 25};
long long discount_threshold = 100;
long long discount_percent = 10;

예상 출력:
double expected = 90.0;
입력:
long[] prices = new long[] {40, 35, 25};
long discount_threshold = 100;
long discount_percent = 10;

예상 출력:
double expected = 90.0;

설명

The total is 100, which reaches the threshold, so 10% off: 90.

예시 2
입력:
prices = [20, 30]
discount_threshold = 100
discount_percent = 10

예상 출력:
expected = 50.0
입력:
std::vector<long long> prices = {20, 30};
long long discount_threshold = 100;
long long discount_percent = 10;

예상 출력:
double expected = 50.0;
입력:
long[] prices = new long[] {20, 30};
long discount_threshold = 100;
long discount_percent = 10;

예상 출력:
double expected = 50.0;

설명

The total 50 is below 100, so no discount: 50.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

First find the cart total before any discount.

힌트 2

Apply the percentage reduction only when the total reaches the threshold.

힌트 3

Make sure the discount is taken from the whole order, not item by item.

예상 복잡도

O(n) time, O(1) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 LMLaura Mahlow Python 823 ms - 3/5 코드 보기
2 HJhettory Jurash Python 828 ms - 3/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 2개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.