-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem9_TwoSumFinancialTransactions.java
More file actions
139 lines (95 loc) · 3.54 KB
/
Problem9_TwoSumFinancialTransactions.java
File metadata and controls
139 lines (95 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.*;
class Transaction {
int id;
int amount;
String merchant;
int time;
Transaction(int id, int amount, String merchant, int time) {
this.id = id;
this.amount = amount;
this.merchant = merchant;
this.time = time;
}
}
class TransactionAnalyzer {
ArrayList<Transaction> transactions = new ArrayList<Transaction>();
public void addTransaction(Transaction t) {
transactions.add(t);
}
public void findTwoSum(int target) {
HashMap<Integer, Transaction> map = new HashMap<Integer, Transaction>();
for (Transaction t : transactions) {
int complement = target - t.amount;
if (map.containsKey(complement)) {
Transaction t2 = map.get(complement);
System.out.println("Pair: (" + t2.id + ", " + t.id + ")");
}
map.put(t.amount, t);
}
}
public void findTwoSumWithTimeWindow(int target, int window) {
HashMap<Integer, Transaction> map = new HashMap<Integer, Transaction>();
for (Transaction t : transactions) {
int complement = target - t.amount;
if (map.containsKey(complement)) {
Transaction t2 = map.get(complement);
if (Math.abs(t.time - t2.time) <= window) {
System.out.println("Time Window Pair: (" + t2.id + ", " + t.id + ")");
}
}
map.put(t.amount, t);
}
}
public void detectDuplicates() {
HashMap<String, ArrayList<Transaction>> map = new HashMap<String, ArrayList<Transaction>>();
for (Transaction t : transactions) {
String key = t.amount + "-" + t.merchant;
if (!map.containsKey(key)) {
map.put(key, new ArrayList<Transaction>());
}
map.get(key).add(t);
}
for (String key : map.keySet()) {
ArrayList<Transaction> list = map.get(key);
if (list.size() > 1) {
System.out.print("Duplicate: ");
for (Transaction t : list) {
System.out.print(t.id + " ");
}
System.out.println();
}
}
}
public void findKSum(int k, int target) {
ArrayList<Integer> current = new ArrayList<Integer>();
findKSumHelper(0, k, target, current);
}
public void findKSumHelper(int start, int k, int target, ArrayList<Integer> current) {
if (k == 0 && target == 0) {
System.out.println(current);
return;
}
if (k == 0 || target < 0) {
return;
}
for (int i = start; i < transactions.size(); i++) {
Transaction t = transactions.get(i);
current.add(t.id);
findKSumHelper(i + 1, k - 1, target - t.amount, current);
current.remove(current.size() - 1);
}
}
}
public class Problem9_TwoSumFinancialTransactions {
public static void main(String[] args) {
TransactionAnalyzer analyzer = new TransactionAnalyzer();
analyzer.addTransaction(new Transaction(1, 500, "StoreA", 600));
analyzer.addTransaction(new Transaction(2, 300, "StoreB", 615));
analyzer.addTransaction(new Transaction(3, 200, "StoreC", 630));
analyzer.addTransaction(new Transaction(4, 500, "StoreA", 640));
analyzer.findTwoSum(500);
analyzer.findTwoSumWithTimeWindow(500, 60);
analyzer.detectDuplicates();
analyzer.findKSum(3, 1000);
}
}