Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 파이썬
- computerscience
- AWS
- 컴퓨터공학
- typescript
- 문자열
- Local Search
- node.js
- 컴퓨터과학
- 문자열처리
- Simulated Annealing
- node배포
- 코드
- CS
- Search Algorithm
- Hill Climbing
- 철학
- multer-s3
- 알고리즘
- lightsailor
- 배포
Archives
- Today
- Total
지식의모듈화
[CoderPad Interview] Problem 77 [Easy] 본문
Problem 77 [Easy]
This problem was asked by Snapchat.
Given a list of possibly overlapping intervals, return a new list of intervals where all overlapping intervals have been merged.
The input list is not necessarily ordered in any way.
For example, given [(1, 3), (5, 8), (4, 10), (20, 25)], you should return [(1, 3), (4, 10), (20, 25)].
//Problem 77 [Easy]
function answer(arr){
const ans=[];
arr.sort((a,b)=>{return a[0]-b[0]});
const [from, to]=arr.shift();
let currentStartPoint=from;
let currentEndPoint=to;
while(arr.length){
const [from, to ]= arr.shift();
if(from>currentEndPoint||to>currentEndPoint){ //1,3 그리고 2,3
ans.push([currentStartPoint,currentEndPoint]);
if(arr.length===0){
ans.push([from,to]);
}
currentStartPoint=from
currentEndPoint=to;
}
}
console.log(ans);
return ans; //[ [ 1, 3 ], [ 4, 10 ], [ 20, 25 ] ]
}
answer([ [1, 3], [5, 8], [4, 10],[20, 25]])
Javasciprt의 sort기능은 매우 강력하다. Python, C, C++, R 통틀어서 제일 효과적이다.
'PS > LeetCode' 카테고리의 다른 글
[Leetcode][Javascript] 36. Valid Sudoku (0) | 2022.08.17 |
---|---|
[LeetCode][Javascript] LinkedList Problems (0) | 2022.08.05 |
[LeetCode][JavaScript] DFS Problems (0) | 2022.08.04 |
[LeetCode][Javascript] Binary Search Tree Problems (0) | 2022.08.04 |
[LeetCode][JavaScript] 1. TwoSum (0) | 2022.08.04 |