Press "Enter" to skip to content

Posts published in July 2018

花花酱 LeetCode 115. Distinct Subsequences

Problem

Given a stringĀ SĀ and a stringĀ T, count the number of distinct subsequences ofĀ SĀ which equalsĀ T.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,Ā "ACE"Ā is a subsequence ofĀ "ABCDE"Ā whileĀ "AEC"Ā is not).

Example 1:

Input: S = "rabbbit", T = "rabbit"
Output:Ā 3
Explanation:  As shown below, there are 3 ways you can generate "rabbit" from S. (The caret symbol ^ means the chosen letters) 
rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^

Example 2:

Input: S = "babgbag", T = "bag"
Output:Ā 5
Explanation:  As shown below, there are 5 ways you can generate "bag" from S. (The caret symbol ^ means the chosen letters)
babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^

Solution: DP

Time complexity: O(|s| * |t|)

Space complexity: O(|s| * |t|)

C++

Related Problems:

花花酱 LeetCode 836. Rectangle Overlap

Problem

A rectangle isĀ represented as aĀ listĀ [x1, y1, x2, y2], whereĀ (x1, y1)Ā are the coordinates of its bottom-left corner, andĀ (x2,Ā y2)Ā are the coordinates of its top-right corner.

Two rectangles overlap if the area of their intersection is positive.Ā  To be clear, two rectangles that only touch at the corner or edges do not overlap.

Given two (axis-aligned) rectangles, return whetherĀ they overlap.

Example 1:

Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true

Example 2:

Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false

Notes:

  1. Both rectanglesĀ rec1Ā andĀ rec2Ā are lists of 4 integers.
  2. All coordinates in rectangles will be betweenĀ -10^9Ā andĀ 10^9.

Solution: Geometry

Time complexity: O(1)

Space complexity: O(1)

 

花花酱 LeetCode 345. Reverse Vowels of a String

Problem

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = “hello”, return “holle”.

Example 2:
Given s = “leetcode”, return “leotcede”.

Note:
The vowels does not include the letter “y”.

Solution

 

花花酱 LeetCode 501. Find Mode in Binary Search Tree

Problem

Given a binary search tree (BST) with duplicates, find all theĀ mode(s)Ā (the most frequently occurred element) in the given BST.

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keysĀ less than or equal toĀ the node’s key.
  • The right subtree of a node contains only nodes with keysĀ greater than or equal toĀ the node’s key.
  • Both the left and right subtrees must also be binary search trees.

 

For example:
Given BSTĀ [1,null,2,2],

returnĀ [2].

Note:Ā If a tree has more than one mode, you can return them in any order.

Follow up:Ā Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).

Solution1: Recursion w/ extra space

Time complexity: O(n)

Space complexity: O(n)

 

Solution2: Recursion w/o extra space

Two passes. First pass to find the count of the mode, second pass to collect all the modes.

Time complexity: O(n)

Space complexity: O(1)

 

花花酱 LeetCode 427. Construct Quad Tree

Problem

We want to use quad trees to store anĀ N x NĀ boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodesĀ until the values in the region it represents are all the same.

Each node has another two boolean attributes :Ā isLeafĀ andĀ val.Ā isLeafĀ is true if and only if the node is a leaf node. TheĀ valĀ attribute for a leaf node contains the value of the region it represents.

Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:

Given theĀ 8 x 8Ā grid below, we want to construct the corresponding quad tree:

It can be divided according to the definition above:

 

The corresponding quad tree should be as following, where each node is represented as aĀ (isLeaf, val)pair.

For the non-leafĀ nodes,Ā valĀ can be arbitrary, so it is represented asĀ *.

Note:

  1. NĀ is less thanĀ 1000Ā and guaranteened to be a power of 2.
  2. If you want to know more about the quad tree, you can refer to itsĀ wiki.

Solution: Recursion

Time complexity: O(n^2*logn)

Space complexity: O(n^2)

C++

V2

Time complexity: O(n^2)

Space complexity: O(n^2)