본문 바로가기

알고리즘

(10)
Prefix Sums_Codility Write a function: class Solution { public int solution(int A, int B, int K); } that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.: { i : A ≤ i ≤ B, i mod K = 0 } For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 1..
TapeEquilibrium_codility 문제 A non-empty array A consisting of N integers is given. Array A represents numbers on a tape. Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])| In other words, it is the absolute dif..
PermMissingElem_codility 문제 An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. Your goal is to find that missing element. Write a function: function solution(A); that, given an array A, returns the value of the missing element. For example, given array A such that: A[0] = 2, A[1] = 3, A[2] = 1, A[3] = 5 the funct..
FrogJmp_codility 문제 A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. Count the minimal number of jumps that the small frog must perform to reach its target. Write a function: function solution(X, Y, D); that, given three integers X, Y and D, returns the m..
OddOccurrencesInArray_Codility 문제 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 have value 9, the elements a..
CyclicRotation_Codility 문제 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 times; that is, each eleme..
Remove Nth Node From End of List - leetcode var removeNthFromEnd = function (head, n) { // Two pointers - fast and slow let slow = head; let fast = head; // Move fast pointer n steps ahead for (let i = 0; i < n; i++) { if (fast.next === null) { // If n is equal to the number of nodes, delete the head node if (i === n - 1) { head = head.next; } return head; } fast = fast.next; } // Loop until we reach to the end. // Now we will move both f..
Valid Anagram - leetcode Given two strings s and t, return true if t is an anagram of s, and false otherwise. case1: Input: s = "anagram", t = "nagaram" Output: true case2: Input: s = "rat", t = "car" Output: false Hash를 이용한 접근법 주어진 길이를 비교하고 같지 않으면 false를 반환합니다. 빈 객체를 만들어 s문자열에 있는 모든 문자를 키로 저장합니다. 저장할 때 이미 존재하는 키가 있다면 value를 1씩 증가합니다. t문자열은 위에서 만든 객체에서 비교합니다. t문자열이 위의 객체의 키로 존재하지않으면 false를 리턴하고 존재하면 value를 1씩 감소합니다. 최종적..