We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 336c349 commit 85b8d7cCopy full SHA for 85b8d7c
src/main/java/joshua/leetcode/array/FindCelebrity.java
@@ -1,5 +1,7 @@
1
package joshua.leetcode.array;
2
3
+import joshua.leetcode.solutiontag.TwoPointers;
4
+
5
import java.util.LinkedList;
6
import java.util.List;
7
@@ -93,4 +95,27 @@ public int findCelebrity(int n) {
93
95
return celebrity;
94
96
}
97
98
99
+ @TwoPointers
100
+ public abstract static class Solution2 extends FindCelebrity {
101
102
103
+ @Override
104
+ public int findCelebrity(int n) {
105
+ int l = 0;
106
+ int r = n -1;
107
+ while ( l < r) {
108
+ if (knows(l, r)) ++l;
109
+ else r--;
110
+ }
111
+ for (int i = 0; i < n; i ++) {
112
+ if (i != l) {
113
+ if (knows(i, l) || !knows(l, i)) return -1;
114
115
116
+ return l;
117
118
119
120
121
0 commit comments