This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_rotation_point_test.go
153 lines (138 loc) · 3.11 KB
/
find_rotation_point_test.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Problem:
- Given a list of words, return an index of a rotation point.
Example:
- Input:
[]string{
"ptolemaic",
"retrograde",
"supplant",
"undulate",
"xenoepist",
"asymptote", // <-- rotates here!
"babka",
"banoffee",
"engender",
"karpatka",
"othellolagkage",
},
Output: 5
Approach:
- Use a binary search approach to search for word, to either go right or go
left because they are in order alphabetically.
- Keep track of the lower and upper bounds and so that when they are next
to each other, the floor is the last item while the ceiling is the rotation
point.
Solution:
- Keep track of the lower and upper bounds on the rotation points.
- While is lower bound is less than the upper bound, check if the guessed word
(the one in the middle of the range), comes after the first word.
- If so, go right by making the floor index be the guessed index.
- Otherwise, go left by making the ceiling index be the guessed index.
- When the floor index and ceiling index have converged, the ceiling is
the rotation point.
Cost:
- O(logn) time, O(1) space.
*/
package interviewcake
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestFindRotationPoint(t *testing.T) {
tests := []struct {
in []string
expected int
}{
{
[]string{}, 0,
},
{
[]string{
"xenoepist",
"asymptote", // <-- rotates here!
},
1,
},
{
[]string{
"xenoepist",
"asymptote", // <-- rotates here!
"babka",
"banoffee",
"engender",
"karpatka",
"othellolagkage",
},
1,
},
{
[]string{
"asymptote",
"babka",
"banoffee",
"engender",
"karpatka",
"othellolagkage",
},
5, // returns the last index if there's no rotation
},
{
[]string{
"ptolemaic",
"retrograde",
"supplant",
"undulate",
"xenoepist",
},
4, // returns the last index if there's no rotation
},
{
[]string{
"ptolemaic",
"retrograde",
"supplant",
"undulate",
"xenoepist",
"asymptote", // <-- rotates here!
"babka",
"banoffee",
"engender",
"karpatka",
"othellolagkage",
},
5,
},
}
for _, tt := range tests {
result := findRotationPoint(tt.in)
common.Equal(t, tt.expected, result)
}
}
func findRotationPoint(words []string) int {
// if there is only on word in the list, return 0
if len(words) <= 1 {
return 0
}
// keep track of the lower and upper bounds on the rotation points.
firstWord := words[0]
floorIndex := 0
ceilingIndex := len(words) - 1
for floorIndex < ceilingIndex {
guessIndex := floorIndex + (ceilingIndex-floorIndex)/2
// if the guess word comes after the first word then go right.
// otherwise, go left.
if words[guessIndex] >= firstWord {
floorIndex = guessIndex
} else {
ceilingIndex = guessIndex
}
// when the floorIndex and ceilingIndex have converged, the ceiling is
// the rotation point. it also means that, if there is no rotation
// point, the last index will be returned.
if floorIndex+1 == ceilingIndex {
return ceilingIndex
}
}
return -1
}