Press "Enter" to skip to content

Posts tagged as “recursion”

花花酱 LeetCode 808. Soup Servings

Problem

There are two types of soup: type A and type B. Initially we haveĀ NĀ ml of each type of soup. There are four kinds of operations:

  1. ServeĀ 100 ml of soup A and 0 ml of soup B
  2. ServeĀ 75 ml of soup A and 25Ā ml of soup B
  3. Serve 50 ml of soup A and 50 ml of soup B
  4. Serve 25Ā ml of soup A and 75Ā ml of soup B

When we serve some soup, we give it to someone and we no longer have it.Ā  Each turn,Ā we will choose from the four operations with equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serveĀ as much as we can.Ā  We stop once we no longer have some quantity of both types of soup.

Note that we do not have the operation where all 100 ml’s of soup B are used first.

Return the probability that soup A will be emptyĀ first, plus half the probability that A and B become empty at the same time.

Example:
Input: N = 50
Output: 0.625
Explanation: 
If we choose the first two operations, A will become empty first. For the third operation, A and B will become empty at the same time. For the fourth operation, B will become empty first. So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.

Notes:

  • 0 <= N <= 10^9.
  • Answers withinĀ 10^-6Ā of the true value will be accepted as correct.

Solution 1: Recursion with Memorization

Time complexity: O(N^2) N ~ 5000 / 25 = 200

Space complexity: O(N^2)

C++

 

花花酱 LeetCode 101. Symmetric Tree

Problem

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary treeĀ [1,2,2,3,4,4,3]Ā is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the followingĀ [1,2,2,null,3,null,3]Ā is not:

    1
   / \
  2   2
   \   \
   3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

Solution: Recursion

Time complexity: O(n)

Space complexity: O(n)

C++

Python

Related Problems

花花酱 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)

 

花花酱 LeetCode 814. Binary Tree Pruning

Problem:

题ē›®å¤§ę„ļ¼šęŠŠäøå«ęœ‰1ēš„节ē‚¹ēš„å­ę ‘å…ØéƒØåˆ é™¤ć€‚

https://leetcode.com/problems/binary-tree-pruning/description/

We are given the head nodeĀ rootĀ of a binary tree, where additionally every node’s value is either a 0 or a 1.

Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.

(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)

Example 1:
Input: [1,null,0,0,1]
Output: [1,null,0,null,1]
 
Explanation: 
Only the red nodes satisfy the property "every subtree not containing a 1".
The diagram on the right represents the answer.


Example 2:
Input: [1,0,1,0,0,0,1]
Output: [1,null,1,null,1]



Example 3:
Input: [1,1,0,1,1,0,1,0]
Output: [1,1,0,1,1,null,1]



Note:

  • The binary treeĀ willĀ haveĀ atĀ mostĀ 100 nodes.
  • The value of each node will only beĀ 0Ā orĀ 1.

Solution: Recursion

Time complexity: O(n)

Space complexity: O(h)

C++

Java

 

Python3

 

花花酱 LeetCode 94. Binary Tree Inorder Traversal

Problem

Given a binary tree, return theĀ inorderĀ traversal of its nodes’ values.

For example:
Given binary treeĀ [1,null,2,3],

   1
    \
     2
    /
   3

returnĀ [1,3,2].

Note:Ā Recursive solution is trivial, could you do it iteratively?

Solution: Recursion

Time complexity: O(n)

Space complexity: O(h)

C++

Solution 2: Iterative