调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
中等
相关标签
相关企业
提示

给你一个 正整数 数组 nums

你需要从数组中选出一个满足下述条件的

  • 你可以将选中的元素放置在一个下标从 0 开始的数组中,并使其遵循以下模式:[x, x2, x4, ..., xk/2, xk, xk/2, ..., x4, x2, x]注意k 可以是任何 非负 的 2 的幂)。例如,[2, 4, 16, 4, 2][3, 9, 3] 都符合这一模式,而 [2, 4, 8, 4, 2] 则不符合。

返回满足这些条件的子集中,元素数量的 最大值

 

示例 1:

输入:nums = [5,4,1,2,2]
输出:3
解释:选择子集 {4,2,2} ,将其放在数组 [2,4,2] 中,它遵循该模式,且 22 == 4 。因此答案是 3 。

示例 2:

输入:nums = [1,3,2,4]
输出:1
解释:选择子集 {1},将其放在数组 [1] 中,它遵循该模式。因此答案是 1 。注意我们也可以选择子集 {2} 、{4} 或 {3} ,可能存在多个子集都能得到相同的答案。

 

提示:

  • 2 <= nums.length <= 105
  • 1 <= nums[i] <= 109
通过次数
5.5K
提交次数
20.3K
通过率
27.1%


相关企业

提示 1
We can select an odd number of 1’s.

提示 2
Put all the values into a HashSet. We can start from each x > 1 as the smallest chosen value and we can find the longest subset by checking the new values (which are the square of the previous value) in the set by brute force.

提示 3
Note when x > 1, x2, x4, x8, … increases very fast, the longest subset with smallest value x cannot be very long. (The length is O(log(log(109))).

提示 4
Hence we can directly check all lengths less than 10 for all values of x.

相似题目

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
nums =
[5,4,1,2,2]
Source