Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit d61f02e

Browse files
committed
new algorithms
1 parent 29d3586 commit d61f02e

11 files changed

+118885
-3
lines changed

src/main/java/CoinChange.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
/*
7+
* Given a number of dollars, n, and a list of dollar values for m distinct
8+
* coins, C = {c0, c1, c2,..., cm-1} , find and print the number of different
9+
* ways you can make change for dollars if each coin is available in an infinite
10+
* quantity.
11+
*
12+
* @see https://www.youtube.com/watch?v=ZaVM057DuzE
13+
*/
14+
15+
public class CoinChange {
16+
17+
public long solve(int money, int[] coins) {
18+
19+
long[] ways = new long[money + 1];
20+
ways[0] = (long) 1;
21+
for (int coin : coins) {
22+
for (int i = coin; i < ways.length; i++) {
23+
ways[i] += ways[i - coin];
24+
}
25+
}
26+
return ways[money];
27+
}
28+
}

src/main/java/DynamicArray.java

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.Scanner;
4+
5+
/**
6+
* @author medany
7+
*/
8+
9+
public class DynamicArray {
10+
11+
public Integer[] solve(int n, String[] queries) {
12+
13+
List<Integer> output = new ArrayList<>();
14+
15+
List<List<Integer>> seqList = new ArrayList<>(n);
16+
int N = n;
17+
for (int i = 0; i < N; N--) {
18+
List<Integer> l = new ArrayList<>(n);
19+
seqList.add(l);
20+
}
21+
22+
int lastAnswer = 0;
23+
24+
for (String q : queries) {
25+
int type = Integer.parseInt(q.split(" ")[0]);
26+
int x = Integer.parseInt(q.split(" ")[1]), y = Integer.parseInt(q.split(" ")[2]);
27+
28+
switch (type) {
29+
case 1:
30+
seqList.get((x ^ lastAnswer) % n).add(y);
31+
break;
32+
case 2:
33+
int size = seqList.get((x ^ lastAnswer) % n).size();
34+
lastAnswer = seqList.get((x ^ lastAnswer) % n).get(y % size);
35+
output.add(lastAnswer);
36+
break;
37+
}
38+
}
39+
40+
return output.toArray(new Integer[output.size()]);
41+
}
42+
43+
public static void main(String[] args) {
44+
Scanner sc = new Scanner(System.in);
45+
46+
int N = sc.nextInt();
47+
int Q = sc.nextInt();
48+
49+
List<List<Integer>> seqList = new ArrayList<>(N);
50+
int n = N;
51+
for (int i = 0; i < n; n--) {
52+
List<Integer> l = new ArrayList<>(N);
53+
seqList.add(l);
54+
}
55+
56+
int lastAnswer = 0;
57+
58+
while (Q > 0) {
59+
int type = sc.nextInt();
60+
int x, y;
61+
62+
switch (type) {
63+
case 1:
64+
x = sc.nextInt();
65+
y = sc.nextInt();
66+
seqList.get((x ^ lastAnswer) % N).add(y);
67+
break;
68+
case 2:
69+
x = sc.nextInt();
70+
y = sc.nextInt();
71+
int size = seqList.get((x ^ lastAnswer) % N).size();
72+
lastAnswer = seqList.get((x ^ lastAnswer) % N).get(y % size);
73+
System.out.println(lastAnswer);
74+
break;
75+
}
76+
77+
Q--;
78+
}
79+
80+
sc.close();
81+
}
82+
}

test/main/java/BalancedBracketsTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ public void Test_Bulk() {
7474
Assert.assertEquals(expected, actual);
7575
}
7676

