Skip to content

Commit fcb2aec

Browse files
aQuaaQua
authored andcommitted
759 wrong answer
1 parent eed341c commit fcb2aec

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

Algorithms/0759.set-intersection-size-at-least-two/set-intersection-size-at-least-two.go

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,24 @@ func intersectionSizeTwo(intervals [][]int) int {
1313
})
1414

1515
n := len(intervals)
16-
lastA := intervals[n-1][0]
17-
18-
hasNum := make(map[int]bool, len(intervals)*2)
19-
hasNum[lastA] = true
20-
hasNum[lastA+1] = true
21-
22-
for i := 0; i < n-1; i++ {
23-
update(hasNum, intervals[i])
24-
}
25-
26-
return len(hasNum)
27-
}
28-
29-
func update(hasNum map[int]bool, interval []int) {
30-
countDown := 2
31-
a, b := interval[0], interval[1]
32-
for i := a; i <= b && countDown > 0; i++ {
33-
if hasNum[i] {
34-
countDown--
35-
} else {
36-
break
16+
hasNum := make(map[int]bool, n*2)
17+
18+
interSect := intervals[0]
19+
for i := 1; i < n; i++ {
20+
if interSect[1] <= intervals[i][0] {
21+
hasNum[interSect[1]] = true
22+
hasNum[interSect[1]-1] = true
23+
interSect = intervals[i]
24+
continue
3725
}
38-
}
3926

40-
for countDown > 0 {
41-
hasNum[b] = true
42-
b--
43-
countDown--
27+
interSect[0] = intervals[i][0]
28+
if intervals[i][1] < interSect[1] {
29+
interSect[1] = intervals[i][1]
30+
}
4431
}
32+
hasNum[interSect[0]] = true
33+
hasNum[interSect[0]+1] = true
4534

46-
return
35+
return len(hasNum)
4736
}

Algorithms/0759.set-intersection-size-at-least-two/set-intersection-size-at-least-two_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var tcs = []struct {
1313
ans int
1414
}{
1515

16+
{
17+
[][]int{{4, 14}, {6, 17}, {7, 14}, {14, 21}, {4, 7}},
18+
4,
19+
},
20+
1621
{
1722
[][]int{{2, 10}, {3, 7}, {3, 15}, {4, 11}, {6, 12}, {6, 16}, {7, 8}, {7, 11}, {7, 15}, {11, 12}},
1823
5,

0 commit comments

Comments
 (0)