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/891.Sum-of-Subsequence-Widths at master · wisdompeak/LeetCode

Expand file tree

/

891.Sum-of-Subsequence-Widths

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

891.Sum-of-Subsequence-Widths

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
891.Sum-of-Subsequence-Widths.cpp
891.Sum-of-Subsequence-Widths.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

891.Sum-of-Subsequence-Widths

对于任何A中的一个子序列,其实相当于在A中任取若干个数组成combination。所以总共有2^n种子序列。既然是组合,那么顺序其实无所谓。我们可以先将A排序一下。

将A排序之后,我们立即可以发现,我们可以遍历任意的A[i]和A[j]做为最小值和最大值,那么这样的子序列马上就能知道有2^(j-i-1)种,因此可以用o(n^2)的时间复杂度来解决:sum += (A[j]-A[i])*pow(2,j-i-1)

还有更优化的方法。求所有子序列的max-min的和,等价于求所有子序列的max的和,减去所有子序列的min的和。这样的话我们只需要遍历任意的A[i]:当它作为最小值时的子序列有2^(n-i-1)种;当它作为最大值时的子序列有2^i种。所以代码很简单,只要o(n)的复杂度:

for (int i=0; i<n; i++)
{
  sum += A[i]*pow(2,i);
  sum -= A[i]*pow(2,n-1-i);
}

Leetcode Link

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.