int** generate(int numRows, int** columnSizes, int* returnSize) {
int **q, i, j;
*returnSize = numRows;
q = (int **) malloc (sizeof(int *) * numRows);
for (i = 0; i < numRows; i++) {
q[i] = (int *) malloc (sizeof(int) * (i + 1));
q[i][0] = 1;
q[i][i] = 1;
}
*columnSizes = (int *) malloc (sizeof(int) * numRows);
for (i = 0; i < numRows; i++)
(*columnSizes)[i] = i + 1;
i = 2;
while (i < numRows) {
j = 1;
while (j < i) {
q[i][j] = q[i  1][j  1] + q[i  1][j];
j++;
}
i++;
}
return q;
}
Vgn
@Vgn
Posts made by Vgn

Simple C solution

RE: Most space efficient in C solutions
Good approach. You can further avoid static allocation for the individual arrays by storing in a temp variable the required string and calculating it's length. Below is the snippet with modifications
... char temp[40]; //Assuming a 64 bit int, the string cannot be more than 40 chars. ... if (i == j) //both start and end of range are same i,e only one element in the range sprintf(temp, "%d", nums[i]); else sprintf(temp, "%d>%d", nums[i], nums[j]); //Below part of the code is brought out of the above ifelse block as it is common. q = (char **) realloc (q, sizeof(char *) * *returnSize); //reallocate with extra rows q[*returnSize  1] = (char *) malloc (sizeof(char) * (strlen(temp) + 1)); //size of array is length of temp + 1 for null character strcpy(q[*returnSize  1], temp); ...

RE: O(n) algorithm using Dictionary in Python
Thanks for your comments.
We can not avoid 'if nums[i] in ar' and by the way this is not done in O(n) time. We are searching for a key in Dictionary and that is O(1) time.
This holds good even if we use 'set'.

RE: [Python Solution question] Incorrect answer on the site but correct answer in Python Shell
You are creating a new (local) list instead of modifying existing list.
Try this:
nums[:] = nums[len(nums)k:] + nums[:len(nums)k]

RE: What's wrong with my java answer?
The logic seems wrong.

When one element in left stack is same as element in right stack, you are just returning 'true'. You should make sure that all the elements are checked and both the stacks are empty before returning true.

Also, you should handle the input like this: (({}()))
Left stack will be (({(
Right stack will be })))
Your logic doesn't work but the sequence is valid.


RE: Why is my DFS solution wrong? Array index out of bounds line 14
Change the sequence of operands in below lines and give a try.
Instead of
if(visited[i][j] == true && i >= 0 && j >= 0) res = count[i][j]; if(visited[i][j] == false && i >= 0 && j >= 0)
Try
if(i >= 0 && j >= 0 && visited[i][j] == true) res = count[i][j]; if(i >= 0 && j >= 0 && visited[i][j] == false)
This makes sure that only if i and j are valid you access 'visited' array

RE: O(n) algorithm using Dictionary in Python
Could you please elaborate ? How does that make change ?