How many numbers are smaller than the current number

















Given an array nums, for one element nums[I], count all the numbers in the array smaller than it.
In other words m, for each nums[I] you must calculate the number of valid js, where j satisfies j! = I and nums[j] < nums[I]
Return the answer as an array.








In this case, the data scale is relatively small, and the double cycle of violence can be solved.










Rank teams by vote

















There is now a special ranking system in which teams are ranked according to the order in the voters’ minds, and each voter has to rank all the teams in the ranking from the highest to the lowest.
The ranking rules are as follows:
Teams are ranked according to the number of “number one” tickets they receive. If there is a tie between multiple teams, the number of “second place” votes will continue to be considered. And so on until there is no juxtaposition.
If a tie still occurs after all the votes are considered, the teams are ranked in alphabetical order.
You are given a string array of votes representing the ranking given by all the voters. Please rank all the teams according to the above ranking rules.
Return a string that represents the ranking of all teams sorted by the ranking system.








In this case, the following three points are mainly investigated:

  • Map data structures in JavaScript

  • Basic operations on JavaScript arrays

  • Multi-conditional sort of JavaScript sort method

First, you need to iterate through the votes array to count all the teams ranked.

Since there are a maximum of 26 teams, you need to prioritize the teams according to the number of the 26 rankings. If the 26 rankings are consistent, then you need to compare the alphabetical order of the teams.

Finally, the join method in JavaScript returns the ranking string of the participating teams.










5346. Lists in binary trees

















You are given a binary tree with root as the root and a linked list with head as the first node.
Return True if there is a path down in the binary tree and each point corresponds exactly to the value of each node in the list headed by head, otherwise return False.
A straight down path is a path that starts at a node in the tree and continues down.








This problem mainly involves two data structures:

  • The list

  • Binary tree

We can start from the root node of any subtree in the binary tree. Therefore, we need to record the root node of all subtrees in the binary tree first, which is actually traversing the binary tree operation. Generally, we use recursive processing idea.

After obtaining the child root node, traversing the linked list can verify whether there is a path down all the way. Traversing the linked list is completed by iterating the next pointer.










The minimum cost of an efficient path

















Give you an m by N grid. Each cell in the grid has a number that corresponds to the direction of the next step from that cell. The number in grid[I][j] may have the following situations:
1. The next step is to go right, so you will go from grid[I][j] to grid[I][j + 1].
2. The next step is to go left, so you will go from grid[I][j] to Grid [I][J-1].
The next step is to go from grid[I][j] to grid[I + 1][j].
4. Next step up, you will go from grid[I][j] to Grid [i-1][j].
Note that there may be invalid numbers in the grid diagram because they may point to areas outside the grid.
At the beginning, you start at the top left corner of the grid (0,0). We define a valid path as one that starts on the grid (0,0) and follows each step in the direction of the corresponding number until it ends on the lower-right grid (m-1, n-1). A valid path does not need to be a shortest path.
You can change the numbers in a cell for cost = 1, but you can only change the numbers in each cell once.
Return the minimum cost of having at least one valid path to the grid graph.








This is a graph theory problem type.

If you follow the direction of the arrow, the weight of the one-way side is 0, otherwise the weight of the one-way side is 1.

Since there is no negative weight problem, this problem can be solved by Dijkstra.

However, the best solution is: BFS and double-ended queue (0-1BFS).

The detailed process is recommended to see the official solution directly. The JavaScript implementation code is given below.

Since JavaScript does not have a data structure for double-endian queues, arrays can be used to simulate the characteristics of double-endian queues.

However, the cost of unshift and shift operations on JavaScript arrays is very high (see V8 array implementation source code for details). Here, we can simulate the characteristics of double-ended queues based on a single linked list to reduce the time complexity of queue head operations.










Highlights from the past






  • LeetCode Journey for front End Engineers — 173 weeks

  • Front End Engineer’s Journey to LeetCode – 177 weeks

  • LeetCode Tour for Front-end Engineers – Meow night (20)

  • The front End Engineer’s LeetCode Journey — Binary Tree Easy

  • LeetCode journey for front End Engineers – Linked List Easy

  • JavaScript AC solutions to problems on LeetCode