Tag: algorithm-problems

• Blind 75 - Invert Binary Tree

Swap left and right. Then repeat for those nodes.

• Blind 75 - Linked List Cycle

Have a fast pointer and a slow pointer. fast increases by 2. slow increases by 1. if both become the same then the cycle exists. if fast one reaches end then no cycle exists.

• Blind 75 - Maximum Depth of Binary Tree

go to each depth, incrementing a depth counter by 1. return max of it.

• Blind 75 - Merge Two-Sorted Lists

Iterate over both and store minimum one in current; Increment in the list from where minimum came. Once one of the lists is empty, empty the other one.

• Blind 75 - Number of 1 Bits

Check if the last bit is 1 by (n&1). Do the right unsigned shift (logical shift >>>). Loop.

• Blind 75 - Reverse Bits

Get the last bit of value from the original. append to the reverse one. shift reverse one to the left. instead of adding, you can do `or` because after the shift last value will be 0. Imp - iterate 32 times only.

• Blind 75 - Reverse LinkedList

Similar to swapping two numbers. Start with prev as null and current and keep swapping next. Return prev because that's the last value.

Placeholder

• Blind 75 - Subtree of Another Tree

Check if root is same as subtree or root.left is same or root.right is same;

• Blind 75 - Best Time To Buy And Sell Stock

Make sure the left pointer is always the lowest yet. Keep checking the difference between left and right. And return max.

• Blind 75 - Valid Palindrome

Have 2 pointers. One at the start other at the end. Skip if `!Character.isLetterOrDigit` and compare with `Character.toLowerCase`

• Blind 75 - Valid Parentheses

God-level solution - iterate.<br>If an opening bracket is found, put the closing bracket in a stack.<br>If the closing bracket is found, pop and check for equality.

• Blind 75 - Product of Array Except Self

Loop twice.<br>first right to left while multiplying.<br>Then second left to right while multiplying.<br>This can be done in a single array and one extra product variable

• Blind 75 - Contains Duplicate

Create a set and put items in one by one while checking if `num` is already there.

• Blind 75 - Group Anagram

compare the count of letters in each word and put group them in a map

• Blind 75 - Top K Frequent Elements - IMP

count all numbers. sort. and return top k. Use streams to make it cleaner. or use bucket sort.

• Blind 75 - Valid Anagrams

check if the length is the same.<br> find the difference in the count of letters in both numbers.<br> since there are only 26 letters, you can count in a single int array of 26. <br>index = char - 'a'

• Blind 75 Two Sums

Make a map of item and index. Check if target - currentItem exists.