Press "Enter" to skip to content

Posts tagged as “counting”

花花酱 LeetCode 790. Domino and Tromino Tiling

题目大意:有两种不同形状的骨牌(1×2长条形,L型)无限多块。给你一个2xN的板子,问一共有多少不同的方式可以完全覆盖。

We have two types of tiles: a 2×1 domino shape, and an “L” tromino shape. These shapes may be rotated.

Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 7.

(In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.)

 

Idea: DP

dp[i][0]: ways to cover i cols, both rows of i-th col are covered
dp[i][1]:  ways to cover i cols, only top row of i-th col is covered
dp[i][2]:  ways to cover i cols, only bottom row of i-th col is covered

Solution 1: DP

Time complexity: O(N)

Space complexity: O(N)

C++

C++ V2

Solution 2: DP

Another way to think about this problem

define: dp[i] ways to completely covert the i*2 board.

C++

花花酱 LeetCode 377. Combination Sum IV

Problem:

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

题目大意:给你一些数和一个目标值,问使用这些数(每个数可以被使用任意次数)加起来等于目标值的组合(其实是不重复的排列)有多少种。

Idea:

DP

Solution:

C++ / Recursion + Memorization

 

C++ / DP

Related Problems:

花花酱 LeetCode 730. Count Different Palindromic Subsequences

Problem:

Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.

A subsequence of a string S is obtained by deleting 0 or more characters from S.

A sequence is palindromic if it is equal to the sequence reversed.

Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.

Example 1:

Input: 
S = 'bccb'
Output: 6
Explanation: 
The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Note that 'bcb' is counted only once, even though it occurs twice.

Example 2:

Input: 
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation: 
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.

Note:

 

  • The length of S will be in the range [1, 1000].
  • Each character S[i] will be in the set {'a', 'b', 'c', 'd'}.



Idea:

DP

 

Solution 1: Recursion with memoization

Time complexity: O(n^2)
Space complexity: O(n^2)

C++

Python

Java

Solution 2: DP

C++

Pyhton

Related Problems:

花花酱 LeetCode 639. Decode Ways II

Problem:

A message containing letters from A-Z is being encoded to numbers using the following mapping way:

Beyond that, now the encoded string can also contain the character ‘*’, which can be treated as one of the numbers from 1 to 9.

Given the encoded message containing digits and the character ‘*’, return the total number of ways to decode it.

Also, since the answer may be very large, you should return the output mod 109 + 7.

Example 1:

Example 2:

Note:

  1. The length of the input string will fit in range [1, 105].
  2. The input string will only contain the character ‘*’ and digits ‘0’ – ‘9’.

Idea:

DP

Time complexity: O(n)

Space complexity: O(1)

Solution:

C++

 

Related Problems:

花花酱 LeetCode 91. Decode Ways

题目大意:

给你一个加密的数字字符串,问你一共有多少种不同的解密方式。

Problem:

A message containing letters from A-Z is being encoded to numbers using the following mapping:

‘A’ -> 1

‘B’ -> 2

‘Z’ -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

The number of ways decoding "12" is 2.

 



Idea:

Dynamic Programming

Solution:

C++

Time complexity: O(n^2)

Space complexity: O(n^2)

 

C++

Time complexity: O(n)

Space complexity: O(n)

 

 

 

C++

Time complexity: O(n)

Space complexity: O(1)

 

Related Problems: