LeetCode

Programming/LeetCode

[Dart] 125. Valid Palindrome

1. 문제https://leetcode.com/problems/valid-palindrome/description/?envType=study-plan-v2&envId=top-interview-150   2. 요구사항1) 팰린드롬은 모든 대문자를 소문자로 바꾸고, 모든 영숫자가 아닌 문자를 제거한 후, 앞뒤로 읽어도 같은 구문이다.2) 문자열 s가 주어진 후, 팰린드롬이면 true를 반환하고, 아니면 false를 반환한다.  3. 핵심 아이디어1) 문자열 전처리 후, 문자 비교 주어지는 문자열에는 공백과 특수 문자가 있으며, 대문자와 소문자가 나누어져 있다.이를 순수한 소문자 문자열로 바꾸기 위해 전처리 과정을 거친다.String result = s.replaceAll(RegExp('[^a-zA-Z0-9]..

Programming/LeetCode

[Dart] 121. Best Time to Buy and Sell Stock

1. 문제 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150   2. 요구사항1) 주식 가격이 작성된 prices 배열 주어진다.2) 주식을 한 번 사고 한 번 팔아서 얻을 수 있는 최대 이익을 계산한다.3) 이익을 얻을 수 없는 경우에는 0을 반환한다.  3. 핵심 아이디어1) 최소 가격을 추적하고, 최대 이익 계산prices 배열에서 가장 싼 가격(minPrice)을 추적한다. 배열을 왼쪽에서 오른쪽으로 순회하며 현재 가격 - minPrice 를 계산해서 현재 시점에서 얻을 수 있는 이익을 구한다. 그 이익이 지금까지 본 최대 이익보다 ..

Programming/LeetCode

[Dart] 104. Maximum Depth of Binary Tree

1. 문제https://leetcode.com/problems/maximum-depth-of-binary-tree/description/?envType=study-plan-v2&envId=leetcode-75   2. 요구사항1) 이진 트리의 루트 노드가 주어졌을 때, 이 트리의 최대 깊이를 구한다.   3. 핵심 아이디어1) 재귀적 접근각 노드의 왼쪽 노드와 오른쪽 노드의 깊이를 각각 구한 후, 그 중 더 큰 깊이에 1을 더하여 반환한다.  2) 코드class Solution { int maxDepth(TreeNode? root) { // root가 null이면 종료 if(root == null) { return 0; } // 오른쪽 노드의 깊이 구하기 int..

Programming/LeetCode

[Dart] 88. Merge Sorted Array

1. 문제 https://leetcode.com/problems/merge-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150   2. 요구사항1) 배열 nums1과 nums2가 주어지며, num1은 충분한 공간을 할당받고 있다.2) num1의 초기 크기는 m이고, num2의 크기는 n이다.3) 두 배열을 합쳐 nums1에 정렬된 형태로 저장해야한다.  3. 핵심 아이디어  1) 뒤에서부터 채우고 정렬nums1의 유효 인덱스 다음부터 nums2 배열을 삽입한다. 그 후, nums1과 nums2가 합친 배열이 nums1에 저장되는데 이를 sort로 정렬한다.  2) 코드class Solution { void merge(List ..