Skip to content

Commit b801031

Browse files
committed
feat: add solutions to lc problem: No.1169
No.1169.Invalid Transactions
1 parent a16dd4b commit b801031

File tree

6 files changed

+423
-2
lines changed

6 files changed

+423
-2
lines changed

solution/1100-1199/1169.Invalid Transactions/README.md

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,173 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:哈希表 + 模拟**
60+
61+
遍历交易列表,对于每笔交易,如果金额大于 1000,或者同名且城市不同且时间间隔不超过 60 分钟,则将其加入答案。
62+
63+
具体地,我们使用哈希表 `d` 记录每个交易,其中键为交易名称,值为一个列表,列表中的每个元素为一个三元组 `(time, city, index)`,表示在 `time` 时刻在 `city` 城市进行了编号为 `index` 的交易。同时,我们使用哈希表 `idx` 记录答案中的交易编号。
64+
65+
遍历交易列表,对于每笔交易,我们首先将其加入哈希表 `d` 中,然后判断其金额是否大于 1000,如果是,则将其编号加入答案中。然后我们遍历哈希表 `d` 中的交易,如果交易名称相同且城市不同且时间间隔不超过 60 分钟,则将其编号加入答案中。
66+
67+
最后,我们遍历答案中的交易编号,将其对应的交易加入答案即可。
68+
69+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为交易列表的长度。
70+
5971
<!-- tabs:start -->
6072

6173
### **Python3**
6274

6375
<!-- 这里可写当前语言的特殊实现逻辑 -->
6476

6577
```python
66-
78+
class Solution:
79+
def invalidTransactions(self, transactions: List[str]) -> List[str]:
80+
d = defaultdict(list)
81+
idx = set()
82+
for i, x in enumerate(transactions):
83+
name, time, amount, city = x.split(",")
84+
time, amount = int(time), int(amount)
85+
d[name].append((time, city, i))
86+
if amount > 1000:
87+
idx.add(i)
88+
for t, c, j in d[name]:
89+
if c != city and abs(time - t) <= 60:
90+
idx.add(i)
91+
idx.add(j)
92+
return [transactions[i] for i in idx]
6793
```
6894

6995
### **Java**
7096

7197
<!-- 这里可写当前语言的特殊实现逻辑 -->
7298

7399
```java
100+
class Solution {
101+
public List<String> invalidTransactions(String[] transactions) {
102+
Map<String, List<Item>> d = new HashMap<>();
103+
Set<Integer> idx = new HashSet<>();
104+
for (int i = 0; i < transactions.length; ++i) {
105+
var e = transactions[i].split(",");
106+
String name = e[0];
107+
int time = Integer.parseInt(e[1]);
108+
int amount = Integer.parseInt(e[2]);
109+
String city = e[3];
110+
d.computeIfAbsent(name, k -> new ArrayList<>()).add(new Item(time, city, i));
111+
if (amount > 1000) {
112+
idx.add(i);
113+
}
114+
for (Item item : d.get(name)) {
115+
if (!city.equals(item.city) && Math.abs(time - item.t) <= 60) {
116+
idx.add(i);
117+
idx.add(item.i);
118+
}
119+
}
120+
}
121+
List<String> ans = new ArrayList<>();
122+
for (int i : idx) {
123+
ans.add(transactions[i]);
124+
}
125+
return ans;
126+
}
127+
}
128+
129+
class Item {
130+
int t;
131+
String city;
132+
int i;
133+
134+
Item(int t, String city, int i) {
135+
this.t = t;
136+
this.city = city;
137+
this.i = i;
138+
}
139+
}
140+
```
141+
142+
### **C++**
143+
144+
```cpp
145+
class Solution {
146+
public:
147+
vector<string> invalidTransactions(vector<string>& transactions) {
148+
unordered_map<string, vector<tuple<int, string, int>>> d;
149+
unordered_set<int> idx;
150+
for (int i = 0; i < transactions.size(); ++i) {
151+
vector<string> e = split(transactions[i], ',');
152+
string name = e[0];
153+
int time = stoi(e[1]);
154+
int amount = stoi(e[2]);
155+
string city = e[3];
156+
d[name].push_back({time, city, i});
157+
if (amount > 1000) {
158+
idx.insert(i);
159+
}
160+
for (auto& [t, c, j] : d[name]) {
161+
if (c != city && abs(time - t) <= 60) {
162+
idx.insert(i);
163+
idx.insert(j);
164+
}
165+
}
166+
}
167+
vector<string> ans;
168+
for (int i : idx) {
169+
ans.emplace_back(transactions[i]);
170+
}
171+
return ans;
172+
}
173+
174+
vector<string> split(string& s, char delim) {
175+
stringstream ss(s);
176+
string item;
177+
vector<string> res;
178+
while (getline(ss, item, delim)) {
179+
res.emplace_back(item);
180+
}
181+
return res;
182+
}
183+
};
184+
```
74185

