This problem is cool as we need using several pointers to solve the problem with memory complexity of O(1).

Here I am explaining everything in one figure.

C++

`class Solution {`

public:

ListNode *detectCycle(ListNode *head) {…

This problem is cool as we need using several pointers to solve the problem with memory complexity of O(1).

Here I am explaining everything in one figure.

C++

`class Solution {`

public:

ListNode *detectCycle(ListNode *head) {…

This problem looks hard at the beginning, however, if you read it carefully and figure our all possible things that can define the unique states, you will find the solution.

- A’s location, B’ location, A + B ‘s current value
- A and B’s moving directions
- Special case where a and…

2115. Find All Possible Recipes from Given Supplies

- If we find a new recipe, then add it to the supplies.
- Repeat this process until no new recipe can be found.

Time complexity:

The worst case each time we find one recipe. The total complexity will be

O(sizeof(recipes)*sizeof(recipes)*sizeof( ingredient)) = O(100³)…

For this problem, we can put a separate after the position i or not put a separate. In total, we will have 2^(n-1) cases to put the separators.

We can solve it by using

- backtrack
- bit manipulation

The time complexity is:

O(n * 2^n ) where O(n)…

This is the 4th problem of this week’s LC weekly contest.

We can change the problem to find the LIS for each subsequence with index of

- 0, k, 2k, …
- 1, k+1, k + 1 + k,…

A well know problem of longest increasing or non decreasing subsqeuence problem. By…

For the problem listed here, it is quite interesting as we can:

- use the diameter of a tree template to solve it
- use the topological sort to solve this one.

Here is my observations:

- The result at most have two nodes. 1) when the longest path’s…

3 steps

`apt remove cmake`

pip install cmake --upgrade

sudo ln /usr/local/bin/cmake /usr/bin/cmake

You can use the following command to check the cmake version

`cmake --version`

952. Largest Component Size by Common Factor

Union Find is a natural way to combine different sets with connection into one set. So the basic idea is:

- If two numbers have common factor, union them
- During the union process, maintain a set size as the final answer is asking for…

This week’s (weekly 268)last question can be solved by checking all the possible numbers in a brute force way. In order to construct all the possible candidates, we can use backtracking based on standard approaches. If you are using python, luckily, we can use itertools.porduct…

2076. Process Restricted Friend Requests

Are you sure you really master the UF data structure/algorithm? If you are, you can use this question to verify your skills of using UF. This is the 4th question of the LC weekly 267 contest. It is pretty fun.

- basic UF
- + maintain members…