@jeantimex "Also, I am curious how you are going to "maintain" the window of size k using what data structure (array, deque, priority queue or something else)"  Just an idea, can we simply do a traversal of othe tree (no matter it's inorder or preorder or postorder), and maintain a maxheap of size K, which stores the K nodes values that are closest to the target. This takes O(N), and space is O(K). And this seems has nothing to do with BST, for any BT we can do this. So I kind of agree with other people's opinions that there should be a better algorithm than O(N) as this is BST.
Zhuzeng
@Zhuzeng
Posts made by Zhuzeng

RE: AC clean Java solution using two stacks

RE: 36 ms C++ solution
@StefanPochmann But you do modify it in the code
grid[i][j];
, right? But I also see that your function looks like:shortestDistance(vector<vector<int>> grid)
, which is not the same as the OJ providesshortestDistance(vector<vector<int>>& grid)
. So did you choose to modify the function prototype? I know it's totally OK to do so as the OJ has no idea about this, but just trying to understand the whole story. 
RE: Two solutions. One is DP, the other is greedy (8 lines).
@chenw2000 Hi can you explain the following highlighted code? Thanks!
if (nums[i] < nums[i  1]) {
endsSmall[i] = Math.max(endsSmall[i  1], endsLarge[i  1] + 1);
endsLarge[i] = endsLarge[i  1]; *** Why this?
}Since nums[i] < nums[i1], why endsLarge[i] should = endsLarges[i1]?

Please help... Cannot find the bug in the code
Hi, the code passed 103/104 tests but failed on the last one. I tried running the last test case by manually copying it to the 'Custom Testcase' and clicked 'Run', it gave the correct result (which is
false
), and I also tried running the program on my machine and the answer is alsofalse
).I am aware of this article Note: is Run Code inconsistent with Submit Solution? If you are using global variables or C/C++, check this out, but still I could not find where the bug is. Can any one please help find out why it's producing wrong result for the very last testcase? The program is simply a 01 backpack DP algorithm. Thanks in advance!!!
Test case: [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,100]
class Solution { public: bool canPartition(vector<int>& nums) { if (nums.size() < 2) return false; int sum = accumulate(nums.begin(), nums.end(), 0); if (sum % 2 != 0) return false; // DP[i][j]: can we get j from first i items // DP[i][j] = DP[i1][j]  DP[i1][jnums[i]] // pick nums[i] or not //auto it = minmax_element(nums.begin(), nums.end()); int target = sum / 2; vector<vector<bool>> DP(nums.size() + 1, vector<bool>(target + 1, false)); for (int i = 0; i <= nums.size(); ++i) { DP[i][0] = true; // pick nothing } for (int i = 1; i <= target; ++i) { DP[0][i] = false; } for (int i = 1; i <= nums.size(); ++i) { for (int j = 1; j <= target; ++j) { if (j >= nums[i]) { DP[i][j] = DP[i1][j]  DP[i1][jnums[i]]; } else { DP[i][j] = DP[i1][j]; } } if (DP[i][target]) { return true; } } return DP[nums.size()][target]; } };

Why "++++++++++" is a TRUE?
Can anyone please confirm why the expected result for the following test case is TRUE?
"++++++++++"My code failed because my alg is giving 'false', but I am a little confused why the OJ gives 'TRUE'. No matter what the first player does, the second player can find a way to eventually win the game. Am I missing some critical info here?

RE: Very easy 1 pass Stack Solution in JAVA (NO STRING CONCAT)
Great solution! One little thing, could this handle the case: "(T) ? 4: 5"? I think the code assumes that '?' is right next to 'T' or 'F'?
 nvm, the question says "only consists of digits 09, ?, :, T and F", so no empty spaces, no parenthesis...

RE: C++ easy understood solution (sort)
Nit: there is no need to come up with your own comparator for pair<int, int>. C++ already handles it for you.

RE: Java DFS Solution with Explanation
@shawngao said in Java DFS Solution with Explanation:
length
Hi can you resubmit your code and make sure it's not TLE? Because I tried the same version in C++ and got TLE (138 / 173 test cases passed, so I believe the correctness is OK). Thanks!