forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added minimum_coins problem in greedy algorithms in C++, python, JS, …
…Java
- Loading branch information
1 parent
d81bd5f
commit 6d5d5f9
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
public class MinimumCoins { | ||
private static List<Integer> minimumCoins(Integer value, List<Integer> denominations) { | ||
List<Integer> result = new ArrayList<>(); | ||
|
||
// Assuming list of denominations is sorted in descending order | ||
for(Integer curDenom : denominations) { | ||
while (curDenom <= value){ | ||
result.add(curDenom); | ||
value -= curDenom; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<TestScenario> scenarios = Arrays.asList( | ||
new TestScenario(100, Arrays.asList(50, 25, 10, 5, 1), Arrays.asList(50, 50)), | ||
new TestScenario(101, Arrays.asList(50, 25, 10, 5, 1), Arrays.asList(50, 50, 1)), | ||
new TestScenario(77, Arrays.asList(50, 25, 10, 5, 1), Arrays.asList(50, 25, 1, 1)), | ||
new TestScenario(38, Arrays.asList(50, 25, 10, 5, 1), Arrays.asList(25, 10, 1, 1, 1)), | ||
new TestScenario(17, Arrays.asList(50, 25, 10, 5, 1), Arrays.asList(10, 5, 1, 1)), | ||
new TestScenario(3, Arrays.asList(50, 25, 10, 5, 1), Arrays.asList(1, 1, 1)), | ||
new TestScenario(191, Arrays.asList(100, 50, 25, 10, 5, 1), Arrays.asList(100, 50, 25, 10, 5, 1)) | ||
); | ||
|
||
for (TestScenario scenario : scenarios) { | ||
List<Integer> actual = minimumCoins(scenario.value, scenario.denoms); | ||
if (!actual.equals(scenario.result)) { | ||
System.out.println("Test Failed: Value: " + scenario.value | ||
+ ", Denominations: " + scenario.denoms | ||
+ ", Expected Result: " + scenario.result | ||
+ ", Actual Result: " + actual); | ||
} | ||
} | ||
} | ||
|
||
private static class TestScenario { | ||
public int value; | ||
public List<Integer> denoms; | ||
public List<Integer> result; | ||
|
||
public TestScenario(int value, List<Integer> denoms, List<Integer> result) { | ||
this.value = value; | ||
this.denoms = denoms; | ||
this.result = result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* Part of Cosmos by OpenGenus Foundation */ | ||
|
||
function minimumCoins(value, denominations) { | ||
var result = []; | ||
// Assuming denominations is sorted in descendig order | ||
for(var i = 0; i < denominations.length; i++) { | ||
var cur_denom = denominations[i]; | ||
while(cur_denom <= value) { | ||
result.push(cur_denom); | ||
value -= cur_denom; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
Array.prototype.equals = function(other) { | ||
if (!other || !(other instanceof Array)) { | ||
return false; | ||
} | ||
|
||
if (this.length != other.length) { | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < this.length; i++) { | ||
if (this[i] != other[i]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function test() { | ||
scenarios = [ | ||
{value: 100, denoms: [50, 25, 10, 5, 1], result: [50, 50]}, | ||
{value: 101, denoms: [50, 25, 10, 5, 1], result: [50, 50, 1]}, | ||
{value: 77, denoms: [50, 25, 10, 5, 1], result: [50, 25, 1, 1]}, | ||
{value: 38, denoms: [50, 25, 10, 5, 1], result: [25, 10, 1, 1, 1]}, | ||
{value: 17, denoms: [50, 25, 10, 5, 1], result: [10, 5, 1, 1]}, | ||
{value: 3, denoms: [50, 25, 10, 5, 1], result: [1, 1, 1]}, | ||
{value: 191, denoms: [100, 50, 25, 10, 5, 1], result: [100, 50, 25, 10, 5, 1]} | ||
]; | ||
|
||
scenarios.forEach(function(scenario) { | ||
var actual = minimumCoins(scenario.value, scenario.denoms); | ||
if (!scenario.result.equals(actual)) { | ||
console.error("Test Failed: Value: " + scenario.value | ||
+ ", Denominations: " + scenario.denoms | ||
+ ", Expected Result: " + scenario.result | ||
+ ", Actual Result: " + actual); | ||
} | ||
}, this); | ||
} | ||
|
||
test(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
/* This file uses features only available on C++11. | ||
Compile using -std=c++11 or -std=gnu++11 flag. */ | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
std::vector<unsigned int> minimum_coins(int value, std::vector<unsigned int> denominations) { | ||
std::vector<unsigned int> result; | ||
// Assuming denominations is sorted in descendig order | ||
for(int cur_denom : denominations) { | ||
while (cur_denom <= value) { | ||
result.push_back(cur_denom); | ||
value -= cur_denom; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
// Testing | ||
int main() { | ||
std::vector<unsigned int> result = minimum_coins(191, {100, 50, 25, 10, 5, 1}); | ||
for (auto i = result.begin(); i != result.end(); ++i) { | ||
std::cout << *i << " "; | ||
} | ||
std::cout << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
def minimum_coins(value, denominations): | ||
result = [] | ||
# Assuming denominations is sorted in descendig order | ||
for cur_denom in denominations: | ||
while cur_denom <= value: | ||
result.append(cur_denom) | ||
value = value - cur_denom | ||
|
||
return result | ||
|
||
# Testing | ||
def test(): | ||
scenarios = [[100, [50, 25, 10, 5, 1], [50, 50]], | ||
[101, [50, 25, 10, 5, 1], [50, 50, 1]], | ||
[77, [50, 25, 10, 5, 1], [50, 25, 1, 1]], | ||
[38, [50, 25, 10, 5, 1], [25, 10, 1, 1, 1]], | ||
[17, [50, 25, 10, 5, 1], [10, 5, 1, 1]], | ||
[3, [50, 25, 10, 5, 1], [1, 1, 1]], | ||
[191, [100, 50, 25, 10, 5, 1], [100, 50, 25, 10, 5, 1]]] | ||
|
||
for scenario in scenarios: | ||
actual = minimum_coins(scenario[0], scenario[1]) | ||
if actual != scenario[2]: | ||
message = "Test Failed: Value: {}, Denominations: {}, Expected Result: {}, Actual Result: {}".format(scenario[0], scenario[1], scenario[2], actual) | ||
print message | ||
|
||
return None | ||
|
||
test() |