由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道题(7)
相关主题
one amazon interview problem给定字符串,求其不出现重复字符的子字符串的最大长度
MMD, 死在了longest contiguous increasing sub-array上了.(已解决,code错了) online judge 有的时候会有点小bug吗?
Longest subarray with equal number of 1 and 0热腾腾的twitter电面经
Linkedin八月onsite面经leetcode online judge Longest Palindromic Substring memory limit exceeded
Google onsite 题目求助T第二轮面经
好挫的F家面经 Memory Limit Exceeded: Longest Palindromic Substring
找连续最长子数组使得总和小于等于一定值Leetcode- Longest Substring Without Repeating Characters 的 test case
问个MSFT的题求助一道 Longest Common Substring 的变形面试题
相关话题的讨论汇总
话题: int话题: onecount话题: zerocount话题: max话题: arr
进入JobHunting版参与讨论
1 (共1页)
f*********d
发帖数: 140
1
Longest subarray with equal number of 1 and 0
c**1
发帖数: 71
2
array. arr[x] is the first offset you see x more 1 than 0. note x can be
negative so you need to allocate an array of 2 * input_size and set [0] at
the middle of the array.
scan input, each step get how many more 1 than 0 you have so far. and if
the corresponding arr item is already initialized, length is the diff of
two offset. max of all such length is what you want.
f*********d
发帖数: 140
3
Not totally understand. Could you please give a simple example? Thanks a lot!

be

【在 c**1 的大作中提到】
: array. arr[x] is the first offset you see x more 1 than 0. note x can be
: negative so you need to allocate an array of 2 * input_size and set [0] at
: the middle of the array.
: scan input, each step get how many more 1 than 0 you have so far. and if
: the corresponding arr item is already initialized, length is the diff of
: two offset. max of all such length is what you want.

d*k
发帖数: 207
4
smart!

be

【在 c**1 的大作中提到】
: array. arr[x] is the first offset you see x more 1 than 0. note x can be
: negative so you need to allocate an array of 2 * input_size and set [0] at
: the middle of the array.
: scan input, each step get how many more 1 than 0 you have so far. and if
: the corresponding arr item is already initialized, length is the diff of
: two offset. max of all such length is what you want.

f*********d
发帖数: 140
5
Got it. Really Nice Idea!
Just being confused by the words~

be

【在 c**1 的大作中提到】
: array. arr[x] is the first offset you see x more 1 than 0. note x can be
: negative so you need to allocate an array of 2 * input_size and set [0] at
: the middle of the array.
: scan input, each step get how many more 1 than 0 you have so far. and if
: the corresponding arr item is already initialized, length is the diff of
: two offset. max of all such length is what you want.

s****p
发帖数: 124
6
You meant to convert 0 to -1, right? Otherwise, it won't work, because for i
< j, if there are same numbers of 1s and 0s between them, C[i] and C[j] won
't map to the same bucket, because C[j] = C[i] + (j-i)/2.
s***5
发帖数: 2136
7
O(n) space, O(n) time.
bool equal_bits(bool input[], int n, int& sta, int& end) {
// check if all elements are all 1's or all 0's
int sum = 0;
for(int i = 0; i < n; i++)
sum += input[i];
if(sum == 0 || sum == n) return false;
unordered_map freq;
int counter = 0, maxL = 0;
freq[counter] = -1;
for(int i = 0; i < n; i++) {
if(input[i])
counter++;
else
counter--;
if(freq.count(counter) == 0)
freq[counter] = i;
else {
sta = freq[counter]+1;
end = i;
maxL = max(maxL,i-freq[counter]);
}
}
return true;
}
f*********d
发帖数: 140
8
MARK
THANKS

