summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go41
1 files changed, 10 insertions, 31 deletions
diff --git a/main.go b/main.go
index bfbdc7c..d475ac3 100644
--- a/main.go
+++ b/main.go
@@ -6,39 +6,25 @@ import (
"bufio"
"io"
"os"
- "runtime/pprof"
"strconv"
"strings"
)
-type List struct {
- Data []int
- Map map[int]int
-}
+type List map[int]int
-func (l *List) Insert(n int) {
- l.Data = append(l.Data, n)
- l.Map[n]++
+func (l List) Insert(n int) {
+ l[n]++
}
-func (l *List) Delete(n int) {
- for i, v := range l.Data {
- if v == n {
- l.Data = append(l.Data[:i], l.Data[i+1:]...)
- switch l.Map[n] {
- case 1:
- delete(l.Map, n)
- default:
- l.Map[n]--
- }
- return
- }
+func (l List) Delete(n int) {
+ if l[n]--; l[n] <= 0 {
+ delete(l, n)
}
}
func (l List) IsHomo() bool {
var top int
- for _, v := range l.Map {
+ for _, v := range l {
if v > top {
top = v
}
@@ -47,7 +33,7 @@ func (l List) IsHomo() bool {
}
func (l List) IsHetero() bool {
- return len(l.Map) > 1
+ return len(l) > 1
}
func Split(s string) (string, int) {
@@ -72,13 +58,9 @@ func (l List) String() string {
func Homo(r io.Reader, w io.Writer) {
scanner := bufio.NewScanner(r)
- scanner.Scan()
- n, _ := strconv.Atoi(scanner.Text())
+ scanner.Scan() // eat # of cases
- l := List{
- Data: make([]int, 0, n),
- Map: make(map[int]int),
- }
+ l := make(List)
for scanner.Scan() {
s, n := Split(scanner.Text())
@@ -94,8 +76,5 @@ func Homo(r io.Reader, w io.Writer) {
}
func main() {
- f, _ := os.Create("cpu.out")
- pprof.StartCPUProfile(f)
- defer pprof.StopCPUProfile()
Homo(os.Stdin, os.Stdout)
}