[LeetCode] 454. 4Sum II, Medium
·
CodingTest/LeetCode
1. 문제배열 4개가 주어질 때, 각 요소의 합이 0이되는 튜플의 수 를 구하라.2. 해결function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number { const map = new Map(); const n = nums1.length; let ans = 0; for(let i =0; i4개의 배열이기 때문에 모든 경우의 수를 탐색하면 당연히 시간 초과가 날 것.근데 못풀었다.접근 방식이 안떠오른다 도저히.map을 이용해서 2개의 배열씩 등장할 수 있는 값과 등장 회수를 key-value로 관리다른 2개의 배열을 이용해 -를 붙여 해당 key가 있으면 등장 회수를 더해준다.
[LeetCode] 3. Longest Substring Without Repeating Characters, Medium
·
CodingTest/LeetCode
1. 문제문자열 s가 주어질 때, 중복되지 않는 가장 긴 서브 문자열의 길이를 구하라.2. 해결function lengthOfLongestSubstring(s: string): number { const map = new Map(); // left ~ right 구간을 슬라이딩 윈도우. let left = 0; let maxLen = 0; for(let right = 0; right= left) { left = map.get(char) + 1; } map.set(char, right); // 윈도우 전 구간 탐색. 6~7까지 후 최고 값 갱신. maxLen = Math.max(maxLen, right - le..
[LeetCode] 771. Jewels and Stones, Easy
·
CodingTest/LeetCode
1. 문제문자열 jwels, stones가 주어질 때, stones에 포함되어 있는 jweles의 개수를 반환하라.2. 해결function numJewelsInStones(jewels: string, stones: string): number { const set = new Set(jewels); let count = 0; for(const stone of stones.split('')){ if(set.has(stone)) count++; } return count;};
[LeetCode] 652. Find Duplicate Subtrees, Medium
·
CodingTest/LeetCode
1. 문제이진 트리가 주어질 때, 구조가 같고 같은 값으로 이루어져 있는 노드의 배열을 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.ri..
[LeetCode] 36. Valid Sudoku, Medium
·
CodingTest/LeetCode
1. 문제현재 주어진 스도쿠 정보를 바탕으로(빈 칸 무시) 스도쿠 룰에 적합한 스도쿠인 지 판단하라.2. 해결function isValidSudoku(board: string[][]): boolean { const rowMap = new Map>(); const colMap = new Map>(); const boxMap = new Map>(); for(let i = 0; i ([number])); if(!colMap.has(j)) colMap.set(j, new Set([number])); if(!boxMap.has(boxIdx)) boxMap.set(boxIdx, new Set([number])); rowMap.get(i..
[LeetCode] 49. Group Anagrams, Medium
·
CodingTest/LeetCode
1. 문제문자열 배열 strs가 주어질 때, 애너그램 문자들 끼리 그룹화한 배열을 반환하라.2. 해결function groupAnagrams(strs: string[]): string[][] { const map = new Map(); for(const str of strs) { const key = str.split('').sort().join(''); if(map.has(key)) { map.set(key, [...map.get(key), str]) } else { map.set(key,[str]) } } return [...map.values()];};각 문자열을 오름차순 정..
[LeetCode] 219. Contains Duplicate II, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums와 정수 k가 주어질 때, 두 요소가 같고 인덱스 차이의 절대값이 k 이하인 경우 true, 아니면 false를 반환하라.2. 해결function containsNearbyDuplicate(nums: number[], k: number): boolean { const map = new Map(); for(let i = 0; i(); // for(let i = 0; i 1) { // let p1 =0, p2 = 1; // while(p1 처음에는 같은 숫자 별로 인덱스를 배열에 저장하고, 해당 배열의 모든 경우의 수에서 구할 수 있는 절대값으로 k를 비교했다.그럴 필요 없이 최신 인덱스만 map에..
[LeetCode] 350. Intersection of Two Arrays II, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums1, nums2가 주어질 때, 두 배열에서 공통으로 등장하는 요소를 반환하라.이 결과에는 등장 횟수만큼 중복될 수 있다. (349번 문제는 고유한 요소 반환)2. 해결function intersect(nums1: number[], nums2: number[]): number[] { const map = new Map(); for(const num of nums1) { if(map.has(num)) map.set(num, map.get(num) + 1) else map.set(num, 1) } const result = []; for(const num of nums2) { if(map.has(num) ..
[LeetCode] 387. First Unique Character in a String, Easy
·
CodingTest/LeetCode
1. 문제문자열 s가 주어질 때, 반복되지 않는 문자 중 가장 먼저 등장하는 문자의 인덱스를 반환하라.없으면 -1을 반환하라.2. 해결function firstUniqChar(s: string): number { const map = new Map(); let index = 0; for(const char of s) { if(!map.has(char)) map.set(char, {idx: index, count:1}); else { const {idx, count} = map.get(char); map.set(char, {idx, count: count + 1}); } i..
[LeetCode] 599. Minimum Index Sum of Two Lists, Easy
·
CodingTest/LeetCode
1. 문제문자열 list1, list2가 주어질 때, 공통으로 존재하는 문자열의 두 인덱스 차이가 가장 작은 문자열을 담아 반환하라.2. 해결function findRestaurant(list1: string[], list2: string[]): string[] { const map1 = new Map(); const map2 = new Map(); for(let i = 0; i 두 문자 배열을 탐색하며 Map에 문자열-인덱스로 맵핑.한쪽 Map을 탐색하며, 다른 쪽 Map에 있으면서 최소값 갱신.최소값보다 작은 경우가 나오면 result 배열을 초기화 후 담기.