solution 1: sort and choose the middle one
solution 2: hash map
solution 3: voting, lets assume the first is the right candidate, if we see zero count, we assign the candidate, if we see the vote is toward the current canadidate, we add vote count by one, if we see different canadidates, we deduct the value by one. The right answer is the remaining canadidate
solution 1: recursive, if both are null, we return null, if either one of the parameters are existed, we return the one remaining. if they are both existed, we make a new node with the sum of the two parameters' node, and call the main function with theirs lefts and rights
solution 1: check the last binary digit by do
nums & 1
if the result is equal to 1, which mean it was 0 so the result was the previous value added by 1.
if the result is equal to 0, which mean it was shift left by 1 bits, so the value is the same value as the value when you shift the value right by 1 bits
solution 1: recursive
if the parameter is null, we return 0 - end condition
otherwise, we add the max value between paramter.left's max depth and right's node max depth by 1
solution 2: level base count
we create a queue, while the queue is not empty, we are counting (by add 1). we grab every elements in one level and push to the array. delete the previous level node.
for loop in a while loop. we can delete the previous level node by step in the for loop, or we can delete them all together in one go after for loop
-
we use xor, if duplicate, it will become 0
-
math, the double sum of the unique number minus the total value
-
hash map
- recursive: revert the result of the left node and right node
- customize stack: create a stack, push left and right to the stack, pop and resolve until stack is empty
- queue: same as above, just use queue instead of stack.
- iteratively: iterate through the list, on each iteration, build the result with new listNode with current cursor value and previous result.
- iteratively and replace value in-place
- recursively: create a recursive function which takes current node and result as parameters, if current node is null, we return result. if its not null, we form and produce a new listNode with current cursor's value and result as the next node. (in-place is possible, save the right next to a variable)
-
brute force
-
one pass loop, we track the minimum value and the maximum profit
when we see a minimum value appears, we assign it to minimum value, make it a protential day to buy in, otherwise we calculate the profit and see if this is the maximum profit day to sell.
- count zero and delete them, loop and push the zero in the end, attention: when you remove them, the length of the array also change
- we track lastNonZeroIndex, loop through the array, if we see non-zero we assign the current value to the lastNonZeroIndex At and increase it by one. then we append zero at the end
- same logic but we swap, maybe good in c++
convert the number to index. make the indexed number negative to track if the number in the array is existed. use the array as a hashmap. stupid and smart....
- loop, advance one of the list when its current value is less. Javascript, create a dummy ListNode to hold the value
- recurisve
- DFS, the max length which passes through this node is the max diameter value of left plus the right. but the depth for the current node is the max diameter value between left and right plus one
- dynamic programming, its either by using array or by passing addition state parameter into function
- dynaimc programming without array, use two variable to save the only two previous value if needed
- recursive, create a child function which take two node and compare them
- queue, push and pull two value each time.
- one pass hashmap. we search the other value each time we iterate the value in the array. using hashmap. care. duplicated value
- dynamic programming, subquestion
1.dynamic programming
- hash map. use set in javascript
- two pointer, a and b, a advanced one b advance two
- attach B to the end of headA, attach A to the end of headB, do a while loop when they are not curA and curB are not equal
- using stack push when open, check the match when close. make sure the stack is empty at the end
- two pointer, slow advance 1 every time. fast advance 2 every time. create the first half array. iterate two array in parallel and compare the value