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/Hash/1658.Minimum-Operations-to-Reduce-X-to-Zero at master · wisdompeak/LeetCode

Expand file tree

/

1658.Minimum-Operations-to-Reduce-X-to-Zero

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

1658.Minimum-Operations-to-Reduce-X-to-Zero

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
1658.Minimum-Operations-to-Reduce-X-to-Zero.cpp
1658.Minimum-Operations-to-Reduce-X-to-Zero.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

1658.Minimum-Operations-to-Reduce-X-to-Zero

本题的题意是:在nums数组里找a个元素的前缀和,与b个元素的后缀和,使得他们的sum是x,问如果能找到的话,a+b最少是多少?

比较直观的算法就是:从右往左遍历b的数目,给定了b,我们就要确定多少个元素的前缀和presum[a],恰好等于x-sufsum[b]。显然,我们可以提前遍历数组来构建所有presum[i]->i的hash表。利用这个hash表就能用o(1)时间快速得到指定前缀和所对应的位置。只要这个位置i小于b,那么i和b就是一对合法的解。

最终答案是遍历所有的b,找到i+(n-b)的最小值。

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.