leetcode 히터 잘못된 출력
Oct 04 2020
나는 leetcode 히터 질문을하고있었습니다.
질문: https://leetcode.com/problems/heaters/
제출시 23/30 입력시 잘못된 출력이 표시되지만 이유를 알 수 없습니다.
23 번째 입력
[282475249,622650073,984943658,144108930,470211272,101027544,457850878,458777923]
[823564440,115438165,784484492,74243042,114807987,137522503,441282327,16531729,823378840,143542612]
예상 출력은 다음과 같습니다. 161834419그러나 현재 출력은181367746
내 코드는 다음과 같습니다.
const a = [282475249,622650073,984943658,144108930,470211272,101027544,457850878,458777923]
const b = [823564440,115438165,784484492,74243042,114807987,137522503,441282327,16531729,823378840,143542612]
function findClosest (house, heaters) {
// if only one heater in array of heaters, subtract it with house number to get the difference and return it
if (heaters.length === 1) return Math.abs(house - heaters[0])
const middleIndex = Math.floor((heaters.length - 1)/2)
// if middle heater is equal to house, heater exist on that house number, difference would be zero
if (house === heaters[middleIndex]) return 0
// heater on the leftside and rightside of the middle Heater, if leftside and rightside does not contain any elements (undefinned) then middle would be the left and right most element
const left = heaters[middleIndex - 1] || heaters[middleIndex]
const right = heaters[middleIndex + 1] || heaters[middleIndex]
// if the left side heater location is greater than current house location, we need to move to left
if (left > house) {
return findClosest(house, heaters.slice(0, middleIndex))
}
// if the right side heater is less than current house, we need to move to right
if (house>right) {
return findClosest(house, heaters.slice(middleIndex + 1, heaters.length))
}
// finding diff b/w left and right index and returing the ones with lease distance
const leftDiff = house - left
const rightDiff = right - house
if (leftDiff < rightDiff) return leftDiff
else return rightDiff
}
const findRadius = (houses, heater) => {
let maxIndex = 0
houses.sort((a,b) => a-b)
heater.sort((a,b) => a-b)
for (let i=0; i<houses.length; i++) {
const currentIndex = findClosest(houses[i], heater)
if (currentIndex > maxIndex) maxIndex = currentIndex // if the current returned distance is the highest, set that as maxIndex
}
return maxIndex
}
console.log(findRadius(a, b))
내가하는 일
- 집에서 가장 가까운 좌우 반경 찾기
- 어느 쪽이 더 가까운 지 현재 최대 반경보다 크면 현재 최대 반경과 비교됩니다.
누군가 내 알고리즘에서 실수를 찾는 데 도움을 줄 수 있습니까?
답변
1 SaranshKataria Oct 05 2020 at 07:55
중간 요소도 확인해야합니다. 그것이 귀하의 경우에 누락 된 조건입니다.
업데이트 된 코드는 다음과 같습니다.
function findClosest (house, heaters) {
// if only one heater in array of heaters, subtract it with house number to get the difference and return it
if (heaters.length === 1) return Math.abs(house - heaters[0])
const middleIndex = Math.floor((heaters.length - 1)/2)
// if middle heater is equal to house, heater exist on that house number, difference would be zero
if (house === heaters[middleIndex]) return 0
// heater on the leftside and rightside of the middle Heater, if leftside and rightside does not contain any elements (undefinned) then middle would be the left and right most element
const left = heaters[middleIndex - 1] || heaters[middleIndex]
const right = heaters[middleIndex + 1] || heaters[middleIndex]
// if the left side heater location is greater than current house location, we need to move to left
if (left > house) {
return findClosest(house, heaters.slice(0, middleIndex+1))
}
// if the right side heater is less than current house, we need to move to right
if (house>right) {
return findClosest(house, heaters.slice(middleIndex + 1, heaters.length))
}
// finding diff b/w left and right index and returing the ones with lease distance
const middleDiff = Math.abs(house-heaters[middleIndex])
const leftDiff = house - left
const rightDiff = right - house
return Math.min(middleDiff, leftDiff, rightDiff)
}
function findRadius (houses, heater) {
let maxIndex = 0
houses.sort((a,b) => a-b)
heater.sort((a,b) => a-b)
for (let i=0; i<houses.length; i++) {
const currentIndex = findClosest(houses[i], heater)
if (currentIndex > maxIndex) maxIndex = currentIndex // if the current returned distance is the highest, set that as maxIndex
}
return maxIndex
}