Skip to content

Instantly share code, notes, and snippets.

@wangming2046
wangming2046 / Swim in Rising Water
Created February 8, 2018 01:51
Swim in Rising Water
On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j).
Now rain starts to fall. At time t, the depth of the water everywhere is t. You can swim from a square to
another 4-directionally adjacent square if and only if the elevation of both squares individually are at most t.
You can swim infinite distance in zero time. Of course, you must stay within the boundaries of the grid during your swim.
You start at the top left square (0, 0). What is the least time until you can reach the bottom right square (N-1, N-1)?
Example 1:
Input: [[0,2],[1,3]]
Output: 3
@wangming2046
wangming2046 / Sliding Puzzle
Created February 8, 2018 01:43
Sliding Puzzle
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.
A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.
The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].
Given a puzzle board, return the least number of moves required so that the state of the board is solved.
If it is impossible for the state of the board to be solved, return -1.
Examples:
Input: board = [[1,2,3],[4,0,5]]
Output: 1
@wangming2046
wangming2046 / Swap Adjacent in LR String
Created February 5, 2018 02:44
Swap Adjacent in LR String
/*In a string composed of 'L', 'R', and 'X' characters, like "RXXLRXRXL", a move consists of either replacing one occurrence of "XL" with "LX", or replacing one occurrence of "RX" with "XR". Given the starting string start and the ending string end, return True if and only if there exists a sequence of moves to transform one string to the other.
Example:
Input: start = "RXXLRXRXL", end = "XRLXXRRLX"
Output: True
Explanation:
We can transform start to end following these steps:
RXXLRXRXL ->
XRXLRXRXL ->
@wangming2046
wangming2046 / Split BST Analysis.txt
Last active February 5, 2018 01:08
Split BST Analysis
/*Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees where one subtree
has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It's not necessarily the case that the tree contains a node with value V.
Additionally, most of the structure of the original tree should remain. Formally, for any child C with parent P in the original
tree, if they are both in the same subtree after the split, then node C should still have the parent P.
You should output the root TreeNode of both subtrees after splitting, in any order.
Example 1:
@wangming2046
wangming2046 / K-th Symbol in Grammar.txt
Last active February 4, 2018 20:58
K-th Symbol in Grammar
/*On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01,
and each occurrence of 1 with 10.
Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed).*/
/* This is a medium problem in the Leetcode website. At first, since the next row is closely related to the former row, I generate
the next row in a recursive method. And definitely it is subject to Time Limit Exceed. And then I change recursive method with the
following code, but it is still Time Limit Exceed. I check the other people code, and discover they just simple observe the feature
of the output, and it turns out that the answer is just related to the value of K. I will list the one line Java code here, and let
us apprepriate the importance of observation in coding.*/