From 6000340883ce93af385891913b479597ddf5200d Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 22 Aug 2016 22:43:59 +0200 Subject: Speedup --- main.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 871d4ac..15fc67d 100644 --- a/main.go +++ b/main.go @@ -10,29 +10,32 @@ import ( "strings" ) -type List map[int]int +type List struct { + Map map[int]int + Homo int +} -func (l List) Insert(n int) { - l[n]++ +func (l *List) Insert(n int) { + if l.Map[n]++; l.Map[n] > 1 { + l.Homo++ + } } -func (l List) Delete(n int) { - if l[n]--; l[n] <= 0 { - delete(l, n) +func (l *List) Delete(n int) { + if l.Map[n] > 1 { + l.Homo-- + } + if l.Map[n]--; l.Map[n] <= 0 { + delete(l.Map, n) } } func (l List) IsHomo() bool { - for _, v := range l { - if v > 1 { - return true - } - } - return false + return l.Homo > 0 } func (l List) IsHetero() bool { - return len(l) > 1 + return len(l.Map) > 1 } func Split(s string) (string, int) { @@ -58,7 +61,9 @@ func (l List) Kind() string { func Homo(r io.Reader, w io.Writer) { scanner := bufio.NewScanner(r) scanner.Scan() // eat # of cases - l := make(List) + l := List{ + Map: make(map[int]int), + } for scanner.Scan() { s, n := Split(scanner.Text()) switch s { -- cgit v1.2.3