Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode 120. Triangle

Problem

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom isĀ 11Ā (i.e.,Ā 2Ā +Ā 3Ā +Ā 5Ā +Ā 1Ā = 11).

Note:

Bonus point if you are able to do this using onlyĀ O(n) extra space, whereĀ nĀ is the total number of rows in the triangle.

Ā 

Solution: DP

Time complexity: O(n^2)

Space complexity: O(1)

C++

花花酱 LeetCode 893. Groups of Special-Equivalent Strings

Problem

You are given an arrayĀ AĀ of strings.

Two stringsĀ SĀ andĀ TĀ areĀ special-equivalentĀ if after any number ofĀ moves, S == T.

AĀ moveĀ consists of choosing two indicesĀ iĀ andĀ jĀ withĀ i % 2 == j % 2, and swappingĀ S[i]Ā withĀ S[j].

Now, aĀ group of special-equivalent strings fromĀ AĀ is aĀ non-empty subset S ofĀ AĀ such that any string not in SĀ is not special-equivalent with any string in S.

Return the number of groups of special-equivalent strings fromĀ A.

Example 1:

Input: ["a","b","c","a","c","c"]
Output: 3
Explanation: 3 groups ["a","a"], ["b"], ["c","c","c"]

Example 2:

Input: ["aa","bb","ab","ba"]
Output: 4
Explanation: 4 groups ["aa"], ["bb"], ["ab"], ["ba"]

Example 3:

Input: ["abc","acb","bac","bca","cab","cba"]
Output: 3
Explanation: 3 groups ["abc","cba"], ["acb","bca"], ["bac","cab"]

Example 4:

Input: ["abcd","cdab","adcb","cbad"]
Output: 1
Explanation: 1 group ["abcd","cdab","adcb","cbad"]

Note:

  • 1 <= A.length <= 1000
  • 1 <= A[i].length <= 20
  • AllĀ A[i]Ā have the same length.
  • AllĀ A[i]Ā consist of only lowercase letters.

Ā 

Solution: Signature

All Special-Equivalent Strings should have the same signature if we sort all the odd-index characters and all the even-index characters.

E.g.Ā [“abcd”,”cdab”,”adcb”,”cbad”] are all in the same graph.

“abcd”: odd “ac”, even: “bd”
“cdab”: odd “ac”, even: “bd”
“adcb”: odd “ac”, even: “bd”
“cbad”: odd “ac”, even: “bd”

We can concatenate the odd and even strings to create a hashable signature.

Time complexity: O(n)

Space complexity: O(n)

C++

Java

Python

Python (1-linear)

花花酱 LeetCode 895. Maximum Frequency Stack

Problem

ImplementĀ FreqStack, a class which simulates the operation of a stack-like data structure.

FreqStackĀ has two functions:

  • push(int x), which pushes an integerĀ xĀ onto the stack.
  • pop(), whichĀ removesĀ and returns the most frequent element in the stack.
    • If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned.

Example 1:

Input: 
["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"],
[[],[5],[7],[5],[7],[4],[5],[],[],[],[]]
Output: [null,null,null,null,null,null,null,5,7,5,4]
Explanation:
After making six .push operations, the stack is [5,7,5,7,4,5] from bottom to top.  Then:

pop() -> returns 5, as 5 is the most frequent.
The stack becomes [5,7,5,7,4].

pop() -> returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top.
The stack becomes [5,7,5,4].

pop() -> returns 5.
The stack becomes [5,7,4].

pop() -> returns 4.
The stack becomes [5,7].

Note:

  • Calls toĀ FreqStack.push(int x)Ā will be such thatĀ 0 <= x <= 10^9.
  • It is guaranteed thatĀ FreqStack.pop()Ā won’t be called if the stack has zero elements.
  • The total number ofĀ FreqStack.pushĀ calls will not exceedĀ 10000Ā in a single test case.
  • The total number ofĀ FreqStack.popĀ calls will not exceedĀ 10000Ā in a single test case.
  • The total number ofĀ FreqStack.pushĀ andĀ FreqStack.popĀ calls will not exceedĀ 150000Ā across all test cases.

Ā 

Solution 1: Buckets

We have nĀ  stacks. The i-th stack has the of elements with freq i when pushed.

We keep tracking the freq of each element.

push(x): stacks[++freq(x)].push(x)Ā  # inc x’s freq and push it onto freq-th stack

pop(): x = stacks[max_freq].pop(), –freq(x); # pop element x from the max_freq stack and dec it’s freq.

Time complexity: O(1) push / pop

Space complexity: O(n)

C++

Solution2: Priority Queue

Use a max heap with key: (freq, seq), the max freq and closest to the top of stack element will be extracted first.

Time complexity: O(logn)

Space complexity: O(n)

C++

Related Problems

花花酱 LeetCode 892. Surface Area of 3D Shapes

Problem

On aĀ NĀ *Ā NĀ grid, we place someĀ 1 * 1 * 1Ā cubes.

Each valueĀ v = grid[i][j]Ā represents a tower ofĀ vĀ cubes placed on top of grid cellĀ (i, j).

Return the total surface area of the resulting shapes.

Example 1:

Input: [[2]]
Output: 10

Example 2:

Input: [[1,2],[3,4]]
Output: 34

Example 3:

Input: [[1,0],[0,2]]
Output: 16

Example 4:

Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 32

Example 5:

Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 46

Note:

  • 1 <= N <= 50
  • 0 <= grid[i][j] <= 50

Ā 

Solution: Geometry

3D version ofĀ čŠ±čŠ±é…± LeetCode 463. Island Perimeter

Ans = total faces – hidden faces.

each pile with height h has the surface area of 2 (top/bottom) + 4*h (sides)

\(ans = ans + 2 + 4 * h\)

if a cube has a neighbour, reduce the total surface area by 1

For each pile, we check 4 neighbours, the number of total hidden faces of this pile is sum(min(h, neighbour’s h)).

\(ans = ans – \Sigma min(h, n.h)\)

Time complexity: O(mn)

Space complexity: O(1)

C++

C++ (opt)

Since the neighbor relationship is symmetric, we can only consider the top and left neighbors and double the hidden faces.

花花酱 LeetCode 894. All Possible Full Binary Trees

Problem

AĀ full binary treeĀ is a binary tree where each node has exactly 0 or 2Ā children.

Return a list of all possible full binary trees withĀ NĀ nodes.Ā  Each element of the answer is the root node of one possible tree.

EachĀ nodeĀ of eachĀ tree in the answerĀ mustĀ haveĀ node.val = 0.

You may return the final list of trees in any order.

Example 1:

Input: 7
Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
Explanation:

 

Note:

  • 1 <= N <= 20

Ā 

Solution: Recursion

Key observations:

  1. n must be odd, If n is even, no possible trees
  2. ans is the cartesian product of trees(i) and trees(n-i-1). Ans = {Tree(0, l, r) for l, r in trees(i) X trees(N – i – 1)}.

w/o cache

C++

Java

Python

w/cache

C++

Python

using itertools.product w/ cache

DP

C++

Benchmark

No-cache vs cached

C++