top | item 21627243

(no title)

pchf | 6 years ago

Right...with

template<typename T> optional<int> minimum_coins2(T target, std::vector<T> denominations) {

  std::unordered_set<T> values, newvalues;

  values.insert(target);

  auto k = 0;

  while (values.size() > 0) {

    k += 1;

    for (auto v : values) {

      for (auto d : denominations) {

    if (v == d)

      return k;

    else if (v > d)

      newvalues.insert(v - d);

    else

      continue;

      }

    }

    swap(values, newvalues);

    newvalues.clear();

  }

  return {};
}

c++ 180ms, julia 270ms (using a value of 9070)

discuss

order

No comments yet.