Skip to content

Commit deed2af

Browse files
committed
Add solution and test-cases for problem 1169
1 parent f03226b commit deed2af

File tree

3 files changed

+100
-22
lines changed

3 files changed

+100
-22
lines changed

leetcode/1101-1200/1169.Invalid-Transactions/README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1169.Invalid Transactions][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
A transaction is possibly invalid if:
5+
6+
- the amount exceeds `$1000`, or;
7+
- if it occurs within (and including) `60` minutes of another transaction with the **same name** in a **different city**.
8+
9+
You are given an array of strings `transactions` where `transactions[i]` consists of comma-separated values representing the name, time (in minutes), amount, and city of the transaction.
10+
11+
Return a list of `transactions` that are possibly invalid. You may return the answer in **any order**.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]
17+
Output: ["alice,20,800,mtv","alice,50,100,beijing"]
18+
Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Invalid Transactions
23-
```go
2423
```
24+
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]
25+
Output: ["alice,50,1200,mtv"]
26+
```
27+
28+
**Example 3:**
2529

30+
```
31+
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]
32+
Output: ["bob,50,1200,mtv"]
33+
```
2634

2735
## 结语
2836

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,75 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"fmt"
5+
"sort"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type transaction struct {
11+
name string
12+
time, amount int
13+
city string
14+
}
15+
16+
func (tr transaction) String() string {
17+
return fmt.Sprintf("%s,%d,%d,%s", tr.name, tr.time, tr.amount, tr.city)
18+
}
19+
20+
func Solution(transactions []string) []string {
21+
list := make([]transaction, len(transactions))
22+
for i, trans := range transactions {
23+
items := strings.Split(trans, ",")
24+
time, _ := strconv.Atoi(items[1])
25+
amount, _ := strconv.Atoi(items[2])
26+
list[i] = transaction{
27+
name: items[0],
28+
time: time,
29+
amount: amount,
30+
city: items[3],
31+
}
32+
}
33+
var ret []string
34+
sort.Slice(list, func(i, j int) bool {
35+
// 感觉时间排序
36+
a, b := list[i], list[j]
37+
if a.time == b.time {
38+
return a.name < b.name
39+
}
40+
return a.time < b.time
41+
})
42+
43+
group := make(map[string][]transaction)
44+
for _, item := range list {
45+
if _, ok := group[item.name]; !ok {
46+
group[item.name] = make([]transaction, 0)
47+
}
48+
group[item.name] = append(group[item.name], item)
49+
}
50+
for _, trans := range group {
51+
invalid := make([]bool, len(trans))
52+
for i := 0; i < len(trans); i++ {
53+
54+
for pre := i - 1; pre >= 0; pre-- {
55+
if trans[i].city == trans[pre].city {
56+
continue
57+
}
58+
if trans[i].time <= trans[pre].time+60 {
59+
invalid[i] = true
60+
invalid[pre] = true
61+
}
62+
}
63+
if trans[i].amount > 1000 {
64+
invalid[i] = true
65+
continue
66+
}
67+
}
68+
for i := range trans {
69+
if invalid[i] {
70+
ret = append(ret, trans[i].String())
71+
}
72+
}
73+
}
74+
return ret
575
}

leetcode/1101-1200/1169.Invalid-Transactions/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"alice,20,800,mtv", "alice,50,100,beijing"}, []string{"alice,20,800,mtv", "alice,50,100,beijing"}},
17+
{"TestCase2", []string{"alice,20,800,mtv", "alice,50,1200,mtv"}, []string{"alice,50,1200,mtv"}},
18+
{"TestCase3", []string{"alice,20,800,mtv", "bob,50,1200,mtv"}, []string{"bob,50,1200,mtv"}},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)