본문 바로가기

알고리즘18

[백준] Javascript의 shift() 사용했을때 시간초과 관계 리스트를 만들때 input에서 shift()를 해서 받아와 만들었는데 시간초과가 났다.. // 연결 관계리스트 만들기 for (let i = 0; i < M; i++) { let [x, y] = input.shift().split(" ").map(Number); graph[x].push(y); graph[y].push(x); } 검색해보니 shift()가 많이 느리다고하네 .. 배열의 끝이 아닌 임의의 위치에서 항목을 삭제하는 것은 큰 대가를 치뤄야 하기 때문입니다. 그래서 index를 지정해서 설정해줬다. // 연결 관계리스트 만들기 for (let i = 0; i < M; i++) { let [x, y] = input[i].split(" ").map(Number); graph[x].push(y).. 2023. 10. 19.
[백준 14503] 백준 로봇청소기 Javascript 문제 이름 백준 14503 로봇청소기 문제 풀기 전 생각 눈물나게 어려웠다 ... 이것이 ... 골드 5 ?????? 다들 이런걸 푼단 말이야 ...? 난이도는 골드 5다. 새롭게 알게 된 것 , 기억할 것 1. 방향에 따른 이동이 있는 문제라면 dx = [0,0,-1,1] dy=[-1,1,0,0] 이동에 따른 좌표를 만들어서 let nx = x + dx[0]; let ny = y + dy[0] 이렇게 왔다갔다 해주는게 좋다 알기 전엔 case 문으로 이동시켰는데 머리 깨질뻔 .... 2. 후진하는 경우 정답이 안나와서 뭐가문제지 .. 하고 보고있는데 후진하는 경우 좌표를 잘못 설정 해줬다. 나는 삼항 연산자를 써서 let bd = nd >= 2 ? nd - 2 : nd + 2; 이렇게 .. 써줬는데 다.. 2023. 10. 18.
[Leetcode/JS] 350. Intersection of Two Arrays II 문제링크 350. Intersection of Two Arrays II https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ Intersection of Two Arrays II - LeetCode Can you solve this real interview question? Intersection of Two Arrays II - Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you.. 2023. 7. 18.
[Leetcode/JS] 226. Invert Binary Tree 문제링크 226. Invert Binary Tree https://leetcode.com/problems/invert-binary-tree/description/ Invert Binary Tree - LeetCode Can you solve this real interview question? Invert Binary Tree - Given the root of a binary tree, invert the tree, and return its root. Example 1: [https://assets.leetcode.com/uploads/2021/03/14/invert1-tree.jpg] Input: root = [4,2,7,1,3,6,9] Output: [4 leetcode.com 문제 설명 Give.. 2023. 7. 18.