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

Commit 98b1966

Browse files
committed
more algorithms
1 parent d61f02e commit 98b1966

8 files changed

+130276
-52
lines changed

Diff for: src/main/java/ArrayManipulation.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @author medany
3+
*/
4+
5+
/*
6+
* You are given a list(1-indexed) of size n, initialized with zeroes. You have
7+
* to perform m operations on the list and output the maximum of final values of
8+
* all the n elements in the list. For every operation, you are given three
9+
* integers a, b and k and you have to add value k to all the elements ranging
10+
* from index a to b (both inclusive).
11+
*
12+
* For example, consider a list of size 3. The initial list would be a= [0, 0,
13+
* 0] and after performing the update O(a, b, k) = (2, 3, 30), the new list
14+
* would be a = [0, 30, 30]. Here, we've added value 30 to elements between
15+
* indices 2 and 3. Note the index of the list starts from 1.
16+
*/
17+
18+
public class ArrayManipulation {
19+
20+
public long solve(int n, int m, String[] operations) {
21+
long max = 0, temp = 0;
22+
long[] array = new long[n];
23+
24+
for (int i = 0; i < m; i++) {
25+
String[] s = operations[i].split(" ");
26+
int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]), k = Integer.parseInt(s[2]);
27+
array[a - 1] += k;
28+
if (b < n)
29+
array[b] -= k;
30+
}
31+
32+
for (int i = 0; i < n; i++) {
33+
temp += array[i];
34+
if (temp > max)
35+
max = temp;
36+
}
37+
return max;
38+
}
39+
}

Diff for: src/main/java/DynamicArray.java

+25-52
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,55 @@
11
import java.util.ArrayList;
22
import java.util.List;
3-
import java.util.Scanner;
43

54
/**
65
* @author medany
76
*/
87

8+
/*
9+
*
10+
* Create a list , seqList, of N empty sequences, where each sequence is indexed
11+
* from 0 to N-1. The elements within each of the sequences also use 0-indexing.
12+
* Create an integer, lastAnswer, and initialize it to 0. The types of queries
13+
* that can be performed on your list of sequences (seqList) are described
14+
* below: Query: 1 x y Find the sequence, seq, at index (x ^ lastAnswer) % N in
15+
* seqList. Append integer y to sequence . Query: 2 x y Find the sequence, seq,
16+
* at index (x ^ lastAnswer) % N in seqList. Find the value of element y % size
17+
* in seq (where is the size of seq) and assign it to lastAnswer. Print the new
18+
* value of on a new line
19+
*
20+
*/
21+
922
public class DynamicArray {
1023

1124
public Integer[] solve(int n, String[] queries) {
1225

1326
List<Integer> output = new ArrayList<>();
1427

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);
28+
List<Integer>[] seqList = new ArrayList[n];
29+
for (int i = 0; i < n; i++) {
30+
List<Integer> l = new ArrayList<>();
31+
seqList[i] = l;
2032
}
2133

22-
int lastAnswer = 0;
34+
int lastAnswer = 0, size = 0;
2335

2436
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]);
37+
String[] input = q.split(" ");
38+
int type = Integer.parseInt(input[0]);
39+
int x = Integer.parseInt(input[1]), y = Integer.parseInt(input[2]);
2740

2841
switch (type) {
2942
case 1:
30-
seqList.get((x ^ lastAnswer) % n).add(y);
43+
seqList[(x ^ lastAnswer) % n].add(y);
3144
break;
3245
case 2:
33-
int size = seqList.get((x ^ lastAnswer) % n).size();
34-
lastAnswer = seqList.get((x ^ lastAnswer) % n).get(y % size);
46+
size = seqList[(x ^ lastAnswer) % n].size();
47+
lastAnswer = seqList[(x ^ lastAnswer) % n].get(y % size);
3548
output.add(lastAnswer);
3649
break;
3750
}
3851
}
3952

4053
return output.toArray(new Integer[output.size()]);
4154
}
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-
}
8255
}

Diff for: src/main/java/SparseArrays.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
/*
9+
* There is a collection N of strings ( There can be multiple occurences of a
10+
* particular string ). Each string's length is no more than 20 characters.
11+
* There are also Q queries. For each query, you are given a string, and you
12+
* need to find out how many times this string occurs in the given collection of
13+
* N strings.
14+
*/
15+
public class SparseArrays {
16+
17+
public int[] solve(int n, String[] input, String[] queries) {
18+
19+
int[] output = new int[queries.length];
20+
21+
Map<String, Integer> inputMap = new HashMap<String, Integer>();
22+
for (String s : input) {
23+
if (inputMap.containsKey(s)) {
24+
inputMap.put(s, inputMap.get(s) + 1);
25+
} else {
26+
inputMap.put(s, 1);
27+
}
28+
}
29+
30+
for (int i = 0; i < queries.length; i++) {
31+
if (inputMap.containsKey(queries[i])) {
32+
output[i] = inputMap.get(queries[i]);
33+
} else {
34+
output[i] = 0;
35+
}
36+
}
37+
38+
return output;
39+
}
40+
}

