[LeetCode] 2130. Maximum Twin Sum of a Linked List, Medium
·
CodingTest/LeetCode
1. 문제짝수 개의 노드로 이루어진 연결 리스트가 주어질 때, 특정 노드의 인덱스와 쌍을 이루는 노드와의 최대 값을 구해라.쌍은 n - 1 - index 위치가 현재 노드의 쌍.2. 해결/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */function rev..
[LeetCode] 2095. Delete the Middle Node of a Linked List, Medium
·
CodingTest/LeetCode
1. 문제연결 리스트가 주어질 때, 중간 노드를 제외한 리스트를 반환하라.중간 노드는 전체 길이 n/2 위치한다.2. 해결/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */function deleteMiddle(head: ListNode | null): Li..
[LeetCode] 649. Dota2 Senate, Medium
·
CodingTest/LeetCode
1. 문제Dota2에는 Rediant, Dire 두 진영이 있다.라운드 별로 투표가 진행되며, 각 의원들은 2가지 권리가 있다.다른 진영 의원의 권리를 무효화자신과 같은 진영만 남으면 승리를 선언R,D로 문자열이 주어질 때, 최종적으로 승리하는 진영을 반환하라.2. 해결function predictPartyVictory(senate: string): string { let rQueue = [] , dQueue =[]; for(let i = 0; i 0 && dQueue.length > 0) { const rIdx = rQueue.shift(); const dIdx = dQueue.shift(); if(rIdx 각 진영의 의원 인덱스를 큐에 담는다.두 큐의..
[LeetCode] 933. Number of Recent Calls, Easy
·
CodingTest/LeetCode
1. 문제요구 사항에 맞는 RecenCounter class를 구현하라.2. 해결class RecentCounter { queue: number[] constructor() { this.queue = []; } ping(t: number): number { const min = t - 3000, max = t; let recent = 0; this.queue.push(t); while(this.queue[0] max) { this.queue.shift(); } return this.queue.length }}/** * Your RecentCounter ..
[LeetCode] 735. Asteroid Collision, Medium
·
CodingTest/LeetCode
1. 문제소행성의 크기와 방향 정보를 가지고 있는 배열이 주어질 때, 충돌 이후 남는 소행성 배열을 반환하라.양수는 오른쪽으로 이동, 음수는 왼쪽으로 이동한다.크기가 같거나 작은 경우, 해당 소행성은 소멸한다.2. 해결function asteroidCollision(asteroids: number[]): number[] { const stack: number[] = []; for (const asteroid of asteroids) { let isAlive = true; while ( isAlive && stack.length > 0 && stack[stack.length - 1] > 0 && ..
[LeetCode] 2390. Removing Stars From a String, Medium
·
CodingTest/LeetCode
1. 문제문자열 s가 주어질 때, *을 만나면 왼쪽 문자를 제거한 결과 문자열을 반환하라.2. 해결function removeStars(s: string): string { const stack = []; for(let i = 0; i*이 아니면 stack에 쌓아주고, *을 만나면 하나씩 빼준다.
[LeetCode] 2352. Equal Row and Column Pairs, Medium
·
CodingTest/LeetCode
1. 문제n x n 크기의 grid가 주어질 때, 행과 열이 같은 경우의 수를 반환하라.2. 해결function equalPairs(grid: number[][]): number { const rowMap = new Map(); const colMap = new Map(); for (let i = 0; i r[i]).join(','); rowMap.set(row, (rowMap.get(row) || 0) + 1); colMap.set(col, (colMap.get(col) || 0) + 1); } let ans = 0; for (const [str, rowCount] of rowMap) { const colCount = colMap...
[LeetCode] 1657. Determine if Two Strings Are Close, Medium
·
CodingTest/LeetCode
1. 문제문자열 word1, word2가 주어질 때 아래 연산을 거쳐 다른 문자열과 동일하게 만들 수 있는 지 여부를 반환하라.연산1: 문자열 내 존재하는 다른 문자와 바꿀 수 있다.연산2: 연속되는 문자열을 다른 문자와 바꿀 수 있다.2. 해결function closeStrings(word1: string, word2: string): boolean { const map1 = new Map(), map2 = new Map(); for(const char of word1) { map1.set(char, (map1.get(char) || 0) + 1) } for(const char of word2) { map2.set(char, (map2.get(char) |..
[LeetCode] 1207. Unique Number of Occurrences, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 arr이 주어질 때, 각 요소의 등장 횟수가 고유하면 true를 반환하고 아니면 false를 반환하라.2. 해결function uniqueOccurrences(arr: number[]): boolean { const map = new Map(); for(const num of arr) { map.set(num, (map.get(num) || 0 )+ 1) } return map.size === new Set(map.values()).size;};map에 각 등장 횟수를 저장한다.요소의 수와 고유한 등장횟수가 같으면 true, 아니면 false를 반환한다.
[LeetCode] 2215. Find the Difference of Two Arrays, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums1과 nums2가 주어질 때, 2개의 배열을 담은 배열을 반환하라.첫 번째 배열에는 nums2 배열의 요소가 아닌 고유한 값.두 번째 배열에는 nums1 배열의 요소가 아닌 고유한 값.2. 해결function findDifference(nums1: number[], nums2: number[]): number[][] { const set1 = new Set(nums1); const set2 = new Set(nums2); const result = [[], []]; for(const num of set1) { if(!set2.has(num)) result[0].push(num); } for(const num of set2) { ..