Ambiente de programação com IA
Soma contígua máxima
Encontre a maior soma contígua em até 1000 inteiros de magnitude até um bilhão e retorne soma e limites exclusivos.
● ● ●SpanKestrel●
Fluxos de trabalho · Soma contígua máxima ×
def maximum_subarray(values):
if type(values) not in (list, tuple) or len(values) > 1000 or any(type(v) is not int or abs(v) > 10**9 for v in values):
raise ValueError("subarray.input")
if not values:
return None
best = None
total, start = 0, 0
for end, value in enumerate(values, 1):
if total < 0:
total, start = 0, end - 1
total += value
candidate = (total, start, end)
if best is None or (-total, start, end) < (-best[0], best[1], best[2]):
best = candidate
return best✳Encontre a maior soma contígua em até 1000 inteiros de magnitude até um bilhão e retorne soma e limites exclusivos.
✓ AlteraçõesRevise antes de aplicar ↗
Ilustração do ambiente
- Modelos
- Configure um endpoint compatível com OpenAI ou Anthropic com a URL base e uma chave API, quando necessária. Teste a conexão. O servidor local precisa estar em execução; ferramentas remotas podem transmitir dados separadamente.
- Projeto
- Teste valores todos negativos e entrada vazia, desempatando pelo início e depois pelo fim mais cedo.
- Revise antes de aplicar
- Compare o comportamento real com o resultado esperado. Se uma verificação falhar, investigue a causa antes de fazer outras alterações.
Fluxos de trabalho
def maximum_subarray(values):
if type(values) not in (list, tuple) or len(values) > 1000 or any(type(v) is not int or abs(v) > 10**9 for v in values):
raise ValueError("subarray.input")
if not values:
return None
best = None
total, start = 0, 0
for end, value in enumerate(values, 1):
if total < 0:
total, start = 0, end - 1
total += value
candidate = (total, start, end)
if best is None or (-total, start, end) < (-best[0], best[1], best[2]):
best = candidate
return best