Algorithm Revising Binary search via 4 interesting problems 1. Search in Rotated Sorted Array Let's formalize the problem: we need to find a number in array A[n]: A[0 .. i] is strictly increasing A[i+1 .. n-1] is strictly increasing

Algorithm LC 716, 155: Stack design's variant 1. Min Stack Use 2 stacks: - s: to store value of designing stack - m: to store maximum value so far e.g: s = 4, 2, 3, 1, 9, 0 m = 4,

Algorithm A note on Detecting cycle - Floyd algorithm Problem Detect the cycle in a Linked list, return the starting Node of the cycle if any, otherwise return NULL. E.g: return Node 2 in following linked list: Algorithm Let fast and

Algorithm Graph Algorithms for Coding Interview Apart from the well-known BFS and DFS algorithms, I find the following algorithms useful for Coding Interview.Topology sort & circle check Given directed graph, sort the nodes in graph so that, if

Algorithm A note on an interesting technique: Range addition Assume you have an array of length n initialized with all 0's and are given k update operations. Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of

Algorithm LC 683: K Empty Slots https://leetcode.com/problems/k-empty-slots/Solution We can come up with a naive solution: let a is array store the state of bulks, a[i] = 1/0 ie bulk i is on/off

Algorithm LC 855: Exam Room https://leetcode.com/problems/exam-room/Solution For each new student, there are 3 cases: seat at 0: distance = distance to first seated student seat at n-1: distance = distance to last seated student seat

Algorithm LC 5: Longest Palindromic Substring https://leetcode.com/problems/longest-palindromic-substring/Solution Dynamic programming: bool fx[i][j] = true if s[i..j] is palindromic else false fx[i][i] = true fx[i][i+1] = (s[i] == s[i+

Algorithm LC 206: Reverse Linked List https://leetcode.com/problems/reverse-linked-list/Solution Let prev and cur be 2 continuous items of the list: prev -> cur We cannot just assign cur->next = prev, because we will lose

Algorithm A note on a technique of Two Pointers algorithm The pattern of question can be solved by Two Pointers algorithms is Return the length of the longest contiguous subarray [u,v] of array A that [u,v] meets the requirement X. With