【在 s***5 的大作中提到】
: O(n) space, O(n) time.
: bool equal_bits(bool input[], int n, int& sta, int& end) {
: // check if all elements are all 1's or all 0's
: int sum = 0;
: for(int i = 0; i < n; i++)
: sum += input[i];
: if(sum == 0 || sum == n) return false;
: unordered_map freq;
: int counter = 0, maxL = 0;
: freq[counter] = -1;

m********c
发帖数: 105
9
mark
g***9
发帖数: 159
10
遵照大牛的思路写了个实现:
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int getMaxEvenSeq(vector &v) {
int n = v.size();
if (n < 2) {
return 0;
}
vector pos(2*n+1, -1);
int zeros, ones;
int i, t, diff, index, dis, ans;
zeros = ones = 0;
ans = 0;
for (i = 0; i < n; i++) {
t = v[i];
if (t == 0) {
zeros++;
}
else {
ones++;
}
diff = ones - zeros;
index = diff;
if (index < 0) {
index = (-index) + n;
}
if (pos[index] != -1) {
dis = i - pos[index];
ans = std::max(ans, dis);
}
else {
pos[index] = i;
}
}
return ans;
}
void Test() {
//int a[] = {1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0};
int a[] = {0, 0, 0, 1, 0, 0, 0};
vector v(a, a+sizeof(a)/sizeof(int));
int maxlen = getMaxEvenSeq(v);
cout << maxlen << endl;
}
int main() {
Test();
return 0;
}
相关主题
好挫的F家面经给定字符串,求其不出现重复字符的子字符串的最大长度
找连续最长子数组使得总和小于等于一定值(已解决,code错了) online judge 有的时候会有点小bug吗?
问个MSFT的题热腾腾的twitter电面经
进入JobHunting版参与讨论
r*c
发帖数: 167
11
这是随手写来的吧,牛啊!
unordered_map freq; --> unordered_map freq;

【在 s***5 的大作中提到】
: O(n) space, O(n) time.
: bool equal_bits(bool input[], int n, int& sta, int& end) {
: // check if all elements are all 1's or all 0's
: int sum = 0;
: for(int i = 0; i < n; i++)
: sum += input[i];
: if(sum == 0 || sum == n) return false;
: unordered_map freq;
: int counter = 0, maxL = 0;
: freq[counter] = -1;

s**x
发帖数: 7506
12
found this
http://www.dsalgo.com/2013/03/longest-subarray-with-equal-numbe
Longest subarray with equal number of ones and zeros
Problem
You are given an array of 1's and 0's only. Find the longest subarray which
contains equal number of 1's and 0's.
Solution
We will keep a running sum of "no of ones minus no of zeros" for each index
of the array. For any two indices, if the running sum is equal, that means
there are equal number of ones and zeros between these two indices. We will
store the running sum in an array such that it acts like a hash map where
key is the running sum and value is the leftmost index of that running sum
to appear. The running sum can vary from -n to +n. So we need an array of
length 2*n+1. So for any index we can check whether this sum has occurred
before and if yes what is the left most index for it. We can do this in O(1)
time by maintaining the array. So at any index we can find the longest
equal subarray till that point. We will compare it with a maximum value and
update the maximum accordingly. So the overall complexity for the process is
O(n)
Code
public class MaxSubarrayEqualOnesZeros
{
public static void main(String[] args)
{
int[] arr =
{ 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1,
0, 1, 1, 0, 1, 0, 0, 0 };
printMaxSubarray(arr);
}
private static void printMaxSubarray(int[] arr)
{
Integer[] diffMap = new Integer[arr.length * 2 + 1];
diffMap[arr.length] = -1;
int sum = 0;
int maxLength = 0;
int maxStart = -1;
int maxEnd = -1;
for (int i = 0; i < arr.length; ++i)
{
if (arr[i] == 0)
sum -= 1;
else
sum += 1;
Integer prevIndex = diffMap[arr.length + sum];
if (prevIndex == null)
diffMap[arr.length + sum] = i;
else
{
if (i - prevIndex > maxLength)
{
maxLength = i - prevIndex;
maxStart = prevIndex + 1;
maxEnd = i;
}
}
}
System.out.println("indices (" + maxStart + "," + maxEnd + ")");
System.out.println("length=" + maxLength);
}
}
c**1
发帖数: 71
13
in leetcode, there is one question asking for longest substring with matched
parens. it is similar to this post but harder.
d****n
发帖数: 233
14
void printMaxSubarray(int[] arr)
{
int[] zeroPos = new int[arr.Length + 1];
int[] onePos = new int[arr.Length + 1];
int zeroCount = 0;
int oneCount = 0;
int start = 0;
int max = 0;
for (int i = 0; i < arr.Length; ++i)
{
if (arr[i] == 1)
{
++oneCount;
onePos[oneCount] = i;
if (oneCount <= zeroCount)
{
max = oneCount;
start = zeroPos[zeroCount - oneCount];

}
}
else
{
++zeroCount;
zeroPos[zeroCount] = i;
if (zeroCount <= oneCount)
{
max = zeroCount;
start = onePos[oneCount-zeroCount];
}
}
}
Console.WriteLine("indices (" + start + "," + (2 * max -1)+ ")");
Console.WriteLine("Length=" + 2*max);
}

which
index
will

【在 s**x 的大作中提到】
: found this
: http://www.dsalgo.com/2013/03/longest-subarray-with-equal-numbe
: Longest subarray with equal number of ones and zeros
: Problem
: You are given an array of 1's and 0's only. Find the longest subarray which
: contains equal number of 1's and 0's.
: Solution
: We will keep a running sum of "no of ones minus no of zeros" for each index
: of the array. For any two indices, if the running sum is equal, that means
: there are equal number of ones and zeros between these two indices. We will

d****n
发帖数: 233
15
void printMaxSubarray2(int[] arr)
{
Stack stack = new Stack();
int max = 0;
int start = 0;
for (int i = 0; i < arr.Length; ++i)
{
if (stack.Count > 0 && (arr[stack.Peek()] + arr[i]) == 1)
{
stack.Pop();
start = stack.Count > 0 ? stack.Peek() : -1;
max = Math.Max(max, i - start);
}
else
{
stack.Push(i);
}

}
Console.WriteLine("indices (" + (start+1) + "," + ( max - 1) + "
)");
Console.WriteLine("Length=" + max);
}

【在 d****n 的大作中提到】
: void printMaxSubarray(int[] arr)
: {
: int[] zeroPos = new int[arr.Length + 1];
: int[] onePos = new int[arr.Length + 1];
: int zeroCount = 0;
: int oneCount = 0;
: int start = 0;
: int max = 0;
: for (int i = 0; i < arr.Length; ++i)
: {

l**********1
发帖数: 415
16
mark
1 (共1页)
进入JobHunting版参与讨论
相关主题
求助一道 Longest Common Substring 的变形面试题Google onsite 题目求助
请教:这个10来行的leetcode程序有什么问题?好挫的F家面经
有人同看Longest Palindromic Substring 这道题么?找连续最长子数组使得总和小于等于一定值
python搞不定Longest Palindromic Substring啊问个MSFT的题
one amazon interview problem给定字符串,求其不出现重复字符的子字符串的最大长度
MMD, 死在了longest contiguous increasing sub-array上了.(已解决,code错了) online judge 有的时候会有点小bug吗?
Longest subarray with equal number of 1 and 0热腾腾的twitter电面经
Linkedin八月onsite面经leetcode online judge Longest Palindromic Substring memory limit exceeded
相关话题的讨论汇总
话题: int话题: onecount话题: zerocount话题: max话题: arr