-
[알고리즘/자바스크립트] Codility, CyclicRotation 문제 풀이Algorithm 2019. 11. 29. 14:37
Codility, CyclicRotation An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place). The goal is to rotate array A K time..
-
[알고리즘/자바스크립트] Codility, OddOccurrencesInArray 문제 풀이Algorithm 2019. 11. 29. 14:22
Codility, OddOccurrencesInArray A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: A[0] = 9 A[1] = 3 A[2] = 9 A[3] = 3 A[4] = 9 A[5] = 7 A[6] = 9 the elements at indexes 0 and 2..
-
[알고리즘/자바스크립트] Codility, Binary Gap 문제 풀이Algorithm 2019. 11. 27. 13:35
Codility, Binary Gap A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The n..
-
[알고리즘/자바스크립트] 배열 뒤집기 (Reverse Array)Algorithm 2019. 9. 4. 10:25
Reverse Array 주어진 배열을 뒤집어서 출력한다. 다만 추가 메모리 공간을 만들지 않고 공간복잡도를 고려하여 in-place 방식으로 해보기. (물론 reverse()와 같은 메소드는 사용하지 않고 풀어야한다.) 매우 매우 간단한 몸풀기 문제. 배열의 맨 왼쪽 인덱스와 맨 오른쪽 인덱스를 잡아서 값을 교환하고 왼쪽 인덱스는 오른쪽으로 한 칸 이동하고, 오른쪽 인덱스는 왼쪽으로 한 칸 이동한다. 위 과정을 반복하다가 왼쪽 인덱스가 오른쪽 인덱스보다 크거나 같아지면 반복을 종료하고 배열을 리턴한다. const reverseArray = array => { let left = 0; let right = array.length - 1; while (left < right) { let temp = arr..
-
[알고리즘/자바스크립트] mix-in 패턴을 이용한 이벤트 함수 만들기Algorithm 2019. 8. 22. 11:43
Mix-in eventing system Make an eventing system mix-in that adds .trigger() and .on() to any input object. ※ Example usage: var obj = mixEvents({ name: 'Alice', age: 30 }); obj.on('ageChange', function () { // On takes an event name and a callback function console.log('Age changed'); }); obj.age++; obj.trigger('ageChange'); // This should call our callback! Should log 'age changed'. ※ Caveats: mi..
-
[알고리즘/자바스크립트] 트리에서 조건에 맞는 값만 필터하기 (Tree Collect) - Breadth-First SearchAlgorithm 2019. 8. 20. 11:31
https://im-developer.tistory.com/152 [알고리즘/자바스크립트] 트리에서 조건에 맞는 값만 필터하기 (Tree Collect) - Depth-First Search Tree Collect Implement a `collect` method on this binary Tree class. Collect accepts a filter function, calls that function on each of the nodes in turn, and returns a flat array of node values of t.. im-developer.tistory.com Tree Collect 지난번에 푼 문제와 완벽하게 동일하지만 지난번에는 Depth-First Search로 풀었다면..
-
[알고리즘/자바스크립트] 완전 일치 (Deep Equals)Algorithm 2019. 8. 15. 11:28
Deep Equals Write a function that, given two objects, returns whether or not the two are deeply equivalent--meaning the structure of the two objects is the same, and so is the structure of each of their corresponding descendants. Examples: deepEquals({a:1, b: {c:3}},{a:1, b: {c:3}}); // true deepEquals({a:1, b: {c:5}},{a:1, b: {c:6}}); // false don't worry about handling cyclical object structur..
-
[알고리즘/자바스크립트] 트리에서 조건에 맞는 값만 필터하기 (Tree Collect) - Depth-First SearchAlgorithm 2019. 8. 15. 11:18
Tree Collect Implement a `collect` method on this binary Tree class. Collect accepts a filter function, calls that function on each of the nodes in turn, and returns a flat array of node values of the tree for which the filter returns true. Example: var root1 = new Tree(1); var branch2 = root1.addChild(2); var branch3 = root1.addChild(3); var leaf4 = branch2.addChild(4); var leaf5 = branch2.addC..
-
[알고리즘/자바스크립트] 환영 연결 리스트 찾기 (Linked List Cycle)Algorithm 2019. 8. 8. 12:06
Linked List Cycle Generally, we assume that a linked list will terminate in a null next pointer, as follows: A -> B -> C -> D -> E -> null A 'cycle' in a linked list is when traversing the list would result in visiting the same nodes over and over This is caused by pointing a node in the list to another node that already appeared earlier in the list. Example: A -> B -> C ^ | | v E false nodeE.ne..
-
[알고리즘/자바스크립트] 문자열 카운터 (String incrementer)Algorithm 2019. 8. 4. 23:00
-해당 문제는 codewars사이트의 level5 문제입니다. (1~8단계 중 8단계가 가장 쉬운 레벨)- String incrementer Your job is to write a function which increments a string, to create a new string. If the string already ends with a number, the number should be incremented by 1. If the string does not end with a number. the number 1 should be appended to the new string. foo → foo1 foobar23 → foobar24 foo0042 → foo0043 foo9 → foo10 ..