# Recent Posts

### LeetCode 456. 132 Pattern

Problem Description: Given a sequence of n integers a1, a2, …, an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list. Example: input: [1,2,3,4] output: False explanation: There is no 132 pattern in the sequence. input: [3,1,4,2] output: True explanation: There is a…

### LeetCode 749. Contain Virus (TLE)

Problem Description: A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. The world is modeled as a 2-D array of cells, where 0 represents uninfected cells, and 1 represents cells contaminated with the virus. A wall (and only one wall) can be installed between any two 4-directionally adjacent cells, on the shared boundary. Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. Resources…

### LeetCode 528. Random Pick with Weight

Problem Description: Given an array w of positive integers, where w[i] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight. Note: 1 <= w.length <= 10000 1 <= w[i] <= 10^5 pickIndex will be called at most 10000 times. Example: input: [“Solution”,”pickIndex”] [[[1]],[]] output: [null,0] input: [“Solution”,”pickIndex”,”pickIndex”,”pickIndex”,”pickIndex”,”pickIndex”] [[[1,3]],[],[],[],[],[]] output: [null,0,1,1,1,0]

### LeetCode 37. Sudoku Solver

Problem Description: Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column. Each of the the digits 1-9 must occur exactly once in each of the 9 3×3 sub-boxes of the grid. Empty cells are indicated by the character ‘.’.

### LeetCode 547. Friend Circles

Problem Description: There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends. Given a N*N matrix M representing the friend relationship between students in the…

### LeetCode 712. Minimum ASCII Delete Sum for Two Strings

Problem Description: Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. Example: input: s1 = “sea”, s2 = “eat” output: 231 explanation: Deleting “s” from “sea” adds the ASCII value of “s” (115) to the sum. input: s1 = “delete”, s2 = “leet” output: 403 explanation: Deleting “dee” from “delete” to turn the string into “let”, adds 100[d]+101[e]+101[e] to the sum. Deleting “e” from “leet” adds 101[e] to the sum….

### LeetCode 794. Valid Tic-Tac-Toe State

Problem Description: A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game. The board is a 3 x 3 array, and consists of characters ” “, “X”, and “O”. The ” ” character represents an empty square. Here are the rules of Tic-Tac-Toe: Players take turns placing characters into empty squares (” “). The first player always places…

### LeetCode 797. All Paths From Source to Target

Problem Description: Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and return them in any order. The graph is given as follows: the nodes are 0, 1, …, graph.length – 1. graph[i] is a list of all nodes j for which the edge (i, j) exists. Example: input: [[1,2], [3], [3], []] output: [[0,1,3],[0,2,3]] explanation: The graph looks like this: There are two paths: 0 -> 1 -> 3 and 0 -> 2…

### LeetCode 129. Sum Root to Leaf Numbers

Problem Description: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Example: input: output: 25 explanation: The root-to-leaf path 1->2 represents the number 12. The root-to-leaf path 1->3 represents the number 13. Therefore, sum = 12 + 13 = 25. input: output: 1026 explanation: The root-to-leaf path 4->9->5 represents the…