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 9: Palindrome Number https://leetcode.com/problems/palindrome-number/Solution The easiest way is to convert n to string s, and compare s to the reverse(s) bool isPalindrome(int x) { string s = to_string(s); string

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 3: Longest Substring Without Repeating Characters https://leetcode.com/problems/longest-substring-without-repeating-characters/Solution 2 pointers - snake technique int lengthOfLongestSubstring(string s) { int n = s.size(); if (n == 0) return 0; int res = 1; map<char, int> count;

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 LC 445: Add Two Numbers II https://leetcode.com/problems/add-two-numbers-ii/Different from LC 2, in this problem, the most significant digit comes first in the list. One easy solution is to reverse 2 lists and apply the algorithm

Algorithm LC 2: Add Two Numbers https://leetcode.com/problems/add-two-numbers/Solution As the head of list is the last digit of the number, we can add 2 lists by iterating from heads of 2 lists. The only thing

Algorithm LC 1: Two Sum https://leetcode.com/problems/two-sum/Solution For each item i in the given array, we need to know whether there is an item j (j < i) such that a[i] + a[j]

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