GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/Math/1641.Count-Sorted-Vowel-Strings at master · wisdompeak/LeetCode

Expand file tree

/

1641.Count-Sorted-Vowel-Strings

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

1641.Count-Sorted-Vowel-Strings

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
1641.Count-Sorted-Vowel-Strings_dp.cpp
1641.Count-Sorted-Vowel-Strings_dp.cpp
 
 
1641.Count-Sorted-Vowel-Strings_math.cpp
1641.Count-Sorted-Vowel-Strings_math.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

Outline

1641.Count-Sorted-Vowel-Strings

解法1: DP

很明显的DP问题。令dp[i][k]表示截止到第i个字符为止,第i个字符是字母k的方案数。根据题意,第i-1个字符必须小于等于k。

for (int j=0; j<=k; j++)
  dp[i][k] += dp[i-1][j];

返回的结果是 sum{dp[n-1][k]} k=0,1,2,3,4

解法2: 数学

我们想象n个球,包括第一个球之前、最后一个球之后,总共n+1个位置。如果任意选出四个位置插入四块板子,那么它们隔出了五个区间。这五个区间其实就对应着一种aeiou的序列:每个区间内球的个数,就代表了对应的字母的个数。举个例子。n=6时,在0,1,2,3,4,5,6里面抽取的是0,2,3,5,那么对应的插板方案就是

| O O| O| O O| O

对应的序列就是eeioou

所以上面的方案里,序列总数是组合数C(n+1,4)。

但是上面的方法有一个问题,那就是除了a和u可以是零个之外,其他的三个区间都至少有一个球。这并不合题意。解决的方案是,除了这n+1个位置,我们还添加三个选项:第1块板和第2块板重合,第2块板和第3块板重合,第3块板和第4块板重合。这样总共有n+4个候选项,依然是随机选择4个。我们发现,如果选到的四个都是隔板的位置,那么就和之前的方案一样。如果选到的是三个是隔板的位置,另外一个是某“重合”选项,我们发现这也能对应一种aeiou的序列。

举个例子。n = 6, 这样有10个候选:0,1,2,3,4,5,6,板12重合,板23重合,板34重合。如果抽到了是3,4,7,板23重合,那么对应的插板方案就是:

 O O O | O || O O |

对应的序列就是 aaaeoo

其他组合方式同理,无论从candiates里面如何抽取四个,都可以翻译为一种合理的插板方案,继而对应为一种合理的字母序列。

所以答案就是C(n+4,4).

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.