包含给定元素的最长排序子数组
我最近申请了一份前端开发工作,测试问题之一是编写一个接受整数数组和整数 0 <= n
的函数。 arr.length
并返回包含 array[n] 的最长排序子数组。并且该函数必须是递归的。
示例:
array = [3, 1, 4, 7, 9, -3]
function(array, 0) => [3]
function(array, 1) => [1,4,7,9]
function(array, 2) => [1,4,7,9]
function(array, 3) => [1,4,7,9]
function(array, 4) => [1,4,7,9]
function(array, 5) => [-3]
我有 30 分钟的时间,但无法想出答案。
I applied recently to a Front-End Dev job and one of the test questions was to write a function that takes in an array of integers and an integer 0 <= n < arr.length
and returns the longest sorted subarray that contains array[n]
. And the function has to be recursive.
Example:
array = [3, 1, 4, 7, 9, -3]
function(array, 0) => [3]
function(array, 1) => [1,4,7,9]
function(array, 2) => [1,4,7,9]
function(array, 3) => [1,4,7,9]
function(array, 4) => [1,4,7,9]
function(array, 5) => [-3]
I had 30 minutes and couldn't come up with an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我第一次真正的工作测试,我压力太大了,我看不到它!
感谢@kcsquared 的想法,我只需要火花来点燃这个想法!
It was my first ever real job test and I was so stressed that I couldn't see it!!!!
Thanks @kcsquared for the idea, I just needed the spark to ignite the idea!