forked from shellfly/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (43 loc) · 1.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/******************************************************************************
* Execution: go run cmd/topm/main.go
* Data files: https://algs4.cs.princeton.edu/24pq/tinyBatch.txt
*
* Given an integer m from the command line and an input stream where
* each line contains a String and a long value, this MinPQ client
* prints the m lines whose numbers are the highest.
*
* % go run cmd/topm/main.go 5 < tinyBatch.txt
* Thompson 2/27/2000 4747.08
* vonNeumann 2/12/1994 4732.35
* vonNeumann 1/11/1999 4409.74
* Hoare 8/18/1992 4381.21
* vonNeumann 3/26/2002 4121.85
*
******************************************************************************/
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"github.com/shellfly/algo/algs4"
)
func main() {
m, _ := strconv.Atoi(os.Args[1])
pq := algs4.NewMinPQ()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
pq.Insert(*algs4.NewTransaction(line))
if pq.Size() > m {
pq.DelMin()
}
}
s := algs4.NewStack()
for !pq.IsEmpty() {
s.Push(pq.DelMin())
}
for !s.IsEmpty() {
fmt.Println(s.Pop())
}
}