Hamming Distance
Problem Solving 2021. 8. 9. 23:44

Hamming Distance 문제 내용 Hamming Distance를 구하라 접근 방법 Hamming distance는 기본적으로 두개의 integer를 bit로 변환했을 때, 몇개의 bit가 다른지 알아내는 문제이다. 예를 들어 2진수로 110001101 111000011 이 있다고 하면 다른 값은 위의 붉은색이 된다. 즉, 두개의 값의 차이가 나는 값, XOR로 대표 될 수 있다. 위의 값을 XOR해보자. 001001110 이 된다. 이제 1의 갯수를 구하면 답이 된다. 가장 쉬운 방법은 1bit씩 오른쪽으로 shift하면서 끝 값이 1인지만 확인 하면 된다. var hammingDistance = function(x, y) { let xor = x ^ y; let count = 0; while(..

2진수 최 좌측 1자리 찾기
Problem Solving 2021. 8. 2. 14:12

2021.05.02 - [Problem Solving] - 2진수 최 우측 1의 자리 찾아내기 우측 찾기에 이어서 최 좌측 1자리를 찾을 일이 있어서 Blog를 남겨 본다. 가장 쉬운 방법은 while(1 >1; } 일 것이다. 이것은 O(N)이 필요 함으로 속도를 높이는 방법을 찾아 보자. ori |= (ori >> 1); ori |= (ori >> 2); ori |= (ori >> 4); ori |= (ori >> 8); ori |= (ori >> 16); return ori - (ori>>1) 위는 O(Log N)의 속도로 가장 좌측 1을 찾게 되는 방법이다. 기본적인 아이디어는 copy and paste다. 10000001 이 있을 경우 1b..

2진수 최 우측 1의 자리 찾아내기
Problem Solving 2021. 5. 2. 16:56

어떤 Integer가 주어졌을 경우 해당 Integer의 가장 우측에 존재하는 1의 위치를 찾고자 한다면 다음과 같이 하면 된다. N & -N 6을 예로 들어보겠다. 6의 2진수는 0000 0110 이 된다. 해당 값을 음수로 변환하면 (보수 처리 하고 마지막 1을 더하면 음수이다.) 1111 1010 이 된다. And 처리를 하면 0000 0010 이 되고 가장 우측 위치 값은 2가 된다.

Count Subtrees With Max Distance Between Cities
Problem Solving 2020. 10. 12. 00:24

Count Subtrees With Max Distance Between Cities Count Subtrees With Max Distance Between Cities - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com - 문제 내용 : Tree로 만들어진 Nodes가 있다. Node의 갯수와 Node간의 Brides Array가 주어 졌을때, (순환 하지 않음) 연결 가능한 모든 Node의 Subsets에서 가장 거리가 긴 Distances를 길이로 해서 A..

Complement of Base 10 Integer
Problem Solving 2020. 10. 5. 22:43

Complement of Base 10 Integer Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com -문제 내용 : 주어진 숫자의 2진수형 보수를 출력하라 -접근 방법 10진수의 수를 2진수로 바꾼다 2진수의 수에 11111 이런식으로된 이진수와 XOR를 처리한다 1번의 경우는 내장함수로 변환이 어렵지 않다. 혹시 숫자로 꼭 해야한다면 10진수 들어온..

Minimum Cost to Connect Two Groups of Points
Problem Solving 2020. 9. 23. 21:49

Minimum Cost to Connect Two Groups of Points Minimum Cost to Connect Two Groups of Points - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com - 문제 내용 : A Group Points와 B Group Points 간의 연결시 Cost가 Array로 주어졌을 때 양쪽의 모든 Point가 서로 모두 연결 되었을 때의 최소값을 찾아라 - 접근 방법 : 전 탐색 늘 다른 사람 답을 보고 문제를 풀었..