전체 글 32

[Leetcode] 3396. Minimum Number of Operations to Make Elements in Array Distinct

문제You are given an integer array nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.Note that an empty array is considered to have distinct elements. Return the minimum number of op..

Leetcode 2025.04.08

[Leetcode] 5. Longest Palindromic Substring

문제Given a string s, return the longest palindromic substring in s. Example 1:Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2:Input: s = "cbbd" Output: "bb"  Constraints:1  풀이palindrome의 시작이 되는 곳을 s의 처음부터 하나씩 구해준다.이때 s[i]==s[i+1], s[i-1]==s[i+1] 경우 둘다 고려해줘야 한다. 중요한점은 while loop 에서 인덱스를 먼저 condition으로 둬야 다음 s[left] == s[right] 비교할때 index out of bound 에러가 뜨지 않는..

Leetcode 2025.04.07

[Leetcode] 515. Find Largest Value in Each Tree Row

문제Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed). Example 1:Input: root = [1,3,2,5,3,null,9] Output: [1,3,9] Example 2:Input: root = [1,2,3] Output: [1,3]  Constraints:The number of nodes in the tree will be in the range [0, 104].-231 풀이 bfs를 사용하는 문제이다.각 row를 bfs 로 찾고, for loop안의 각 element의 최댓값을 찾아준다.코드/** * Definition for a binary tree ..

Leetcode 2025.04.04