avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1798You can construct the maximum number of continuity One question daily

  2024-01-01        
字数统计: 193字   |   阅读时长: 1min

topic:

2023-02-05.png
1798. You can construct the maximum number of continuity

Thought:

4A052176419779588F1300C93180C44A.png
img.png
nums = [1,4,10,3,1]1 ~ 10 All can be constructed,so11 ~ 20As well。
Traversal nums = [1,1,3,4,10], if i<= Earlier+1

$i_0(1) <= 0+1; i_1(1) <= 1+1; i_2(3) <= 2+1; i_3(4) <= 5+1; i_4(10) <= 9+1$

, ExplainiThe previous numbers can be constructed。if i > m + 1It means that it cannot be constructed。

Code:

1
2
3
4
5
6
7
8
9
class Solution:
def getMaximumConsecutive(self, coins: List[int]) -> int:
m = 0 # At the beginning, it can only be constructed 0
coins.sort()
for c in coins:
if c > m + 1: # coins Sort,There is no comparison c Smaller
break # Unable to construct m+1,Continue to loop is meaningless
m += c # Can be constructed [0,m+c] All integer
return m + 1 # [0,m] China m+1 An integer
1
2
3
4
5
6
7
8
9
10
11
12
func getMaximumConsecutive(coins []int) int {
// Initialize a containing0Array
m := 0
sort.Ints(coins)
for _, c := range coins {
if c > m+1 {
break
}
m += c
}
return m + 1
}
  • Python
  • answer
  • golang

扫一扫,分享到微信

微信分享二维码
1669. Merge two linked watches One question daily
1814. Statistics the number of good pairs in an array One question daily
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号