-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution806.go
More file actions
33 lines (28 loc) · 766 Bytes
/
solution806.go
File metadata and controls
33 lines (28 loc) · 766 Bytes
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
package solution806
// ============================================================================
// 806. Number of Lines To Write String
// URL: https://leetcode.com/problems/number-of-lines-to-write-string
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_numberOfLines-24 70308742 16.61 ns/op 0 B/op 0 allocs/op
PASS
*/
func numberOfLines(widths []int, s string) []int {
lines := 1
pixels := 0
for i := range s {
ch := s[i]
w := widths[int(ch-97)]
if 1 <= pixels+w && pixels+w <= 100 {
pixels += w
} else {
pixels = w
lines++
}
}
return []int{lines, pixels}
}