Skip to content

Commit 9d04e51

Browse files
Accepted
1 parent f4df28e commit 9d04e51

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

problems/src/CoinChange2.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Arrays;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 22/03/2017.
5+
*/
6+
public class CoinChange2
7+
{
8+
/**
9+
* Main method
10+
* @param args
11+
* @throws Exception
12+
*/
13+
public static void main(String[] args) throws Exception
14+
{
15+
int[] coins = {1,2,5};
16+
System.out.println(new CoinChange2().change(5, coins));
17+
}
18+
19+
public int change(int amount, int[] coins)
20+
{
21+
int[][] dp = new int[coins.length][amount + 1];
22+
for(int i = 0, l = coins.length; i < l; i ++)
23+
Arrays.fill(dp[i], -1);
24+
return dp(dp, 0, coins, coins.length, amount);
25+
}
26+
27+
private int dp(int[][] dp, int i, int[] coins, int l, int n)
28+
{
29+
if(n == 0) return 1;
30+
else if(i >= l) return 0;
31+
if(n < 0) return 0;
32+
if(dp[i][n] != -1) return dp[i][n];
33+
dp[i][n] = dp(dp, i + 1, coins, l, n) + dp(dp, i, coins, l, n - coins[i]);
34+
return dp[i][n];
35+
}
36+
37+
}

problems/src/RandomizedSet.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.*;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 23/03/2017.
5+
* Accepted
6+
*/
7+
public class RandomizedSet
8+
{
9+
private Map<Integer, Integer> map;
10+
private List<Integer> list;
11+
private Random random;
12+
/** Initialize your data structure here. */
13+
public RandomizedSet()
14+
{
15+
map = new HashMap<>();
16+
list = new ArrayList<>();
17+
random = new Random();
18+
}
19+
20+
/**
21+
* Main method
22+
* @param args
23+
* @throws Exception
24+
*/
25+
public static void main(String[] args) throws Exception
26+
{
27+
RandomizedSet rSet = new RandomizedSet();
28+
System.out.println(rSet.getRandom());
29+
System.out.println(rSet.insert(1));
30+
System.out.println(rSet.insert(2));
31+
System.out.println(rSet.insert(2));
32+
System.out.println(rSet.insert(3));
33+
System.out.println(rSet.remove(2));
34+
System.out.println(rSet.insert(2));
35+
System.out.println(rSet.getRandom());
36+
System.out.println(rSet.insert(234));
37+
System.out.println(rSet.insert(23));
38+
System.out.println(rSet.insert(22));
39+
System.out.println(rSet.getRandom());
40+
System.out.println(rSet.remove(245));
41+
System.out.println(rSet.remove(234));
42+
System.out.println(rSet.getRandom());
43+
}
44+
45+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
46+
public boolean insert(int val)
47+
{
48+
if(!map.keySet().contains(val))
49+
{
50+
int pos = list.size();
51+
map.put(val, pos);
52+
list.add(val);
53+
return true;
54+
}
55+
return false;
56+
}
57+
58+
/** Removes a value from the set. Returns true if the set contained the specified element. */
59+
public boolean remove(int val)
60+
{
61+
if(map.containsKey(val))
62+
{
63+
int size = list.size();
64+
int posVal = map.get(val);
65+
if(posVal < (size - 1))
66+
{
67+
int last = list.get(size - 1);
68+
map.put(last, posVal);
69+
list.set(posVal, last);
70+
}
71+
map.remove(val);
72+
list.remove(size - 1);
73+
return true;
74+
}
75+
return false;
76+
}
77+
78+
/** Get a random element from the set. */
79+
public int getRandom()
80+
{
81+
/*if(list.size() == 0) return 0;
82+
else if (list.size() == 1) return list.get(0);*/
83+
return list.get(random.nextInt(list.size() - 1));
84+
}
85+
86+
}

0 commit comments

Comments
 (0)