본문 바로가기

Web Development

(78)
[Algorithm][LeetCode 75][Medium] Sort Colors 문제Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.You must solve this problem without using the library's sort function.Example 1Input: nums = [2,0,2,1,1,0]Output: [0,0,1..
[Algorithm][LeetCode 1550] Three Consecutive Odds 연속된 홀수 판별하기 (Three Consecutive Odds)문제: 주어진 정수 배열 arr에 대해, 연속된 홀수 숫자 3개가 존재하면 true를 반환하고, 그렇지 않으면 false를 반환하라.1. 접근 방식연속된 홀수의 개수를 카운트할 변수 count를 생성한다.배열을 순회하면서 현재 숫자가 홀수면 count를 1 증가시킨다.짝수가 나오면 count를 0으로 초기화한다.count가 3이 되는 순간 true를 반환한다.끝까지 반복했지만 3이 되지 않으면 false를 반환한다.2. 자바스크립트 코드/** * @param {number[]} arr * @return {boolean} */var threeConsecutiveOdds = function(arr) { let count = 0; fo..
[Algorithm][LeetCode 2918] Minimum Equal Sum of Two Arrays After Replacing Zeros 이번 글에서는 LeetCode 문제 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros를 풀이하고, if / else if 구조와 독립된 if 구조에 대한 성능 실험을 함께 진행해봤습니다.1. 문제 설명You are given two arrays nums1 and nums2 consisting of positive integers.You have to replace all the 0's in both arrays with strictly positive integers such that the sum of elements of both arrays becomes equal.Return the minimum equal sum you can obtain..
[Algorithm][LeetCode 69] Sqrt(x) LeetCode 문제 풀이 - 제곱근 구하기 (Square Root of x)문제 설명주어진 비음수 정수 x에 대해, 내장 제곱근 함수 없이 정수 제곱근을 구하고 내림(floor)하여 반환하는 문제입니다. 즉, Math.sqrt, pow(x, 0.5), x ** 0.5와 같은 내장 함수는 사용할 수 없습니다.Input: x = 4Output: 2Input: x = 8Output: 2접근 방법제곱근은 수학적으로 sqrt * sqrt = x를 만족하는 sqrt를 구하는 것과 같습니다. 단, 실수로 정확하게 구하는 것이 아니라, 정수 범위 내에서 내림한 값을 구하는 것이 목표입니다.따라서 가능한 제곱근 후보를 이진 탐색(Binary Search)으로 좁혀가며 정답을 구할 수 있습니다.풀이 코드 (JavaScr..
[Problem Solving] useSelector, 커스텀 훅, 그리고 리렌더링 최적화 React + Redux 프로젝트를 하면서, 사용자 상태에 따라 UI가 변화하는 요구가 많아졌습니다. 이를 깔끔하게 관리하기 위해 커스텀 훅을 만들었고, 그 과정에서 useSelector의 리렌더링 조건과 최적화 방식에 대해 고민하게 되었습니다. 이 글은 그 과정을 정리한 실전 기록입니다.1. 사용자 관련 조건을 커스텀 훅으로 분리하다조건이 많아지면서 복잡한 분기를 커스텀 훅으로 분리했습니다.// hooks/useUserSegment.jsimport { useSelector } from 'react-redux';export const useUserSegment = () => { const isAuthed = useSelector((state) => state.isAuthed); const user =..
[Algorithm][LeetCode 35] Search Insert Position 이진 탐색을 활용한 삽입 위치 찾기 (O(log n))정렬된 배열이 주어졌을 때, 특정 target 값을 찾고, 해당 값이 존재하지 않는다면 정렬된 상태를 유지하면서 삽입할 수 있는 위치를 반환하는 문제입니다.Given a sorted array of distinct integers and a target value,return the index if the target is found. If not, return the index where it would be if it were inserted in order.You must write an algorithm with O(log n) runtime complexity.Input: nums = [1,3,5,6], target = 5Output: 2In..
[Problem Solving] 프론트엔드 성능 최적화 기본 개념 프론트엔드 성능 최적화프론트엔드 성능은 단순히 페이지를 빠르게 보이게 하는 것을 넘어, 검색 엔진 최적화(SEO), 사용자 경험, 전환율 등에 직접적인 영향을 미칩니다.핵심 성능 지표: Core Web Vitals지표설명기준LCP최대 콘텐츠가 로딩 완료된 시점2.5초 이하INP전체 상호작용의 평균 응답 시간 (FID 대체)200ms 이하CLS예기치 않은 레이아웃 이동의 누적 점수0.1 이하Core Web Vitals는 SEO에도 직접적인 영향을 주는 사용자 중심의 성능 지표입니다.주요 성능 최적화 전략1. LCP 개선 전략이미지 최적화: WebP 포맷 사용, 크기 조절, 압축 적용반응형 이미지 제공: srcset 및 sizes 속성 활용지연 로딩 적용: loading="lazy"필수 CSS는 인라인 처리..
[Algorithm][LeetCode 1920] buildArray 배열 인덱스를 활용한 간단한 배열 구성 문제 - buildArrayLeetCode의 배열 문제 중 하나인 “Build Array from Permutation” 문제를 정리해보았습니다.문제 설명0부터 시작하는 순열 배열 nums가 주어졌을 때, ans[i] = nums[nums[i]]가 되도록 배열 ans를 구성하여 반환하는 문제입니다.여기서 0 기반 순열(zero-based permutation)은 0부터 nums.length - 1까지의 정수를 중복 없이 포함한 배열을 의미합니다.예제 1Input: nums = [0,2,1,5,3,4]Output: [0,1,2,4,5,3]설명:ans = [ nums[nums[0]], // nums[0] = 0 → nums[0] = 0 nums[nums[1]], /..