Algorithm (38) 썸네일형 리스트형 [LeetCode] 84.Largest Rectangle in Histogram (js) 문제 Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram. 더보기 각 막대의 너비가 1인 히스토그램의 막내 높이를 나타내는 정수 배열이 주어진다. 히스토그램에서 나올 수 있는 가장 큰 직사각형의 면적을 반환하라. 풀이 const stack = []; let maxArea = 0; 문제에 접근하기 위해서는 Histogram의 막대 인덱스를 저장할 stack과 최대 너비를 담을 maxArea 라는 두 개의 변수가 필요하다. for (let i = 0; i [LeetCode] Merge Two Sorted Lists (JS) 문제 You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. 더보기 두 개의 정렬된 Linked list list1과 list2가 주언다. 두 리스트를 하나의 정렬된 list로 병합하고 연결된 리스트의 헤드를 반환하라 풀이 let resultList = new ListNode(); let head = list; list1과 list2를 병합하여 새롭게.. [LeetCode] Reverse Linked List (JS) 문제 Given the head of a singly linked list, reverse the list, and return the reversed list. 주어진 단일 연결 리스트를 반대로 뒤집고 그 값을 return 하라 풀이 단일 연결 리스트는 Data와 Next 값만 포함하고 있다. 그렇다면, 어떻게 연결 링크를 이전 노드의 값을 참조할 수 있을까. 방법은 생각보다 단순하다. 바로, 세개의 포인터(prev,curr,next)를 활용하여 연결관계를 바꿔주면 된다. 최초에 변수 설정(포인터)는 다음과 같다. let prev = null; let curr = head; let next; 현재를 나타내는 포인터 curr가 head에 있고. 바로 전 노드 prev 위치를 null로 설정해준다. whi.. [LeetCode] Remove Nth Node From End of List 문제 Given the head of a linked list, remove the nth node from the end of the list and return its head. 주어진 Linked list의 끝에서 N번째 있는 노드를 삭제한 리스트를 반환하는 문제 풀이 Linked list는 배열과 달리 특정 위치의 노드로 접근하기 위해서는 head부터 순차적으로 선회해서 접근해야한다. 그렇기 때문에 끝에서 N번째에 자리한 노드의 위치를 파악 하기 위해서는 두 개의 포인터를 활용하여 계산해 주어야한다. var dummy = new ListNode(0); dummy.next = head; var fast = dummy; var slow = dummy; - 노드의 끝에 먼저 도달할 FAST 포인터와 -.. [LeetCode] Maximum Depth of Binary Tree (재귀함수 이해하기) 문제 Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 주어진 이진트리의 최대 깊이를 반환하여라. 이진트리의 최대 깊이는 루트 노드에서부터 가장 멀리 떨어진 리프 노드까지의 길이를 의미한다. 문제 풀이 사실 문제를 '어떻게 풀어야겠다.'하는 감이 전혀 오지 않았다. 배열의 길이를 나눠서 계산을 해야하나 싶었지만, 전혀 풀리지 않았고. '재귀함수'라는 키워드를 듣고도 선뜻 떠오르지는 않았다. 재귀함수가 자기 자신을 호출하는 함수.. [LeetCode] Plus One (ChatGPT코드) 문제 You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's. Increment the large integer by one and return the resulting array of digits. 오답 노트 /** * @param {number[]} digits * @return {numb.. [LeetCode] Two Sum 문제 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. 풀이 /** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function(nums, target) { if (nu.. [LeetCode] Longest Common Prefix 문제 Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". 배열로 주어진 문자열들의 가장 긴 prefix를 찾는 function을 작성하라. 공통 prefix가 없는 경우 빈 문자열 ""를 반환하여라 풀이 문자열 배열 중 첫번째 단어가 포함하는 알파벳을 기준으로 prefix를 찾는 방법으로 접근해보았다. const firstWord = Array.from(strs[0]); let prefix = ''; 주어진 배열의 첫 단어를 딴 변수 firstWord와 prefix가 확정된 알파벳을 저장할 변수 pre.. 이전 1 2 3 4 5 다음