当前位置:网站首页>Leetcode exercise - jumping game, combination summation
Leetcode exercise - jumping game, combination summation
2022-06-24 07:56:00 【It's nini】
Jumping game II
subject
Here's an array of nonnegative integers nums , You are first in the array .
Each element in the array represents the maximum length you can jump at that location .
Your goal is to reach the last position of the array with the least number of jumps .
Suppose you can always reach the last position of the array .
Example
Input : nums = [2,3,1,1,4]
Output : 2
explain : The minimum number of jumps to the last position is 2.
From the subscript for 0 Jump to subscript 1 The location of , jump 1 Step , Then jump 3 Step to the last position of the array .
Input : nums = [2,3,0,1,4]
Output : 2
Code
class Solution(object):
def jump(self, nums):
if len(nums) == 1: return 0
ans = 0 # Indicates the farthest position reached
curDistance = 0 # Parameter initialization simplified writing
nextDistance = 0
for i in range(len(nums)): # Traverse nums Each location in
nextDistance = max(i + nums[i], nextDistance) # Next jump position
if i == curDistance:
if curDistance != len(nums) - 1:
ans += 1
curDistance = nextDistance
if nextDistance >= len(nums) - 1: break
return ans
Combinatorial summation II
subject
Given an array without repeating elements candidates And a target number target , find candidates All can make numbers and target The combination of .
candidates Each number in can only be used in each combination once .
explain :
All figures ( Include target) Positive integers
The solution set cannot include duplicate combinations
Example
Input : candidates = [10,1,2,7,6,1,5], target = 8,
Output :
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
Input : candidates = [2,5,2,1,2], target = 5,
Output :
[
[1,2,2],
[5]
]
Code
class Solution(object):
def combinationSum2(self, candidates, target): # Combinatorial summation
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
path = []
def backtrack(candidates,target,sum,startIndex):
if sum == target: res.append(path[:])
for i in range(startIndex,len(candidates)): # To skip elements used in the same layer
if sum + candidates[i] > target: return
if i > startIndex and candidates[i] == candidates[i-1]: continue # Direct use startIndex To and fro , To skip elements used in the same tree layer
sum += candidates[i]
path.append(candidates[i])
backtrack(candidates,target,sum,i+1) # i+1 Each number can only be used once in each combination
sum -= candidates[i] # backtracking
path.pop()
candidates = sorted(candidates) # Sort
backtrack(candidates,target,0,0)
return res
边栏推荐
猜你喜欢

Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization

Backup and restore SQL Server Databases locally

OpenGauss数据库在 CentOS 上的实践,配置篇

The two most frequently asked locks in the interview
![[C language] system date & time](/img/de/faf397732bfa4920a8ed68df5dbc48.png)
[C language] system date & time

Resolution error: LNK2019 unresolved external symbol

热赛道上的冷思考:乘数效应才是东数西算的根本要求

语料库数据处理个案实例(句子检索相关个案)

毕业两年月薪36k,说难也不难吧

Cold thinking on the hot track: multiplier effect is the fundamental requirement of East West calculation
随机推荐
Hongmeng development IV
UTC、GMT、CST
AWTK 最新动态:Grid 控件新用法
报错“Computation failed in `stat_summary_hex()`”
The two most frequently asked locks in the interview
Oracle-高级SQL限定查询
First acquaintance with JUC - day01
Moonwell Artemis现已上线Moonbeam Network
Thread blocking
位运算
交友相亲类软件是如何割你韭菜的
1-4metasploitable2介绍
面试中的最常被问到的两种锁
站在风暴中心:如何给飞奔中的腾讯更换引擎
Exploration on Optimization of elastic expansion engineering
Mousse shares listed on Shenzhen Stock Exchange: gross profit margin continued to decline, and marketing failed in the first quarter of 2022
调用Feign接口时指定ip
Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV
Any remarks
第 2 篇:绘制一个窗口