-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def findLucky(self, arr: List[int]) -> int: | ||
d = {} | ||
for x in arr: | ||
d[x] = d.get(x, 0) + 1 | ||
|
||
max_res = -1 | ||
for k,v in d.items(): | ||
if k==v and max_res < k: | ||
max_res = k | ||
return max_res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
可以直接调用 collections 模块 | ||
count = collections.Counter(arr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution: | ||
def numTeams(self, rating: List[int]) -> int: | ||
res = 0 | ||
for n in (0, 1): | ||
length = len(rating) | ||
for i in range(0, length-2): | ||
for j in range(i+1, length-1): | ||
for k in range(j+1, length): | ||
if n == 0 and rating[i] < rating[j] < rating[k]: | ||
res+=1 | ||
elif n == 1 and rating[i] > rating[j] > rating[k]: | ||
res+=1 | ||
return res | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
* Solution | ||
** 三重循环 O(n^3) | ||
** 二分 | ||
#+BEGIN_SRC python | ||
class Solution: | ||
def numTeams(self, rating: List[int]) -> int: | ||
res = 0 | ||
for rating2 in [rating, rating[::-1]]: | ||
d = [] | ||
d2 = [] | ||
for val in rating2: | ||
ind = bisect.bisect_right(d, val) | ||
d.insert(ind, val) | ||
d2.insert(ind, ind) | ||
res += sum(d2[:ind]) | ||
|
||
return res | ||
#+END_SRC | ||
** DP |