Diff for: test/main/java/ArrayManipulationTest.java

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 ArrayManipulationTest {
14+
15+
private ArrayManipulation alg = new ArrayManipulation();
16+
long actual, expected;
17+
18+
@Test
19+
public void Test_1() {
20+
21+
BufferedReader ir = null;
22+
FileReader i = null;
23+
24+
try {
25+
i = new FileReader(
26+
new File(this.getClass().getClassLoader().getResource("array_manipulation_input_1").getPath()));
27+
ir = new BufferedReader(i);
28+
29+
String line = ir.readLine();
30+
31+
int n = Integer.parseInt(line.split(" ")[0]), m = Integer.parseInt(line.split(" ")[1]);
32+
String[] array = new String[m];
33+
34+
int q = 0;
35+
while ((line = ir.readLine()) != null && q < array.length) {
36+
array[q] = line;
37+
q++;
38+
}
39+
40+
actual = alg.solve(n, m, array);
41+
expected = 200L;
42+
43+
Assert.assertEquals(expected, actual);
44+
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
} finally {
48+
try {
49+
if (ir != null)
50+
ir.close();
51+
if (i != null)
52+
i.close();
53+
} catch (IOException ex) {
54+
ex.printStackTrace();
55+
}
56+
}
57+
58+
}
59+
60+
@Test
61+
public void Test_2() {
62+
63+
BufferedReader ir = null;
64+
FileReader i = null;
65+
66+
try {
67+
i = new FileReader(
68+
new File(this.getClass().getClassLoader().getResource("array_manipulation_input_2").getPath()));
69+
ir = new BufferedReader(i);
70+
71+
String line = ir.readLine();
72+
73+
int n = Integer.parseInt(line.split(" ")[0]), m = Integer.parseInt(line.split(" ")[1]);
74+
String[] array = new String[m];
75+
76+
int q = 0;
77+
while ((line = ir.readLine()) != null && q < array.length) {
78+
array[q] = line;
79+
q++;
80+
}
81+
82+
actual = alg.solve(n, m, array);
83+
expected = 7542539201L; // 2497169732L
84+
85+
Assert.assertEquals(expected, actual);
86+
87+
} catch (IOException e) {
88+
e.printStackTrace();
89+
} finally {
90+
try {
91+
if (ir != null)
92+
ir.close();
93+
if (i != null)
94+
i.close();
95+
} catch (IOException ex) {
96+
ex.printStackTrace();
97+
}
98+
}
99+
100+
}
101+
102+
@Test
103+
public void Test_3() {
104+
105+
BufferedReader ir = null;
106+
FileReader i = null;
107+
108+
try {
109+
i = new FileReader(
110+
new File(this.getClass().getClassLoader().getResource("array_manipulation_input_3").getPath()));
111+
ir = new BufferedReader(i);
112+
113+
String line = ir.readLine();
114+
115+
int n = Integer.parseInt(line.split(" ")[0]), m = Integer.parseInt(line.split(" ")[1]);
116+
String[] array = new String[m];
117+
118+
int q = 0;
119+
while ((line = ir.readLine()) != null && q < array.length) {
120+
array[q] = line;
121+
q++;
122+
}
123+
124+
actual = alg.solve(n, m, array);
125+
expected = 2497169732L;
126+
127+
Assert.assertEquals(expected, actual);
128+
129+
} catch (IOException e) {
130+
e.printStackTrace();
131+
} finally {
132+
try {
133+
if (ir != null)
134+
ir.close();
135+
if (i != null)
136+
i.close();
137+
} catch (IOException ex) {
138+
ex.printStackTrace();
139+
}
140+
}
141+
142+
}
143+
}

Diff for: test/main/java/SparseArraysTest.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class SparseArraysTest {
9+
10+
private SparseArrays alg = new SparseArrays();
11+
int[] actual = null, expected = null;
12+
13+
@Test
14+
public void Test_1() {
15+
16+
actual = alg.solve(4, new String[] { "aba", "baba", "aba", "xzxb" }, new String[] { "aba", "xzxb", "ab" });
17+
18+
expected = new int[] { 2, 1, 0 };
19+
20+
Assert.assertArrayEquals(expected, actual);
21+
}
22+
23+
}

Diff for: test/main/resources/array_manipulation_input_1

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
5 3
2+
1 2 100
3+
2 5 100
4+
3 4 100

0 commit comments

Comments
 (0)