如题,例子:[0,1,1,1,1,0,0,1], and the result should be [1,1,0,0] 有没有O(n)runtime and O(1) space的solution? 谢谢!
s*i
2 楼
有关注的么?去年BF前sear有个sony 46寸的deal,今年会再来么?
s*e
3 楼
dp题,我的思路,大家看看对不对 O(n)runtime and O(n) space的solution: create an array of length n, len[n], len[i] represent the length of subarray which has equal number of 1 and 0 ending at index i. Initially, set all the element len[] to be 0. len[i] = len[i - 1] + 2 if arr[i] = 1 - arr[i - len[i - 1] - 1] len[i] = 0 otherwise then scan array len[], find out the largest one optimized solution, O(n)runtime and O(1) space的solution: actaully, we do not need to keep array len[] since len[i] only depends on len[i - 1], for each i, if we know len[i - 1] we can get len[i], we need another two variables maxSubarrayLen and maxEndingIndex to record the max length of subarray and the ending index of this subarray respectively
s*n
4 楼
有问题啊:比如01111000,访问最后一个0
s*e
5 楼
恩,原算法有问题,漏掉了这种情况,不过把这种情况考虑进去应该就对了吧,keep ending index of previous subarray, 每次计算以当前index为结束的0,1个数相同 subarray时,判断是否可以跟previous合并,还是O(1)space的
His answer is not too far from being correct. In fact, it'd be much easier if we put -1 at the 0 positions. Then all that we need to do is to get the sum array and do a counting sort. It's still O(n) space though.
【在 m****i 的大作中提到】 : 没看懂什么意思 : 这个如何O(n)time
c*l
22 楼
Isn't the answer 0,1,1,1,1,0,0,1,1,0,0,0,0,1? Did I miss anything here.
// You can run this code in your browser console, e.g. firebug with firefox or chrome console. var a = [0,1,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,1]; var getMaxZeroOneSeq = function(a) { var sum = 0; var max = 0; var map = {0: 0}; // for (var i = 0; i < a.length; i++) { if (a[i] == 1) { sum++; } else { // if (a[i] == 0) sum--; } if (map[sum] == null) { map[sum] = i; } else { max = Math.max(max, i - map[sum] + 1); } } return max; }; getMaxZeroOneSeq(a);
【在 b***e 的大作中提到】 : // You can run this code in your browser console, e.g. firebug with : firefox or chrome console. : var a = [0,1,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,1]; : var getMaxZeroOneSeq = function(a) { : var sum = 0; : var max = 0; : var map = {0: 0}; // : for (var i = 0; i < a.length; i++) { : if (a[i] == 1) { : sum++;
b*e
28 楼
Modified boundary condition.
【在 s******o 的大作中提到】 : for {1,1,0} your method returns 3
如题,例子:[0,1,1,1,1,0,0,1], and the result should be [1,1,0,0] 有没有O(n)runtime and O(1) space的solution? 谢谢!
s*e
31 楼
dp题,我的思路,大家看看对不对 O(n)runtime and O(n) space的solution: create an array of length n, len[n], len[i] represent the length of subarray which has equal number of 1 and 0 ending at index i. Initially, set all the element len[] to be 0. len[i] = len[i - 1] + 2 if arr[i] = 1 - arr[i - len[i - 1] - 1] len[i] = 0 otherwise then scan array len[], find out the largest one optimized solution, O(n)runtime and O(1) space的solution: actaully, we do not need to keep array len[] since len[i] only depends on len[i - 1], for each i, if we know len[i - 1] we can get len[i], we need another two variables maxSubarrayLen and maxEndingIndex to record the max length of subarray and the ending index of this subarray respectively
s*n
32 楼
有问题啊:比如01111000,访问最后一个0
s*e
33 楼
恩,原算法有问题,漏掉了这种情况,不过把这种情况考虑进去应该就对了吧,keep ending index of previous subarray, 每次计算以当前index为结束的0,1个数相同 subarray时,判断是否可以跟previous合并,还是O(1)space的
His answer is not too far from being correct. In fact, it'd be much easier if we put -1 at the 0 positions. Then all that we need to do is to get the sum array and do a counting sort. It's still O(n) space though.
【在 m****i 的大作中提到】 : 没看懂什么意思 : 这个如何O(n)time
c*l
50 楼
Isn't the answer 0,1,1,1,1,0,0,1,1,0,0,0,0,1? Did I miss anything here.
/* You can run this code in your browser console, e.g. firebug with firefox or chrome console. */ var a = [0,1,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,1]; var getMaxZeroOneSeq = function(a) { var sum = 0; var max = 0; var map = {0: -1}; // for (var i = 0; i < a.length; i++) { if (a[i] == 1) { sum++; } else { // if (a[i] == 0) sum--; } if (map[sum] == null) { map[sum] = i; } else { max = Math.max(max, i - map[sum]); } } return max; }; getMaxZeroOneSeq(a);
【在 b***e 的大作中提到】 : // You can run this code in your browser console, e.g. firebug with : firefox or chrome console. : var a = [0,1,1,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,1]; : var getMaxZeroOneSeq = function(a) { : var sum = 0; : var max = 0; : var map = {0: 0}; // : for (var i = 0; i < a.length; i++) { : if (a[i] == 1) { : sum++;
b*e
56 楼
Modified boundary condition.
【在 s******o 的大作中提到】 : for {1,1,0} your method returns 3