Skip to content

Commit 7dc6a65

Browse files
committed
update 645/277
1 parent e555522 commit 7dc6a65

7 files changed

+225
-68
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
175175

176176
## Simulation
177177

178-
| Link | Problem(14) | Solution | Tag | Time | Space | Ref |
178+
| Link | Problem(15) | Solution | Tag | Time | Space | Ref |
179179
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
180180
| [Leetcode-67](https://leetcode.com/problems/add-binary/) | Add Binary | [c++](./leetcode/67.add-binary.cpp), [python3](./leetcode/67.add-binary.py) | Simulation | O\(M\+N\) | O\(1\) | - |
181181
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Simulation | O\(N\) | O\(1\) | - |
@@ -184,6 +184,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
184184
| [Lintcode-849](https://www.lintcode.com/problem/basic-calculator-iii/) | Basic Calculator III | [c++](./lintcode/849.basic-calculator-iii.cpp), [python3](./lintcode/849.basic-calculator-iii.py) | Simulation | O\(N\) | O\(1\) | Leetcode-772 |
185185
| [Lintcode-553](https://www.lintcode.com/problem/bomb-enemy/) | Bomb Enemy | [c++](./lintcode/553.bomb-enemy.cpp), [python3](./lintcode/553.bomb-enemy.py) | Simulation | O\(NM\) | O\(NM\) | Leetcode-361 |
186186
| [Lintcode-366](https://www.lintcode.com/problem/fibonacci/) | Fibonacci | [c++](./lintcode/366.fibonacci.cpp), [python3](./lintcode/366.fibonacci.py) | Simulation | O\(N\) | O\(N\) | - |
187+
| [Lintcode-645](https://www.lintcode.com/problem/find-the-celebrity/) | Find The Celebrity | [c++](./lintcode/645.find-the-celebrity.cpp), [python3](./lintcode/645.find-the-celebrity.py) | Simulation | O\(N\) | O\(N\) | Leetcode-277 |
187188
| [Lintcode-22](https://www.lintcode.com/problem/flatten-list/) | Flatten List | [c++](./lintcode/22.flatten-list.cpp), [python3](./lintcode/22.flatten-list.py) | Simulation | O\(N\) | O\(N\) | - |
188189
| [Lintcode-640](https://www.lintcode.com/problem/one-edit-distance/) | One Edit Distance | [c++](./lintcode/640.one-edit-distance.cpp), [python3](./lintcode/640.one-edit-distance.py) | Simulation | O\(N\) | O\(1\) | Leetcode-161 |
189190
| [Lintcode-3622](https://www.lintcode.com/problem/read-n-characters-given-read4/) | Read N Characters Given Read4 | [c++](./lintcode/3622.read-n-characters-given-read4.cpp), [python3](./lintcode/3622.read-n-characters-given-read4.py) | Simulation | O\(N\) | O\(1\) | Leetcode-157 |
@@ -925,7 +926,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
925926

926927
## Other
927928

928-
| Link | Problem(124) | Solution | Tag | Time | Space | Ref |
929+
| Link | Problem(123) | Solution | Tag | Time | Space | Ref |
929930
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
930931
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
931932
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -960,7 +961,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
960961
| [Leetcode-652](https://leetcode.com/problems/find-duplicate-subtrees/) | Find Duplicate Subtrees | [c++](./leetcode/652.find-duplicate-subtrees.cpp) | Other | \- | \- | - |
961962
| [Leetcode-366](https://leetcode.com/problems/find-leaves-of-binary-tree/) | Find Leaves Of Binary Tree | [c++](./leetcode/366.find-leaves-of-binary-tree.cpp), [python3](./leetcode/366.find-leaves-of-binary-tree.py) | Other | \- | \- | - |
962963
| [Leetcode-724](https://leetcode.com/problems/find-pivot-index/) | Find Pivot Index | [c++](./leetcode/724.find-pivot-index.cpp) | Other | \- | \- | - |
963-
| [Leetcode-277](https://leetcode.com/problems/find-the-celebrity/) | Find The Celebrity | [c++](./leetcode/277.find-the-celebrity.cpp), [python3](./leetcode/277.find-the-celebrity.py) | Other | \- | \- | - |
964964
| [Leetcode-41](https://leetcode.com/problems/first-missing-positive/) | First Missing Positive | [python3](./leetcode/41.first-missing-positive.py) | Other | \- | \- | - |
965965
| [Leetcode-387](https://leetcode.com/problems/first-unique-character-in-a-string/) | First Unique Character In A String | [c++](./leetcode/387.first-unique-character-in-a-string.cpp), [python3](./leetcode/387.first-unique-character-in-a-string.py) | Other | \- | \- | - |
966966
| [Leetcode-251](https://leetcode.com/problems/flatten-2d-vector/) | Flatten 2D Vector | [python3](./leetcode/251.flatten-2d-vector.py) | Other | \- | \- | - |

leetcode/277.find-the-celebrity.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

leetcode/277.find-the-celebrity.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

lintcode/645.find-the-celebrity.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Tag: Simulation
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: Leetcode-277
5+
// Note: -
6+
7+
// Suppose you are at a party with `n` people (labeled from `0` to `n - 1`) and among them, there may exist one celebrity.
8+
// The definition of a celebrity is that all the other `n - 1` people know him/her but he/she does not know any of them.
9+
//
10+
// Now you want to find out who the celebrity is or verify that there is not one.
11+
// The only thing you are allowed to do is to ask questions like: "Hi, A.
12+
// Do you know B?" to get information of whether A knows B.
13+
// You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
14+
//
15+
// You are given a helper function `bool knows(a, b)` which tells you whether A knows B.
16+
// Implement a function `int findCelebrity(n)`, your function should minimize the number of calls to `knows`.
17+
//
18+
// **Example1**
19+
// ```
20+
// Input:
21+
// 2 // next n * (n - 1) lines
22+
// 0 knows 1
23+
// 1 does not know 0
24+
// Output: 1
25+
// Explanation:
26+
// Everyone knows 1,and 1 knows no one.
27+
// ```
28+
// **Example2**
29+
// ```
30+
// Input:
31+
// 3 // next n * (n - 1) lines
32+
// 0 does not know 1
33+
// 0 does not know 2
34+
// 1 knows 0
35+
// 1 does not know 2
36+
// 2 knows 0
37+
// 2 knows 1
38+
// Output: 0
39+
// Explanation:
40+
// Everyone knows 0,and 0 knows no one.
41+
// 0 does not know 1,and 1 knows 0.
42+
// 2 knows everyone,but 1 does not know 2.
43+
// ```
44+
//
45+
// There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return `-1`.
46+
47+
// Forward declaration of the knows API.
48+
bool knows(int a, int b);
49+
50+
class Solution {
51+
public:
52+
/**
53+
* @param n a party with n people
54+
* @return the celebrity's label or -1
55+
*/
56+
int findCelebrity(int n) {
57+
// Write your code here
58+
stack <int> st;
59+
for (int i = 0; i < n; i++) {
60+
st.push(i);
61+
}
62+
63+
while (st.size() > 1) {
64+
int a = st.top();
65+
st.pop();
66+
int b = st.top();
67+
st.pop();
68+
if (knows(a, b)) {
69+
st.push(b);
70+
} else {
71+
st.push(a);
72+
}
73+
}
74+
75+
int celebrity = st.top();
76+
77+
for (auto i = 0; i < n; ++i) {
78+
if (i == celebrity) {
79+
continue;
80+
}
81+
82+
if(!knows(i, celebrity)) {
83+
return -1;
84+
}
85+
}
86+
87+
return celebrity;
88+
}
89+
};
90+
91+
class Solution {
92+
public:
93+
/**
94+
* @param n a party with n people
95+
* @return the celebrity's label or -1
96+
*/
97+
int findCelebrity(int n) {
98+
// Write your code here
99+
int candidate = 0;
100+
for(auto i = 1; i < n; ++i) {
101+
candidate = knows(candidate, i) ? i: candidate;
102+
}
103+
104+
for (auto i = 0; i < n; ++i) {
105+
if (candidate == i) {
106+
continue;
107+
}
108+
109+
if(knows(candidate, i)) {
110+
return -1;
111+
}
112+
113+
if (!knows(i, candidate)) {
114+
return -1;
115+
}
116+
}
117+
118+
return candidate;
119+
}
120+
};

lintcode/645.find-the-celebrity.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Tag: Simulation
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: Leetcode-277
5+
# Note: -
6+
7+
# Suppose you are at a party with `n` people (labeled from `0` to `n - 1`) and among them, there may exist one celebrity.
8+
# The definition of a celebrity is that all the other `n - 1` people know him/her but he/she does not know any of them.
9+
#
10+
# Now you want to find out who the celebrity is or verify that there is not one.
11+
# The only thing you are allowed to do is to ask questions like: "Hi, A.
12+
# Do you know B?" to get information of whether A knows B.
13+
# You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).
14+
#
15+
# You are given a helper function `bool knows(a, b)` which tells you whether A knows B.
16+
# Implement a function `int findCelebrity(n)`, your function should minimize the number of calls to `knows`.
17+
#
18+
# **Example1**
19+
# ```
20+
# Input:
21+
# 2 // next n * (n - 1) lines
22+
# 0 knows 1
23+
# 1 does not know 0
24+
# Output: 1
25+
# Explanation:
26+
# Everyone knows 1,and 1 knows no one.
27+
# ```
28+
# **Example2**
29+
# ```
30+
# Input:
31+
# 3 // next n * (n - 1) lines
32+
# 0 does not know 1
33+
# 0 does not know 2
34+
# 1 knows 0
35+
# 1 does not know 2
36+
# 2 knows 0
37+
# 2 knows 1
38+
# Output: 0
39+
# Explanation:
40+
# Everyone knows 0,and 0 knows no one.
41+
# 0 does not know 1,and 1 knows 0.
42+
# 2 knows everyone,but 1 does not know 2.
43+
# ```
44+
#
45+
# There will be exactly one celebrity if he/she is in the party. Return the celebrity's label if there is a celebrity in the party. If there is no celebrity, return `-1`.
46+
47+
"""
48+
The knows API is already defined for you.
49+
@param a, person a
50+
@param b, person b
51+
@return a boolean, whether a knows b
52+
you can call Celebrity.knows(a, b)
53+
"""
54+
55+
56+
class Solution:
57+
# @param {int} n a party with n people
58+
# @return {int} the celebrity's label or -1
59+
def findCelebrity(self, n):
60+
# Write your code here
61+
stack = [i for i in range(n)]
62+
while len(stack) > 1:
63+
a = stack.pop()
64+
b = stack.pop()
65+
if Celebrity.knows(a, b):
66+
stack.append(b)
67+
else:
68+
stack.append(a)
69+
70+
celebrity = stack[0]
71+
for i in range(n):
72+
if i != celebrity and not Celebrity.knows(i, celebrity):
73+
return -1
74+
75+
return celebrity
76+
77+
78+
class Solution:
79+
# @param {int} n a party with n people
80+
# @return {int} the celebrity's label or -1
81+
def findCelebrity(self, n):
82+
# Write your code here
83+
celebrity = 0
84+
for i in range(1, n):
85+
if Celebrity.knows(celebrity, i):
86+
celebrity = i
87+
88+
for i in range(n):
89+
if i == celebrity:
90+
continue
91+
92+
if Celebrity.knows(celebrity, i):
93+
return -1
94+
95+
if not Celebrity.knows(i, celebrity):
96+
return -1
97+
98+
return celebrity

list/.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"string": "cpp",
1010
"string_view": "cpp",
1111
"unordered_map": "cpp",
12-
"vector": "cpp"
12+
"vector": "cpp",
13+
"queue": "cpp",
14+
"stack": "cpp"
1315
}
1416
}

list/9c-top.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
17. https://leetcode.com/problems/longest-absolute-file-path/
2525
18. https://leetcode.com/problems/roman-to-integer/
2626
19. https://leetcode.com/problems/integer-to-roman/
27-
28-
- https://www.lintcode.com/problem/identify-celebrity/
27+
20. https://www.lintcode.com/problem/identify-celebrity/
2928

3029
### 基础、数据结构
3130

0 commit comments

Comments
 (0)