77-
} catch (
78-
79-
IOException e) {
77+
} catch (IOException e) {
8078
e.printStackTrace();
8179
} finally {
8280
try {

test/main/java/CoinChangeTest.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import org.junit.Assert;
2+
import org.junit.ComparisonFailure;
3+
import org.junit.Test;
4+
5+
/**
6+
* @author medany
7+
*/
8+
9+
public class CoinChangeTest {
10+
11+
private CoinChange alg = new CoinChange();
12+
long actual = 0, expected = 0;
13+
14+
@Test
15+
public void Test_1() {
16+
17+
actual = alg.solve(4, new int[] { 1, 2, 3 });
18+
expected = 4;
19+
20+
test(expected, actual);
21+
}
22+
23+
@Test
24+
public void Test_2() {
25+
26+
actual = alg.solve(10, new int[] { 2, 5, 3, 6 });
27+
expected = 5;
28+
29+
test(expected, actual);
30+
}
31+
32+
@Test
33+
public void Test_3() {
34+
35+
actual = alg.solve(250, new int[] { 8, 47, 13, 24, 25, 31, 32, 35, 3, 19, 40, 48, 1, 4, 17, 38, 22, 30, 33, 15,
36+
44, 46, 36, 9, 20, 49 });
37+
expected = 3542323427L;
38+
39+
test(expected, actual);
40+
}
41+
42+
@Test
43+
public void Test_4() {
44+
45+
actual = alg.solve(85, new int[] { 50, 10, 17, 21, 8, 3, 12, 41, 9, 13, 43, 37, 49, 19, 23, 28, 45, 46, 29, 16,
46+
34, 25, 2, 22, 1 });
47+
expected = 370927;
48+
49+
test(expected, actual);
50+
}
51+
52+
@Test
53+
public void Test_5() {
54+
55+
actual = alg.solve(222, new int[] { 3, 25, 34, 38, 26, 42, 16, 10, 15, 50, 39, 44, 36, 29, 22, 43, 20, 27, 9,
56+
30, 47, 13, 40, 33 });
57+
expected = 5621927;
58+
59+
test(expected, actual);
60+
}
61+
62+
private void test(long expected, long actual) {
63+
try {
64+
Assert.assertEquals(expected, actual);
65+
} catch (ComparisonFailure failed) {
66+
System.out.println(failed.getStackTrace()[3].getMethodName() + " failed, " + failed.getMessage());
67+
}
68+
}
69+
}

test/main/java/DynamicArrayTest.java

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import java.io.BufferedReader;
2+
import java.io.File;
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
/**
10+
* @author medany
11+
*/
12+
13+
public class DynamicArrayTest {
14+
15+
private DynamicArray alg = new DynamicArray();
16+
Integer[] actual = null, expected = null;
17+
18+
@Test
19+
public void Test_1() {
20+
21+
BufferedReader ir = null, or = null;
22+
FileReader i = null, o = null;
23+
24+
try {
25+
i = new FileReader(
26+
new File(this.getClass().getClassLoader().getResource("dynamic_array_input_1").getPath()));
27+
o = new FileReader(
28+
new File(this.getClass().getClassLoader().getResource("dynamic_array_output_1").getPath()));
29+
ir = new BufferedReader(i);
30+
or = new BufferedReader(o);
31+
int n = 0;
32+
String[] queries;
33+
String line = ir.readLine();
34+
35+
n = Integer.parseInt(line.split(" ")[0]);
36+
queries = new String[Integer.parseInt(line.split(" ")[1])];
37+
38+
int q = 0, r = 0;
39+
while ((line = ir.readLine()) != null && q < queries.length) {
40+
if (line.charAt(0) == '2')
41+
r++;
42+
queries[q] = line;
43+
q++;
44+
}
45+
46+
actual = alg.solve(n, queries);
47+
expected = new Integer[r];
48+
49+
int e = 0;
50+
while ((line = or.readLine()) != null && e < expected.length) {
51+
expected[e] = Integer.parseInt(line);
52+
e++;
53+
}
54+
55+
Assert.assertArrayEquals(expected, actual);
56+
57+
} catch (IOException e) {
58+
e.printStackTrace();
59+
} finally {
60+
try {
61+
if (ir != null)
62+
ir.close();
63+
if (or != null)
64+
or.close();
65+
if (i != null)
66+
i.close();
67+
if (o != null)
68+
o.close();
69+
} catch (IOException ex) {
70+
ex.printStackTrace();
71+
}
72+
}
73+
74+
}
75+
76+
@Test
77+
public void Test_2() {
78+
79+
BufferedReader ir = null, or = null;
80+
FileReader i = null, o = null;
81+
82+
try {
83+
i = new FileReader(
84+
new File(this.getClass().getClassLoader().getResource("dynamic_array_input_2").getPath()));
85+
o = new FileReader(
86+
new File(this.getClass().getClassLoader().getResource("dynamic_array_output_2").getPath()));
87+
ir = new BufferedReader(i);
88+
or = new BufferedReader(o);
89+
int n = 0;
90+
String[] queries;
91+
String line = ir.readLine();
92+
93+
n = Integer.parseInt(line.split(" ")[0]);
94+
queries = new String[Integer.parseInt(line.split(" ")[1])];
95+
96+
int q = 0, r = 0;
97+
while ((line = ir.readLine()) != null && q < queries.length) {
98+
if (line.charAt(0) == '2')
99+
r++;
100+
queries[q] = line;
101+
q++;
102+
}
103+
104+
actual = alg.solve(n, queries);
105+
expected = new Integer[r];
106+
107+
int e = 0;
108+
while ((line = or.readLine()) != null && e < expected.length) {
109+
expected[e] = Integer.parseInt(line);
110+
e++;
111+
}
112+
113+
Assert.assertArrayEquals(expected, actual);
114+
115+
} catch (IOException e) {
116+
e.printStackTrace();
117+
} finally {
118+
try {
119+
if (ir != null)
120+
ir.close();
121+
if (or != null)
122+
or.close();
123+
if (i != null)
124+
i.close();
125+
if (o != null)
126+
o.close();
127+
} catch (IOException ex) {
128+
ex.printStackTrace();
129+
}
130+
}
131+
132+
}
133+
134+
@Test
135+
public void Test_3() {
136+
137+
BufferedReader ir = null, or = null;
138+
FileReader i = null, o = null;
139+
140+
try {
141+
i = new FileReader(
142+
new File(this.getClass().getClassLoader().getResource("dynamic_array_input_3").getPath()));
143+
o = new FileReader(
144+
new File(this.getClass().getClassLoader().getResource("dynamic_array_output_3").getPath()));
145+
ir = new BufferedReader(i);
146+
or = new BufferedReader(o);
147+
int n = 0;
148+
String[] queries;
149+
String line = ir.readLine();
150+
151+
n = Integer.parseInt(line.split(" ")[0]);
152+
queries = new String[Integer.parseInt(line.split(" ")[1])];
153+
154+
int q = 0, r = 0;
155+
while ((line = ir.readLine()) != null && q < queries.length) {
156+
if (line.charAt(0) == '2')
157+
r++;
158+
queries[q] = line;
159+
q++;
160+
}
161+
162+
actual = alg.solve(n, queries);
163+
expected = new Integer[r];
164+
165+
int e = 0;
166+
while ((line = or.readLine()) != null && e < expected.length) {
167+
expected[e] = Integer.parseInt(line);
168+
e++;
169+
}
170+
171+
Assert.assertArrayEquals(expected, actual);
172+
173+
} catch (IOException e) {
174+
e.printStackTrace();
175+
} finally {
176+
try {
177+
if (ir != null)
178+
ir.close();
179+
if (or != null)
180+
or.close();
181+
if (i != null)
182+
i.close();
183+
if (o != null)
184+
o.close();
185+
} catch (IOException ex) {
186+
ex.printStackTrace();
187+
}
188+
}
189+
190+
}
191+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2 5
2+
1 0 5
3+
1 1 7
4+
1 0 3
5+
2 1 0
6+
2 1 1

0 commit comments

Comments
 (0)