LeetCode[JS] > 350. Intersection of Two Arrays II
·
🔒Algorithm
문제링크https://leetcode.com/problems/intersection-of-two-arrays-ii/description/문제풀이해시테이블을 이용한 풀이 -> Time complexity O(n)/** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */var intersect = function (nums1, nums2) { const map = new Map(); const answer = []; for (const n of nums1) { map.set(n, (map.get(n) || 0) + 1); } for (const n of nums2) { if..
LeetCode [JS] > 1496. Path Crossing
·
🔒Algorithm
문제링크https://leetcode.com/problems/path-crossing/description/문제풀이이 문제는 Set 객체를 이용하면 쉽게 풀 수 있다. Set 객체는 얼핏 보면 Array형태라고 생각될 수 도 있는데 HashTable 형태를 띄고 있기에 시간복잡도 차원에서 Array보다 훨씬 좋다. ( Set 객체는 내부적으로 key와 value가 동일한 구조이며 add되는 순서가 보장된다 그리고 iterable 하기에 순회함수를 쓰기에도 좋다 ). 해당 문제의 시간 복잡도는 O(n) 이다.  /** * @param {string} path * @return {boolean} */var isPathCrossing = function (path) { let position = [0, ..
LeetCode [JS] > 859. Buddy strings
·
🔒Algorithm
📌문제링크https://leetcode.com/problems/buddy-strings/🔨문제풀이이 문제는 s, goal 이 서로 완전히 동일한 문자일 때, 문자열 길이가 다를때, 문자중에 2개만 다를때를 분기처리하여 풀면 된다. 결국 경우를 파악할 수 있어야 한다. 조금만 생각하면 쉽게 풀 수 있다. 시간복잡도는 O(n)이다. 완전히 동일한 문자열일 때는 s 문자중 중복되는 문자가 있으면 해당 문자끼리 순서를 바꿔도 동일한 문자열이 되는데 이를 확인하기 위해 나는 Set() 객체를 썻다. Set 객체는 얼핏보면 시간 복잡도가 O(n^2) 처럼 보일 수 있는데 Set객체는 내부적으로 Hash Table을 사용하여 실제론 O(n) 이다. /** * @param {string} s * @param {st..
LeetCode > 1346. Check If N and Its Double Exist
·
🔒Algorithm
문제https://leetcode.com/problems/check-if-n-and-its-double-exist/풀이쉽게 풀 수 있다. 시간 복잡도는 O(n^2) 이다./** * @param {number[]} arr * @return {boolean} */var checkIfExist = function(arr) { for(let i =0; i
LeetCode > 806. Number of Lines To Write String
·
🔒Algorithm
➕문제https://leetcode.com/problems/number-of-lines-to-write-string/description/➕풀이character의 코드를 이용하면 쉽게 풀 수 있다. 시간 복자도는 O(n) 이다./** * @param {number[]} widths * @param {string} s * @return {number[]} */var numberOfLines = function (widths, s) { let base = "a".charCodeAt(0); let line = 1, pixels = 0; for (let i = 0; i
LeetCode > 2591. Distribute Money to Maximum Children
·
🔒Algorithm
✅링크https://leetcode.com/problems/distribute-money-to-maximum-children/description/🔨문제풀이시간복잡도는 O(n) 으로 무리 없이 풀 수 있었다. 분기처리 잘 해주면 된다./** * @param {number} money * @param {number} children * @return {number} */var distMoney = function(money, children) { if (money