Posts

Showing posts from September, 2024

Calender 2

Image
  Approach 2: Line Sweep Intuition The previous approach works well for the given problem, where we need to avoid triple bookings. However, if the requirements change such as checking for four overlapping bookings, the method becomes less flexible. We'd need to introduce additional lists, for example, to track triple bookings, making the solution harder to maintain and extend. To address this, we can use a more flexible and standard solution: the Line Sweep algorithm. This approach is common for interval-related problems and can easily handle changes, such as checking for four or more overlapping bookings. The Line Sweep algorithm works by marking when bookings start and end. For each booking (start, end) , we mark the start point by increasing its count by 1 (indicating a booking begins), and we mark the end point by decreasing its count by 1 (indicating a booking ends). These marks are stored in a map, which keeps track of the number of bookings starting or endi...

Count numbers with non repeating digits less than n

  Idea: We can break the problem to two sub-problems: Count special ints with digits smaller than  n . Count special ints with digits the same than  n . Sub-problem 1 is straightforward, we can just use basic combination. Sub-problem 2 is harder, we can further divide it to two subsub-problems. Count special ints with digits the same than  n . Use  345  as an example. Count special ints like  1xx  and  2xx , where  45  is not a limitation here. Count special ints like  3xx , which is the same as special ints smaller than  45  and without digit  3 .

KMP

KMP Pattern matching algorithm String: ababababcd    pattern: ababcd For pattern matching using Naïve method what we do is to compare first character from of both string , if it matches then we move ahead and look for all other characters, if it doesn’t match we move index in main string back to the original character and start the process again. This takes a bit of time as we are recomparing all the charactes. To avoid that we can calculate the shift of pattern at every index so we don’t have to shift the pattern to start, we can shift to last successfully compared character. We basically check how many times the prefix of pattern starting from start is repeating inside the pattern. We compare from start if we find a difference we reset or else we increment. For this we create pi of pattern of   length   m: For(int I = 0 ,num=0;i<m;i++){             If(i!=0 && pattenr[num]==pattern[i]){ ...

BIT operations:

  BIT operations: Terminology: Bit is set: bit value is 1 at that position. The  & (bitwise AND)  takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.   The  | (bitwise OR)  takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.  The  ^ (bitwise XOR)  takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.  The  << (left shift)  takes two numbers, the left shifts the bits of the first operand, and the second operand decides the number of places to shift. Can be used to multiply by 2 power (n) example a<<b means a* 2Power (b).   also a&(1<<n)   will tell whether nth bit is set on a or not. The  >> (righ...