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/Greedy/1405.Longest-Happy-String at master · wisdompeak/LeetCode

Expand file tree

/

1405.Longest-Happy-String

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

1405.Longest-Happy-String

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
1405.Longest-Happy-String.cpp
1405.Longest-Happy-String.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

1405.Longest-Happy-String

本题就是 984.String-Without-AAA-or-BBB 的升级版,只不过包含了三种字符。

数据结构不变:就是按照频次从大到小塞进优先队列。算法的核心思想:

  1. 每个回合取频次最高的两种字符,即若干个A和一个B,目的是保证两个回合不会有相同的字符邻接在一起。

  2. 取A的个数x取决于当前A的频次比B的频次多多少,最多取两个。即 x = min(2, freq(A)-freq(B)). 目的是为了使得剩余的字符的频次尽量趋同。可以想象,如果剩下的字符频次都相同,那么只要不停输出abcabc...即可。

当队列里只有一种字符的时候,最多只能再输出该种字符的两个。

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.