avatar
问个google面试题# JobHunting - 待字闺中
B*1
1
Given an array of strings of 0s and 1s. X and Y are also given. Return the
maximum number of elements in a subset of the array elements which will X
number of zeroes and Y number of 1s when combined. For eg: if array[] = {"01
", "10", "0", "110"} X=3, Y=2
Answer should be 3 since first 3 strings when combined will give the
required number of 0s and 1s.
这题是不是要用dp啊。
avatar
s*j
2
就是二维背包问题吧
f[i][j]表示i个0,j个1的时候,最多用到几个元素,然后顺着推就可以了

01

【在 B*******1 的大作中提到】
: Given an array of strings of 0s and 1s. X and Y are also given. Return the
: maximum number of elements in a subset of the array elements which will X
: number of zeroes and Y number of 1s when combined. For eg: if array[] = {"01
: ", "10", "0", "110"} X=3, Y=2
: Answer should be 3 since first 3 strings when combined will give the
: required number of 0s and 1s.
: 这题是不是要用dp啊。

avatar
g*y
3
对,只要背包了,面试里可解的,最好的多半是DP。

【在 s****j 的大作中提到】
: 就是二维背包问题吧
: f[i][j]表示i个0,j个1的时候,最多用到几个元素,然后顺着推就可以了
:
: 01

avatar
b*c
4
f[i][j][k]表示i个0,j个1的时候,前k个元素 最多用到几个元素
当然f[i][j][k]只跟f[i][j][k-1]相关
avatar
B*1
5
Thanks.

【在 g**********y 的大作中提到】
: 对,只要背包了,面试里可解的,最好的多半是DP。
avatar
c*7
6
这个怎么推啊
f[i][j]又不是f[i-1][j]能确定的

【在 s****j 的大作中提到】
: 就是二维背包问题吧
: f[i][j]表示i个0,j个1的时候,最多用到几个元素,然后顺着推就可以了
:
: 01

avatar
s*n
7
DP公式为
f[i,j,n] = max(f[i,j,n-1], f[i-zeros(n), j-ones(n), n-1] + 1)
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。