Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Smallest Missing Coin Sum

Find the first positive amount that cannot be made from the available coins.

Bạn sẽ học được gì

  • Sort coins and track reachable sums
  • Find the first gap in what you can pay
  • Prove why the greedy rule works

Đề bài

Xem bản dịch

You may select any subset of the supplied coins. Each individual coin may be used at most once, even when multiple coins share a value. Find the smallest positive integer amount that no selection can produce.

Implement missing_coin_sum(coins).

Input

coins: The values of the available coins; duplicates represent separate coins.

Output

Return the smallest positive impossible amount.

Ràng buộc

1 ≤ number of coins ≤ 200,000. Each value is between 1 and 1,000,000,000.

Ví dụ

Ví dụ 1
Đầu vào:
coins = [1, 1, 4]

Kết quả mong đợi:
expected = 3
Đầu vào:
std::vector<long long> coins = {1, 1, 4};

Kết quả mong đợi:
long long expected = 3;
Đầu vào:
long[] coins = new long[] {1, 1, 4};

Kết quả mong đợi:
long expected = 3;

Giải thích

Amounts 1 and 2 use the one-valued coins. No available subset totals 3.

Ví dụ 2
Đầu vào:
coins = [2, 3]

Kết quả mong đợi:
expected = 1
Đầu vào:
std::vector<long long> coins = {2, 3};

Kết quả mong đợi:
long long expected = 1;
Đầu vào:
long[] coins = new long[] {2, 3};

Kết quả mong đợi:
long expected = 1;

Giải thích

There is no way to make 1 because every coin is worth at least 2.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Sort the coins and build the smallest reachable sum from smallest upward.

Gợi ý 2

If the next coin is larger than the first unreachable amount, that amount can never be formed.

Gợi ý 3

Duplicates help only when they extend the currently reachable range without gaps.

Độ phức tạp mong đợi

O(n log n) time, O(1) extra space, excluding sorting implementation details

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.