점 연결: 프런트 엔드 및 알고리즘
8553 단어 reactjavascriptalgorithms
알고리즘 도트 - 병합 간격
자세한 내용은 여기LeetCode에서 확인할 수 있습니다.
예 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
예 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
프런트 엔드에 연결
그래서, 제가 지난 주에 본 알고리즘 테스트가 제 현재 작업과 어떻게 관련되어 있는지. 아래 gif를 보자
직접 체험해보세요 ⇒ codesandbox
점들을 잇는
이것이 해결책입니다
const merge = (currentList, newList) => {
// We take the currentList at first load, and the "load more" list.
// First we just merge it by spread operator => [...currentList, ...newList]
// After that we use `Array.prototype.reduce` to generate a new array,
// Which is merged and grouped by date.
const merged = [...currentList, ...newList].reduce((acc, item) => {
// Condition 1: just push the item to the new empty array, or
// if the last item in `acc` doesn't have the same date with the current item
if (!acc.length || acc[acc.length - 1].date !== item.date) {
acc.push(item);
} else {
// Condition 2: if they have the same date, we merge it. 🤝
const lastItem = acc[acc.length - 1];
const mergedDate = [...lastItem.transactions, ...item.transactions];
lastItem.transactions = mergedDate;
}
return acc;
}, []);
return merged;
};
const result = await fakeAPIRequest(page);
setMergedList((prevList) =>
merge(cloneDeep(prevList), cloneDeep(result.items))
);
그래서 이것은 알고리즘 테스트와 약간 다르지만 아이디어는 동일합니다. 이제 리트코드로 돌아와서 직접 해결해보자.
알고리즘 테스트는 때때로 악몽이며 실제 프로젝트에서는 거의 생각하거나 사용하지 않기 때문에 종종 과소 평가합니다. 이 경험은 저를 다르게 생각하게 하고 다음 주말에 더 많이 연습하도록 영감을 줍니다.
Reference
이 문제에 관하여(점 연결: 프런트 엔드 및 알고리즘), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/papercoding22/connecting-the-dots-front-end-and-algorithms-2jpc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)