186+
### **Go**
187+
188+
```go
189+
func invalidTransactions(transactions []string) (ans []string) {
190+
d := map[string][]tuple{}
191+
idx := map[int]bool{}
192+
for i, x := range transactions {
193+
e := strings.Split(x, ",")
194+
name := e[0]
195+
time, _ := strconv.Atoi(e[1])
196+
amount, _ := strconv.Atoi(e[2])
197+
city := e[3]
198+
d[name] = append(d[name], tuple{time, city, i})
199+
if amount > 1000 {
200+
idx[i] = true
201+
}
202+
for _, item := range d[name] {
203+
if city != item.city && abs(time-item.t) <= 60 {
204+
idx[i], idx[item.i] = true, true
205+
}
206+
}
207+
}
208+
for i := range idx {
209+
ans = append(ans, transactions[i])
210+
}
211+
return
212+
}
213+
214+
func abs(x int) int {
215+
if x < 0 {
216+
return -x
217+
}
218+
return x
219+
}
220+
221+
type tuple struct {
222+
t int
223+
city string
224+
i int
225+
}
75226
```
76227

77228
### **...**

solution/1100-1199/1169.Invalid Transactions/README_EN.md

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,152 @@
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def invalidTransactions(self, transactions: List[str]) -> List[str]:
60+
d = defaultdict(list)
61+
idx = set()
62+
for i, x in enumerate(transactions):
63+
name, time, amount, city = x.split(",")
64+
time, amount = int(time), int(amount)
65+
d[name].append((time, city, i))
66+
if amount > 1000:
67+
idx.add(i)
68+
for t, c, j in d[name]:
69+
if c != city and abs(time - t) <= 60:
70+
idx.add(i)
71+
idx.add(j)
72+
return [transactions[i] for i in idx]
5973
```
6074

6175
### **Java**
6276

6377
```java
78+
class Solution {
79+
public List<String> invalidTransactions(String[] transactions) {
80+
Map<String, List<Item>> d = new HashMap<>();
81+
Set<Integer> idx = new HashSet<>();
82+
for (int i = 0; i < transactions.length; ++i) {
83+
var e = transactions[i].split(",");
84+
String name = e[0];
85+
int time = Integer.parseInt(e[1]);
86+
int amount = Integer.parseInt(e[2]);
87+
String city = e[3];
88+
d.computeIfAbsent(name, k -> new ArrayList<>()).add(new Item(time, city, i));
89+
if (amount > 1000) {
90+
idx.add(i);
91+
}
92+
for (Item item : d.get(name)) {
93+
if (!city.equals(item.city) && Math.abs(time - item.t) <= 60) {
94+
idx.add(i);
95+
idx.add(item.i);
96+
}
97+
}
98+
}
99+
List<String> ans = new ArrayList<>();
100+
for (int i : idx) {
101+
ans.add(transactions[i]);
102+
}
103+
return ans;
104+
}
105+
}
106+
107+
class Item {
108+
int t;
109+
String city;
110+
int i;
111+
112+
Item(int t, String city, int i) {
113+
this.t = t;
114+
this.city = city;
115+
this.i = i;
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
vector<string> invalidTransactions(vector<string>& transactions) {
126+
unordered_map<string, vector<tuple<int, string, int>>> d;
127+
unordered_set<int> idx;
128+
for (int i = 0; i < transactions.size(); ++i) {
129+
vector<string> e = split(transactions[i], ',');
130+
string name = e[0];
131+
int time = stoi(e[1]);
132+
int amount = stoi(e[2]);
133+
string city = e[3];
134+
d[name].push_back({time, city, i});
135+
if (amount > 1000) {
136+
idx.insert(i);
137+
}
138+
for (auto& [t, c, j] : d[name]) {
139+
if (c != city && abs(time - t) <= 60) {
140+
idx.insert(i);
141+
idx.insert(j);
142+
}
143+
}
144+
}
145+
vector<string> ans;
146+
for (int i : idx) {
147+
ans.emplace_back(transactions[i]);
148+
}
149+
return ans;
150+
}
151+
152+
vector<string> split(string& s, char delim) {
153+
stringstream ss(s);
154+
string item;
155+
vector<string> res;
156+
while (getline(ss, item, delim)) {
157+
res.emplace_back(item);
158+
}
159+
return res;
160+
}
161+
};
162+
```
64163

164+
### **Go**
165+
166+
```go
167+
func invalidTransactions(transactions []string) (ans []string) {
168+
d := map[string][]tuple{}
169+
idx := map[int]bool{}
170+
for i, x := range transactions {
171+
e := strings.Split(x, ",")
172+
name := e[0]
173+
time, _ := strconv.Atoi(e[1])
174+
amount, _ := strconv.Atoi(e[2])
175+
city := e[3]
176+
d[name] = append(d[name], tuple{time, city, i})
177+
if amount > 1000 {
178+
idx[i] = true
179+
}
180+
for _, item := range d[name] {
181+
if city != item.city && abs(time-item.t) <= 60 {
182+
idx[i], idx[item.i] = true, true
183+
}
184+
}
185+
}
186+
for i := range idx {
187+
ans = append(ans, transactions[i])
188+
}
189+
return
190+
}
191+
192+
func abs(x int) int {
193+
if x < 0 {
194+
return -x
195+
}
196+
return x
197+
}
198+
199+
type tuple struct {
200+
t int
201+
city string
202+
i int
203+
}
65204
```
66205

67206
### **...**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
vector<string> invalidTransactions(vector<string>& transactions) {
4+
unordered_map<string, vector<tuple<int, string, int>>> d;
5+
unordered_set<int> idx;
6+
for (int i = 0; i < transactions.size(); ++i) {
7+
vector<string> e = split(transactions[i], ',');
8+
string name = e[0];
9+
int time = stoi(e[1]);
10+
int amount = stoi(e[2]);
11+
string city = e[3];
12+
d[name].push_back({time, city, i});
13+
if (amount > 1000) {
14+
idx.insert(i);
15+
}
16+
for (auto& [t, c, j] : d[name]) {
17+
if (c != city && abs(time - t) <= 60) {
18+
idx.insert(i);
19+
idx.insert(j);
20+
}
21+
}
22+
}
23+
vector<string> ans;
24+
for (int i : idx) {
25+
ans.emplace_back(transactions[i]);
26+
}
27+
return ans;
28+
}
29+
30+
vector<string> split(string& s, char delim) {
31+
stringstream ss(s);
32+
string item;
33+
vector<string> res;
34+
while (getline(ss, item, delim)) {
35+
res.emplace_back(item);
36+
}
37+
return res;
38+
}
39+
};

0 commit comments

Comments
 (0)