Skip to content

Commit ceddd99

Browse files
committed
2024.05 Optimize row fixer
1 parent 64cc88e commit ceddd99

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

2024/5-printer/main.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,20 @@ func isValidRow(ints []int, rules [][2]int) bool {
7171

7272
func fixInvalidRow(ints []int, rules [][2]int) []int {
7373
outer:
74-
for !isValidRow(ints, rules) {
75-
position := map[int]int{}
76-
for i := range ints {
77-
position[ints[i]] = i
78-
}
74+
position := map[int]int{}
75+
for i := range ints {
76+
position[ints[i]] = i
77+
}
7978

80-
var pos1, pos2 int
81-
var ok1, ok2 bool
82-
for i := range rules {
83-
pos1, ok1 = position[rules[i][0]]
84-
pos2, ok2 = position[rules[i][1]]
79+
var pos1, pos2 int
80+
var ok1, ok2 bool
81+
for i := range rules {
82+
pos1, ok1 = position[rules[i][0]]
83+
pos2, ok2 = position[rules[i][1]]
8584

86-
if ok1 && ok2 && pos1 >= pos2 {
87-
ints[pos1], ints[pos2] = ints[pos2], ints[pos1]
88-
continue outer
89-
}
85+
if ok1 && ok2 && pos1 >= pos2 {
86+
ints[pos1], ints[pos2] = ints[pos2], ints[pos1]
87+
goto outer
9088
}
9189
}
9290

0 commit comments

Comments
 (0)