See the original problem on HackerRank.
Crypto Bank provides $n$ crypto currencies, each of which has a fixed conversion rate with respect to dollar. The conversion rate w.r.t dollar is not subject to change over time. But as you may have imagined, the conversion rate between each pair of crypto currencies may change over time, due to economic and political factors.
Ron has $m$ bitcoins with him. The conversion rate of a bitcoin w.r.t dollar is $k$. You are given the conversion rates of every other crypto currency with respect to bitcoin. You are also given the conversion rate of these crypto currencies w.r.t dollar. You have to find the currency, that Ron can buy with $m$ bitcoins, such that the total value in dollars is maximized.
Note that, Ron cannot buy crypto currency in fractions and Ron can own only one type of crypto currency at the end of the transaction.
Input Format
In the first line, you are given three integers - \(n\), \(m\) and \(k\), where \(n\) is the number of currencies excluding bitcoin, \(m\) is the amount of bitcoins Ron has and \(k\) is the conversion rate of bitcoin w.r.t dollar.
In the next line, you will be given \(n\) integers \((a_{i})\), the \(i^{th}\) of which denotes the conversion rate of the \(i^{th}\) crypto currency w.r.t dollar.
In the third line, you will be given \(n\) integers \((b_{i})\), the \(i^{th}\) of which denotes the number of units Ron can buy with one bitcoin, Ron can buy \(b_{i}\) units of \(i^{th}\) crypto currency with 1 bitcoin.
Constraints
- \(1<n,m,k<101\)
- \(1<a_{i}<1001\)
- \(1<b_{i}<1001\)
Output Format
In a single line, you have to output the maximum value in dollars that you can have. It is guaranteed that the answer will fit into a 32-bit integer.
Solutions
The challenge can be solved by applying map | reduce on both sequences at the same time, that results in applying zip before.
In C++:
|
|
We can factor m out:
|
|
Rust, first map, then reduce:
|
|
Rust, find max while iterating. The fold
init is k
, i.e. the best exchange
before trying with other currencies. Note that this version, compared to the
previous, doesn’t need to check for errors (max
on an empty collection returns
a None
), so it’s safer.
|
|
In Rust, we can even use a library for parallel computation: rayon
|
|
Using reduce
. The identity is (k, 1)
|
|