注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
我遇到的两题
第二次 而而巴依
一般OA第一题都不难, 我从后往前做的:
perfect set
find the largest size of possible 'perfect' set.
'perfect' is defined as:
1. set contains at least two elements.
2. the elements in the set after sorting satisfies the condition perfect * perfect = perfect[i+1]
if no such set is possible, return -1
Example:
input: [3,9,4,2,16]
some possible perfect set:
[3,9] (size of 2)
[4,2] (size of 2)
[4,16] (size of 2)
[4,2,16] (size of 3) -> largest possible size
return 3
就是找平方根,和平方根的跟,一共有几个, 我从后往前扫,加一个 hashset o(n) 解决
Min days to deliver all parcels
最近社招高频的第一题:
给一堆哑铃, 将最轻的放在最前面, 最重的放在最后面, 求最小次数, 每次只能交换相邻的哑铃
example: 3,2,1 -> 1, 2, 3. 最小步数2 ->2, 3, 1 -> 2, 1, 3, -> 1, 2, 3
我看完以为是排序, 用merge sort做完后发现有test case过不去, debug之后发现是只需要把最轻的放前面, 最重的放后面, 中间并不需要有序.
我用的做法就是扫一遍, 记录最大值最小值index, 然后用头尾减去index, 但要注意如果最小值在最大值后面结果要多减去1.
是给一个整数数组,表示货物的重量。如果i货物重量 < i + 1货物重量,则这两个货物的重量可以merge。
OA2 依然是给一个货物重量的数组和k,然后这个数组中任意一个subarray的重量差(即最重货物减去最轻货物的重量)小于k则可以合并成一个group。
Grocery从数组依次取物,后一堆取的数目要大于前一堆取的数目,问总共能取的最大数目。两道题都是地里报告过的常见题了,直接秒了。
input array。 找出最大的subarray that meets following condition:
sort subarray in order
subarray * subarray = subarray[i+1]
find the largest size of possible 'perfect' set.
'perfect' is defined as:
1. set contains at least two elements.
2. the elements in the set after sorting satisfies the condition perfect * perfect = perfect[i+1]
if no such set is possible, return -1
Example:
input: [3,9,4,2,16]
some possible perfect set:
[3,9] (size of 2)
[4,2] (size of 2)
[4,16] (size of 2)
[4,2,16] (size of 3) -> largest possible size
return 3 |