본문 바로가기

프로그래밍/알고리즘, 프로그래밍 언어35

[JavaScript] async/await을 array.map에 사용 시 Promise { <pending> } 에러 array.map 메소드에 async-await을 적용하자 Promise{}에러가 발생했다. // orderList는 주문 데이터가 담긴 배열 const data = await orderList.map(async (order) => { const itemData = await Order_item.findAll({ where: { Order_id: order.id } }) const itemList = itemData.map(el => el.dataValues) order.itemList = itemList return order }) 원인 async 함수는 Promise를 리턴하기 때문에 발생한 에러이다. map 함수가 호출한 async 함수는 Promise를 리턴한다. 그리고 map 함수는 이 Prom.. 2022. 8. 10.
[코딩테스트] Police chase - Queue (JavaScript) 문제 Police officer Daniel has been informed of the location of a suspect and wants to catch him immediately. He starts at a point N on a number line and the suspect is at a point M on the same number line. Police officer has two modes of transportation: walking and teleporting. Walking: Police officer can move from any point X to the point X-1 or X+1 in a single minute. Teleporting: Police office.. 2022. 2. 7.
[코딩테스트] sumOfSubOrdinates - DFS (JavaScript) 문제 There is a company in which the relationships of employees are hierarchical. For example, If there is a subordinate B whose boss is A, then B's subordinate C is also a subordinate of A. A HR staff wants to know the total number of subordinates for a specific employee in the company. Complete a function that returns the sum of subordinates of employee from hierarchcy.Each employee cannot be du.. 2022. 2. 7.
[코딩테스트] 나선형 순회 spiralTraversal (JavaScript) 문제 설명 Take a two-dimensional array(i.e., an array containing many arrays of equal length) and return all the values in the array. 조건 The spiral turns clockwise (start left -> right) All elements of an array contain only numbers Empty arrays are not entered Based on the entire two-dimensional array, there are no identical elements(the same number). 예시 spiralTraversal([ [1,2,3], [4,5,6], [7,8,9] ].. 2022. 2. 7.
[BFS] 1에서 가장 먼 노드의 개수 구하기 (JavaScript) 문제 설명 The number of nodes n and a two-dimensional array with edges is given as a parameter. Please write a function that returns how many nodes are farthest from node 1. The furthest node is the node with the largest number of edges when moving to the shortest path. 매개변수 n : number type으로 number of nodes를 나타냄 (n >= 2) edge : Two-Dimensional Array which Indicates the edge of each node (bidirectio.. 2022. 2. 4.
[코딩테스트] identical characters (JavaScript) 문제 설명 Implement a function that, given a string, Finds the longest run of identical characters and returns an array containing the start and end indices of that run. If there are two runs of equal length, return the first one. 매개변수 str : 문자열 (1 2022. 2. 3.
[코딩테스트] rowBikesOut (JavaScript) 문제 설명 There is a cycling road with bikes lined up in a row. If an owner wants to take out the bike located in N, next bikes should be taken out. Then, bikes must enter the end of the road in the order in which they exited. Complete a function that returns an array of bikes after they going out and in. 매개변수 rowBikes : Array type with bikes in a regular sequence n : number type 아웃풋 Array should be.. 2022. 2. 3.
[코딩테스트] Codility 6-2 MaxProductOfThree (JavaScript) 링크 : https://app.codility.com/programmers/lessons/6-sorting/max_product_of_three/ Solution 정답률 : 100% 시간복잡도 : O(N * log(N)) 우선 배열 A를 오름차순으로 정렬한다. 그리고 여러가지 경우의 수 중 최대값을 구하는 방법은 다음과 같다. 양수 * 양수 * 양수 : 양수 중 절대값이 가장 큰 3개 곱하기 (마지막 index 3개 곱하기) 양수 * 양수 * 음수 : 이 경우가 최대값이 되려면 배열의 엘리먼트가 3개일 때 뿐이므로, 고려하지 않아도 됨 양수 * 음수 * 음수 : 절대값이 가장 큰 음수 2개와 절대값이 가장 큰 양수 곱하기 음수 * 음수 * 음수 : 절대값이 가장 작은 음수 3개 곱하기 (마지막 index.. 2022. 1. 29.
반응형