题号+题名+简单思路+code
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
memo=0
ans=-float("inf")
for i in nums:
memo=max(0,memo)+i
if memo>ans:
ans=memo
return ans
进阶:
如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
func lengthOfLIS(nums []int) int {
if len(nums)==0 {
return 0
}
memo:=make([]int,len(nums))
memo[0]=1
ans:=1
for i:=1;i<len(memo);i++ {
tmp:=1
for j:=0;j<i;j++ {
if nums[j]<nums[i] {
if memo[j]+1>tmp {
tmp=memo[j]+1
}
}
}
memo[i]=tmp
if tmp>ans {
ans=tmp
}
}
return ans
}
class Solution:
def climbStairs(self, n: int) -> int:
a=1
b=1
for i in range(2,n+1):
c=a+b
a=b
b=c
return b
class Solution:
def numDecodings(self, s: str) -> int:
if len(s)==1:
if s=="0":
return 0
return 1
if s[0]=="0":
return 0
a=1
tmp=s[:2]
b=0
if int(tmp)<=26 and int(s)>=11 and tmp[1]!="0":
b=2
elif tmp[1]=="0":
if tmp[0]>="3":
return 0
b=1
else:
b=1
t=b
for i in range(2,len(s)):
tmp=s[i-1:i+1]
ti=int(tmp)
if ti<=26 and ti>=11 and tmp[1]!="0":
t=a+b
a=b
b=t
elif ti==0:
return 0
elif tmp[1]=="0":
if tmp[0]>="3":
return 0
t=a
a=b
b=t
else:
a=b
t=b
return t
class Solution:
def translateNum(self, num: int) -> int:
str_num=str(num)
if len(str_num)==0:
return 0
if len(str_num)==1:
return 1
two=1
one=1
for i in range(1, len(str_num)):
tmp=one
if str_num[i-1]=="1" or (str_num[i-1]=="2" and int(str_num[i])<=5): # 约束
one=one+two
two=tmp
return one
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
if s=="":
return False
dic=set(wordDict)
memo=[False for i in range(len(s)+1)]
memo[0]=True
for i in range(len(s)):
j=i
while j>=0:
if s[j:i+1] in dic and memo[j]:
memo[i+1]=True
break
j-=1
return memo[-1]
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
if s=="":
return False
self.dic=set(wordDict)
self.memo=[None for i in range(len(s)+1)]
self.memo[0]=True
return self.assist(s,len(s)-1)
def assist(self,s:str,end:int) -> bool:
if self.memo[end+1]!=None:
return self.memo[end+1]
for i in range(end,-1,-1):
if s[i:end+1] in self.dic and self.assist(s,i-1):
self.memo[end+1]=True
return True
self.memo[end+1]=False
return False
var memo map[int]int =map[int]int{
1:1}
func integerBreak(n int) int {
if v, ok:=memo[n];ok {
return v
}
ans:=0
for i:=1;i<n;i++ {
tmp:=i*integerBreak(n-i)
if tmp>ans {
ans=tmp
}
if i*(n-i)>ans {
ans=i*(n-i)
}
}
memo[n]=ans
return ans
}
class Solution:
memo=[0 for i in range(59)]
def integerBreak(self, n: int) -> int:
if self.memo[n]!=0:
return self.memo[n]
if n==1:
return 1
tmp=0
for i in range(1,n):
tmp=max(tmp, i*self.integerBreak(n-i), i*(n-i))
self.memo[n]=tmp
return tmp
func rob(nums []int) int {
a:=0
b:=0
for i:=0;i<len(nums);i++ {
if a+nums[i]>b {
tmp:=a
a=b
b=tmp+nums[i]
} else {
a=b
}
}
return b
}
{ f ( i , k , 0 ) = max { f ( i − 1 , k , 0 ) , f ( i − 1 , k , 1 ) + p r i c e s [ i ] } f ( i , k , 1 ) = max { f ( i − 1 , k , 1 ) , f ( i − 1 , k − 1 , 0 ) − p r i c e s [ i ] } f ( − 1 , k , 0 ) = f ( i , 0 , 0 ) = 0 f ( − 1 , k , 1 ) = f ( i , 0 , 1 ) = − inf \begin{cases} f(i,k,0)=\max\{f(i-1,k,0),f(i-1,k,1)+prices[i]\}\\ f(i,k,1)=\max\{f(i-1,k,1),f(i-1,k-1,0)-prices[i]\}\\ f(-1,k,0)=f(i,0,0)=0\\ f(-1,k,1)=f(i,0,1)=- \inf\\ \end{cases} ⎩⎪⎪⎪⎨⎪⎪⎪⎧f(i,k,0)=max{ f(i−1,k,0),f(i−1,k,1)+prices[i]}f(i,k,1)=max{ f(i−1,k,1),f(i−1,k−1,0)−prices[i]}f(−1,k,0)=f(i,0,0)=0f(−1,k,1)=f(i,0,1)=−inf
a n s = f ( n , k , 0 ) ans=f(n,k,0) ans=f(n,k,0)
{ f ( i , 0 ) = max { f ( i − 1 , 0 ) , f ( i − 1 , 1 ) + p r i c e s [ i ] } f ( i , 1 ) = max { f ( i − 1 , 1 ) , − p r i c e s [ i ] } f ( 0 , 0 ) = 0 f ( 0 , 1 ) = − p r i c e s [ 0 ] \begin{cases} f(i,0)=\max\{f(i-1,0),f(i-1,1)+prices[i]\}\\ f(i,1)=\max\{f(i-1,1),-prices[i]\}\\ f(0,0)=0\\ f(0,1)=-prices[0]\\ \end{cases} ⎩⎪⎪⎪⎨⎪⎪⎪⎧f(i,0)=max{ f(i−1,0),f(i−1,1)+prices[i]}f(i,1)=max{ f(i−1,1),−prices[i]}f(0,0)=0f(0,1)=−prices[0]
{ f ( i , 0 ) = max { f ( i − 1 , 0 ) , f ( i − 1 , 1 ) + p r i c e s [ i ] } f ( i , 1 ) = max { f ( i − 1 , 1 ) , f ( i − 1 , 0 ) − p r i c e s [ i ] } f ( − 1 , 0 ) = 0 f ( − 1 , 1 ) = − inf \begin{cases} f(i,0)=\max\{f(i-1,0),f(i-1,1)+prices[i]\}\\ f(i,1)=\max\{f(i-1,1),f(i-1,0)-prices[i]\}\\ f(-1,0)=0\\ f(-1,1)=-\inf\\ \end{cases} ⎩⎪⎪⎪⎨⎪⎪⎪⎧f(i,0)=max{ f(i−1,0),f(i−1,1)+prices[i]}f(i,1)=max{ f(i−1,1),f(i−1,0)−prices[i]}f(−1,0)=0f(−1,1)=−inf
{ f ( i , k , 0 ) = max { f ( i − 1 , k , 0 ) , f ( i − 1 , k , 1 ) + p r i c e s [ i ] } f ( i , k , 1 ) = max { f ( i − 1 , k , 1 ) , f ( i − 1 , k − 1 , 0 ) − p r i c e s [ i ] } f ( − 1 , k , 0 ) = f ( i , 0 , 0 ) = 0 f ( − 1 , k , 1 ) = f ( i , 0 , 1 ) = − inf \begin{cases} f(i,k,0)=\max\{f(i-1,k,0),f(i-1,k,1)+prices[i]\}\\ f(i,k,1)=\max\{f(i-1,k,1),f(i-1,k-1,0)-prices[i]\}\\ f(-1,k,0)=f(i,0,0)=0\\ f(-1,k,1)=f(i,0,1)=- \inf\\ \end{cases} ⎩⎪⎪⎪⎨⎪⎪⎪⎧f(i,k,0)=max{ f(i−1,k,0),f(i−1,k,1)+prices[i]}f(i,k,1)=max{ f(i−1,k,1),f(i−1,k−1,0)−prices[i]}f(−1,k,0)=f(i,0,0)=0f(−1,k,1)=f(i,0,1)=−inf
import "math"
var MININT int=^(int(^uint32(0)>>1))
func maxProfit(k int, prices []int) int {
if len(prices)==0 {
return 0
}
max_k:=k
if max_k>=len(prices)/2 {
return maxProfitInf(prices)
}
memo:=make([][][2]int,len(prices))
for i:=0;i<len(prices);i++ {
memo[i]=make([][2]int,max_k+1)
}
for i:=0;i<len(prices);i++ {
for k:=0;k<=max_k;k++ {
if i==0 {
memo[i][k][0]=0
memo[i][k][1]=-prices[i]
} else if k==0 {
memo[i][k][0]=0
memo[i][k][1]=MININT
} else {
memo[i][k][0]=int(math.Max(float64(memo[i-1][k][0]),float64(memo[i-1][k][1]+prices[i])))
memo[i][k][1]=int(math.Max(float64(memo[i-1][k][1]),float64(memo[i-1][k-1][0]-prices[i])))
}
}
}
return memo[len(prices)-1][max_k][0]
}
func maxProfitInf(prices []int) int {
if len(prices)<=1 {
return 0
}
profit:=0
for i:=1;i<len(prices);i++ {
if prices[i]>prices[i-1] {
profit+=prices[i]-prices[i-1]
}
}
return profit
}
{ f ( i , 0 ) = max { f ( i − 1 , 0 ) , f ( i − 1 , 1 ) + p r i c e s [ i ] } f ( i , 1 ) = max { f ( i − 1 , 1 ) , f ( i − 2 , 0 ) − p r i c e s [ i ] } f ( − 1 , 0 ) = 0 f ( − 1 , 1 ) = − inf \begin{cases} f(i,0)=\max\{f(i-1,0),f(i-1,1)+prices[i]\}\\ f(i,1)=\max\{f(i-1,1),f(i-2,0)-prices[i]\}\\ f(-1,0)=0\\ f(-1,1)=-\inf\\ \end{cases} ⎩⎪⎪⎪⎨⎪⎪⎪⎧f(i,0)=max{ f(i−1,0),f(i−1,1)+prices[i]}f(i,1)=max{ f(i−1,1),f(i−2,0)−prices[i]}f(−1,0)=0f(−1,1)=−inf
{ f ( i , 0 ) = max { f ( i − 1 , 0 ) , f ( i − 1 , 1 ) + p r i c e s [ i ] } f ( i , 1 ) = max { f ( i − 1 , 1 ) , f ( i − 1 , 0 ) − p r i c e s [ i ] − f e e } f ( − 1 , 0 ) = 0 f ( − 1 , 1 ) = − inf \begin{cases} f(i,0)=\max\{f(i-1,0),f(i-1,1)+prices[i]\}\\ f(i,1)=\max\{f(i-1,1),f(i-1,0)-prices[i]-fee\}\\ f(-1,0)=0\\ f(-1,1)=-\inf\\ \end{cases} ⎩⎪⎪⎪⎨⎪⎪⎪⎧f(i,0)=max{ f(i−1,0),f(i−1,1)+prices[i]}f(i,1)=max{ f(i−1,1),f(i−1,0)−prices[i]−fee}f(−1,0)=0f(−1,1)=−inf
var MAXINT int=int(^uint32(0)>>1)
func coinChange(coins []int, amount int) int {
memo:=make([]int,amount+1)
for i:=1;i<=amount;i++ {
tmp:=MAXINT
for _,c:=range coins {
if i-c<0 || memo[i-c]==-1 {
continue
}
if memo[i-c]+1<tmp {
tmp=memo[i-c]+1
}
}
if tmp==MAXINT {
memo[i]=-1
} else {
memo[i]=tmp
}
}
return memo[amount]
}
var MAXINT int=int(^uint32(0)>>1)
func minPathSum(grid [][]int) int {
m:=len(grid)
if m==0 {
return 0
}
n:=len(grid[0])
memo:=make([][]int,m)
for i:=0;i<m;i++ {
memo[i]=make([]int,n)
for j:=0;j<n;j++ {
memo[i][j]=MAXINT
}
}
for i:=0;i<m;i++ {
for j:=0;j<n;j++ {
if i==0 && j==0 {
memo[0][0]=grid[0][0]
} else if i==0 {
memo[0][j]=memo[0][j-1]+grid[0][j]
} else if j==0 {
memo[i][0]=memo[i-1][0]+grid[i][0]
} else {
if memo[i-1][j]>memo[i][j-1] {
memo[i][j]=memo[i][j-1]+grid[i][j]
} else {
memo[i][j]=memo[i-1][j]+grid[i][j]
}
}
}
}
return memo[m-1][n-1]
}
import "math"
func minDistance(word1 string, word2 string) int {
memo:=map[[2]int]int{
}
return assist(memo,[]byte(word1),[]byte(word2),len(word1)-1,len(word2)-1)
}
func assist(memo map[[2]int]int,word1 []byte,word2 []byte,i int,j int) int {
if i==-1 {
return j+1
}
if j==-1 {
return i+1
}
if r,ok:=memo[[2]int{
i,j}];ok {
return r
}
r:=0
if word1[i]==word2[j] {
r=assist(memo,word1,word2,i-1,j-1)
} else {
i1:=assist(memo,word1,word2,i,j-1)+1
i2:=assist(memo,word1,word2,i-1,j)+1
i3:=assist(memo,word1,word2,i-1,j-1)+1
r=int(math.Min(math.Min(float64(i1),float64(i2)),float64(i3)))
}
memo[[2]int{
i,j}]=r
return r
}
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
memo=[[0 for j in range(len(word1)+1)] for i in range(len(word2)+1)]
for i in range(len(word1)+1):
memo[0][i]=i
for i in range(len(word2)+1):
memo[i][0]=i
for i in range(1,len(word2)+1):
for j in range(1,len(word1)+1):
if word1[j-1]==word2[i-1]:
memo[i][j]=memo[i-1][j-1]
else:
memo[i][j]=min(memo[i][j-1],memo[i-1][j],memo[i-1][j-1])+1
return memo[-1][-1]
var MAXINT int=int(^uint32(0)>>1)
func superEggDrop(K int, N int) int {
memo:=make([][]int,K+1)
for i:=0;i<K+1;i++ {
memo[i]=make([]int,N+1)
for j:=0;j<N+1;j++ {
memo[i][j]=-1
}
}
return assist(memo,K,N)
}
func assist(memo [][]int,K int,N int) int {
if K==1 {
return N
}
if N==0 {
return 0
}
if memo[K][N]!=-1 {
return memo[K][N]
}
tmp:=MAXINT
start:=1
end:=N
for start<=end {
mid:=(end-start)/2+start
i1:=assist(memo,K-1,mid-1)
i2:=assist(memo,K,N-mid)
if i1-i2<0 {
start=mid+1
if i2+1<tmp {
tmp=i2+1
}
} else {
end=mid-1
if i1+1<tmp {
tmp=i1+1
}
}
}
memo[K][N]=tmp
return tmp
}
import "math"
func longestCommonSubsequence(text1 string, text2 string) int {
memo:=make([][]int,len(text1)+1)
for i:=0;i<len(text1)+1;i++ {
memo[i]=make([]int,len(text2)+1)
}
for i:=1;i<len(text1)+1;i++ {
for j:=1;j<len(text2)+1;j++ {
if text1[i-1]==text2[j-1] {
memo[i][j]=memo[i-1][j-1]+1
} else {
memo[i][j]=int(math.Max(float64(memo[i-1][j]),float64(memo[i][j-1])))
}
}
}
return memo[len(text1)][len(text2)]
}
func findLength(A []int, B []int) int {
memo:=make([][]int,len(A)+1)
for i:=0;i<len(memo);i++ {
memo[i]=make([]int,len(B)+1)
}
ans:=0
for i:=1;i<=len(A);i++ {
for j:=1;j<=len(B);j++ {
if A[i-1]==B[j-1] {
memo[i][j]=memo[i-1][j-1]+1
if memo[i][j]>ans {
ans=memo[i][j]
}
}
}
}
return ans
}
import "math"
func longestPalindromeSubseq(s string) int {
memo:=make([][]int,len(s))
for i:=0;i<len(s);i++ {
memo[i]=make([]int,len(s))
}
for j:=0;j<len(s);j++ {
for i:=j;i>=0;i-- {
if i==j {
memo[i][j]=1
} else if s[i]==s[j] {
memo[i][j]=memo[i+1][j-1]+2
} else {
memo[i][j]=int(math.Max(float64(memo[i+1][j]),float64(memo[i][j-1])))
}
}
}
return memo[0][len(s)-1]
}
f ( i , j , s e c o n d ) = { 0 i = j f ( i + 1 , j , f i r s t ) left > right f ( i , j − 1 , f i r s t ) left ≤ right f(i,j,second)=\begin{cases} 0 & \text{i$=$j}\\ f(i+1,j,first)& \text{left $>$right}\\ f(i,j-1,first) & \text{left $\le$ right}\\ \end{cases} f(i,j,second)=⎩⎪⎨⎪⎧0f(i+1,j,first)f(i,j−1,first)i=jleft >rightleft ≤ right
type Pair struct {
first int
second int
}
func stoneGame(piles []int) bool {
memo:=make([][]Pair,len(piles))
for i:=0;i<len(piles);i++ {
memo[i]=make([]Pair,len(piles))
}
for j:=0;j<len(piles);j++ {
for i:=j;i>=0;i-- {
if i==j {
memo[i][j].first=piles[i]
memo[i][j].second=0
} else {
left:=piles[i]+memo[i+1][j].second
right:=piles[j]+memo[i][j-1].second
if left>right {
memo[i][j].first=left
memo[i][j].second=memo[i+1][j].first
} else {
memo[i][j].first=right
memo[i][j].second=memo[i][j-1].first
}
}
}
}
return memo[0][len(piles)-1].first-memo[0][len(piles)-1].second>0
}
func isMatch(s string, p string) bool {
memo:=make(map[[2]int]bool)
return assist(memo,&s,&p,0,0)
}
func assist(memo map[[2]int]bool, s *string, p *string, point1 int, point2 int) bool {
if point2==len(*p) {
return point1==len(*s)
}
if b,ok:=memo[[2]int{
point1,point2}];ok {
return b
}
flag:=point1<len(*s) && ((*s)[point1]==(*p)[point2] || (*p)[point2]=='.')
ans:=false
if point2+1<len(*p) && (*p)[point2+1]=='*' {
ans=assist(memo,s,p,point1,point2+2) || (flag && assist(memo,s,p,point1+1,point2))
} else {
ans=flag && assist(memo,s,p,point1+1,point2+1)
}
memo[[2]int{
point1,point2}]=ans
return ans
}
// golang中全局变量;python3中全局变量需要加global
var memo []int
var MAXINT int = int(^uint32(0)>>1)
func buildMemo() {
memo=make([]int,10001)
for i:=1;i<len(memo);i++ {
memo[i]=MAXINT
}
for i:=1;i<len(memo);i++ {
for j:=1;1<<j-1 < 2*i;j++ {
target:=1<<j-1
if i==target {
memo[i]=j
break
}
if i<target {
tmp:=j+1+memo[target-i]
if tmp<memo[i] {
memo[i]=tmp
}
} else {
for k:=0;k<j;k++ {
tmp:=j+2+k+memo[i-target+1<<k-1]
if tmp<memo[i] {
memo[i]=tmp
}
}
}
}
}
}
func racecar(target int) int {
if memo==nil {
buildMemo()
}
// fmt.Println(memo)
return memo[target]
}
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
if len(intervals)==0:
return 0
s_intervals=sorted(intervals,key=lambda x:x[1])
count=1
min_end=s_intervals[0][1]
for i in range(1,len(s_intervals)):
if s_intervals[i][0]>=min_end:
count+=1
min_end=s_intervals[i][1]
return len(intervals)-count
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
if len(points)==0:
return 0
s_points=sorted(points,key=lambda x:x[1])
count=1
min_end=s_points[0][1]
for i in range(1,len(s_points)):
if s_points[i][0]>min_end:
count+=1
min_end=s_points[i][1]
return count
import "sort"
func reconstructQueue(people [][]int) [][]int {
sort.Slice(people, func(i int, j int) bool {
if people[i][0]>people[j][0] {
return true
} else if people[i][0]<people[j][0] {
return false
} else {
return people[i][1]<people[j][1]
}
})
ans:=make([][]int, len(people))
end:=-1
for i := range people {
idx:=people[i][1]
for j:=end;j>=idx;j-- {
ans[j+1]=ans[j]
}
end++
ans[idx]=people[i]
}
return ans
}
func canJump(nums []int) bool {
maxL:=0
for i:=0;i<len(nums);i++ {
if i<=maxL && i+nums[i]>=maxL {
maxL=i+nums[i]
if maxL>=len(nums)-1 {
return true
}
}
}
return false
}
func jump(nums []int) int {
pos:=len(nums)-1
step:=0
for pos>0 {
for i:=0;i<pos;i++ {
if pos-i<=nums[i] {
pos=i
break
}
}
step++
}
return step
}
func jump(nums []int) int {
maxVal:=0
end:=0
step:=0
for i:=0;i<len(nums)-1;i++ {
if nums[i]+i>maxVal {
maxVal=nums[i]+i
}
if i==end {
step++
end=maxVal
}
}
return step
}
class Solution:
def minSwapsCouples(self, row: List[int]) -> int:
N=len(row)//2
couples=[[] for i in range(N)]
for i,v in enumerate(row):
couples[v//2].append(i//2)
graph=[set() for i in range(N)]
for i,j in couples:
graph[i].add(j)
graph[j].add(i)
count=0 # dfs查找连通分量
visited=[False for i in range(N)]
for i in range(N):
if visited[i]:
continue
visited[i]=True
count+=1
stack=[i]
while len(stack)>0:
item=stack.pop()
for node in graph[item]:
if not visited[node]:
visited[node]=True
stack.append(node)
return N-count
class Solution:
def minSwapsCouples(self, row: List[int]) -> int:
site={
}
for idx, num in enumerate(row):
site[num]=idx
step=0
for idx in range(0,len(row),2):
couple=row[idx] ^ 1
if row[idx+1]==couple:
continue
real_site=site[couple]
row[idx+1], row[real_site]=row[real_site], row[idx+1]
site[row[idx+1]]=idx+1
site[row[real_site]]=real_site
step+=1
return step
class Solution:
def reorganizeString(self, S: str) -> str:
if S=="":
return ""
self.count={
}
for c in S:
if c not in self.count:
self.count[c]=1
else:
self.count[c]+=1
self.ans=""
self.assist("")
return self.ans
def assist(self,curr:str):
if len(curr)==0:
c=""
else:
c=curr[-1]
tmp=0
for word in self.count:
tmp+=self.count[word]
if tmp==0:
self.ans=curr
return
for word in self.count:
if self.ans!="":
return
if word!=c and self.count[word]>0:
self.count[word]-=1
self.assist(curr+word)
self.count[word]+=1
import "sort"
func reorganizeString(S string) string {
if len(S)==0 {
return ""
}
n:=0
if len(S)%2==0 {
n=len(S)/2
} else {
n=len(S)/2+1
}
count:=map[rune]int{
}
for _,c := range S {
count[c]++
if count[c]>n {
return ""
}
}
source:=[]rune(S)
sort.Slice(source,func(i int,j int) bool {
tmp:=count[source[i]]-count[source[j]]
if tmp>0 {
return true
} else if tmp<0 {
return false
} else {
return source[i]-source[j]<0
}
})
index:=0
tmp:=make([]rune,len(S))
if len(source)%2==1 {
for i:=0;i<len(source);i++ {
tmp[index]=source[i]
index=(index+2)%len(source)
}
} else {
for i:=0;i<len(source);i++ {
tmp[index]=source[i]
if index==len(source)-2 {
index=1
continue
}
index+=2
}
}
return string(tmp)
}
import "container/heap"
type item struct {
cha byte
count int
}
type Stack []item
func (s Stack) Len() int{
return len(s)
}
func (s Stack) Less(i int,j int) bool {
return s[i].count-s[j].count>0
}
func (s Stack) Swap(i int,j int) {
s[i],s[j]=s[j],s[i]
}
func (s *Stack) Push(i interface{
}) {
*s=append(*s,i.(item))
}
func (s *Stack) Pop() interface{
} {
tmp:=*s
x:=tmp[len(tmp)-1]
*s=tmp[:len(tmp)-1]
return x
}
func reorganizeString(S string) string {
count:=map[byte]int{
}
for i:=0;i<len(S);i++ {
count[S[i]]++
if 2*count[S[i]]>len(S)+1 {
return ""
}
}
stack:=&Stack{
}
for k,v:=range count {
*stack=append(*stack,item{
k,v})
}
result:=make([]byte,0,len(S))
heap.Init(stack)
for stack.Len()>=2 {
item1:=heap.Pop(stack).(item)
item2:=heap.Pop(stack).(item)
result=append(result,item1.cha,item2.cha)
if item1.count-1>0 {
heap.Push(stack,item{
item1.cha,item1.count-1})
}
if item2.count-1>0 {
heap.Push(stack,item{
item2.cha,item2.count-1})
}
}
if stack.Len()==1 {
result=append(result,(*stack)[0].cha)
}
return string(result)
}
import "container/heap"
type Item struct {
name byte
count int
}
type CPUHeap []*Item
func (p CPUHeap) Len() int {
return len(p)
}
func (p CPUHeap) Less(i int, j int) bool {
return p[i].count>p[j].count
}
func (p CPUHeap) Swap(i int, j int) {
p[i],p[j]=p[j],p[i]
}
func (p *CPUHeap) Push(x interface{
}) {
*p=append(*p, x.(*Item))
}
func (p *CPUHeap) Pop() interface{
} {
x:=(*p)[len(*p)-1]
*p=(*p)[:len(*p)-1]
return x
}
func leastInterval(tasks []byte, n int) int {
countMap:=map[byte]int{
}
cpuHeap:=&CPUHeap{
}
for i := range tasks {
countMap[tasks[i]]++
}
for k,v := range countMap {
heap.Push(cpuHeap, &Item{
k, v})
}
ans:=0
for cpuHeap.Len() >= n+1 {
tmp:=[]*Item{
}
for i:=0;i<n+1;i++ {
x:=heap.Pop(cpuHeap).(*Item)
x.count--
if x.count>0 {
tmp=append(tmp, x)
}
}
for i:=0;i<len(tmp);i++ {
heap.Push(cpuHeap, tmp[i])
}
ans+=n+1
}
x:=0
m:=1
if cpuHeap.Len()>0 {
m=heap.Pop(cpuHeap).(*Item).count
x++
for cpuHeap.Len()>0 {
if heap.Pop(cpuHeap).(*Item).count==m {
x++
} else {
break
}
}
}
ans+=(m-1)*(n+1)+x
return ans
}
func leastInterval(tasks []byte, n int) int {
count:=make([]int, 26)
for i := range tasks {
count[tasks[i]-'A']++
}
sort.Slice(count, func(i int, j int) bool {
return count[i]-count[j]>0
})
barrelNum:=count[0]
time1:=(barrelNum-1)*(n+1)
lastBarrelNum:=1
for i:=1;i<len(count);i++ {
if count[i]==barrelNum {
lastBarrelNum++
} else {
break
}
}
time1+=lastBarrelNum
time2:=len(tasks)
if time1 > time2 {
return time1
} else {
return time2
}
}
func matrixScore(A [][]int) int {
m:=len(A)
if m==0 {
return 0
}
n:=len(A[0])
for i:=0;i<m;i++ {
if A[i][0]==0 {
for j:=0;j<n;j++ {
A[i][j]^=1
}
}
}
ans:=0
for j:=0;j<n;j++ {
tmp:=0
for i:=0;i<m;i++ {
if A[i][j]==1 {
tmp++
}
}
if tmp<m/2+1 {
tmp=m-tmp
}
ans+=(1<<(n-1-j))*tmp
}
return ans
}
import "sort"
func partitionLabels(S string) []int {
seen:=map[byte][]int{
}
for i:=0;i<len(S);i++ {
if list,ok:=seen[S[i]];!ok {
seen[S[i]]=[]int{
i, i}
} else {
list[1]=i
}
}
lists:=make([][]int, 0, len(seen))
for key := range seen {
lists=append(lists, seen[key])
}
sort.Slice(lists, func(i int, j int) bool {
return lists[i][0]<lists[j][0]
})
point:=0
minEnd:=lists[0][1]
ans:=[]int{
}
for i:=1;i<len(lists);i++ {
if lists[i][0]<minEnd {
if lists[i][1]>minEnd {
minEnd=lists[i][1]
}
} else {
ans=append(ans, minEnd+1-point)
point=minEnd+1
minEnd=lists[i][1]
}
}
ans=append(ans, minEnd+1-point)
return ans
}
func partitionLabels(S string) []int {
seen:=[26]int{
}
for i:=0;i<len(S);i++ {
seen[S[i]-'a']=i
}
start:=0
end:=0
ans:=[]int{
}
for i:=0;i<len(S);i++ {
if seen[S[i]-'a']>end {
end=seen[S[i]-'a']
}
if i==end {
ans=append(ans, end-start+1)
start=i+1
}
}
return ans
}