文章目录
- [2148. 元素计数](https://leetcode-cn.com/problems/count-elements-with-strictly-smaller-and-greater-elements/)
- [2149. 按符号重排数组](https://leetcode-cn.com/problems/rearrange-array-elements-by-sign/)
- [2150. 找出数组中的所有孤独数字](https://leetcode-cn.com/problems/find-all-lonely-numbers-in-the-array/)
- [2151. 基于陈述统计最多好人数](https://leetcode-cn.com/problems/maximum-good-people-based-on-statements/)
- 总结
上周的周赛,还是挺简单的
2148. 元素计数
只需要保证【最小】<x<【最大】就可以了
考虑到数据量1 <= nums.length <= 100
,可以直接写成一行,否则需要存一下min和max
class Solution:
def countElements(self, nums: List[int]) -> int:
return sum(min(nums) < x < max(nums) for x in nums)
2149. 按符号重排数组
筛选+拼接,可以学习下chain和zip的用法
class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
return [*chain(*zip((x for x in nums if x > 0), (x for x in nums if x < 0)))]
2150. 找出数组中的所有孤独数字
Counter直接就过了
class Solution:
def findLonely(self, nums: List[int]) -> List[int]:
cnt = Counter(nums)
return [i for i in cnt.keys() if not cnt[i+1] and not cnt[i-1] and cnt[i]==1]
2151. 基于陈述统计最多好人数
二进制枚举,2行解法
class Solution:
def maximumGood(self, statements: List[List[int]]) -> int:
cnt=lambda i:0 if any((i >> j) & 1 and st < 2 and st != (i >> k) & 1 for j, row in enumerate(statements) for k, st in enumerate(row)) else bin(i).count('1')
return max(cnt(i) for i in range(1, 1 << len(statements)))
偶然看到的一个大神的真·一行解法