Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
El problema de House Robber 2 Leetcode da una salida incorrecta

Estoy resolviendo el problema de Leetcode House Robber II. El problema dice,

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. The constraint is you can not rob two adjacent houses. Also, the houses in that place are arranged in a circle, so the first and last houses are also adjacent.

Aquí está el enlace: https://leetcode.com/problems/house-robber-ii/

He usado el enfoque de arriba hacia abajo para resolverlo. Esta es la definición de la función: dp(index, isFirstHouseRobbed) En el cuerpo de la función, estoy comprobando if(index === nums.length - 1 && isFirstHouseRobbed === true) return 0; . Aquí está la función completa:

 var rob = function(nums) { const dp = (index, isFirstHouseRobbed) => { if (index >= nums.length) { return 0; } if (index === nums.length - 1 && isFirstHouseRobbed === true) { return 0; } if (index === 0) { // Two choice: rob or not rob // rob let max1 = dp(index + 2, true) + nums[index]; // not rob let max2 = dp(index + 1, false); return Math.max(max1, max2); } else { // Two choice: rob or not rob // rob let max1 = dp(index + 2, false) + nums[index]; // This does not work // --->(line 20) let max1 = dp(index + 2, isFirstHouseRobbed) + nums[index]; // But this works. // not rob let max2 = dp(index + 1, false); // This does not work // --->(line23) let max2 = dp(index + 1, isFirstHouseRobbed); // But This works. return Math.max(max1, max2); } } return dp(0, false); };

Esta solución solo funciona si uso line20 y line23 . La única diferencia entre esas dos líneas y sus líneas anteriores es que estoy usando la variable isFirstHouseRobbed en lugar de pasar false . Mi pregunta es, en la llamada de función principal estoy llamando a dp(0, false) . Por lo tanto, el valor de isFirstHouseRobbed debe establecerse en false . Mi pregunta es, ¿por qué usar false en lugar de isFirstHouseRobbed no funciona, donde el valor de isFirstHouseRobbed se establece en false de todos modos? Tal vez me estoy perdiendo algo fácil, pero no pude resolverlo. Cualquier ayuda sería muy apreciada.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No siempre es false ; uno de sus casos recursivos pasa true para el segundo parámetro. Esto tiene sentido: no tendría mucho sentido que fuera un parámetro si nunca cambiara.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!