적당한 고통은 희열이다

- 댄 브라운 '다빈치 코드' 중에서

반응형

Algorithm/LeetCode 15

[Swift 알고리즘] LeetCode 746. Min Cost Climbing Stairs

○Dynamic Programming - Easy LeetCode 746. Min Cost Climbing Stairs 문제You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.You can either start from the step with index 0, or the step with index 1.Return the minimum cost to reach the top of the floor.Constraints:• 2 • 0 내 답안시간 O(n), 공간복잡도 O(n)배열을 사용..

Algorithm/LeetCode 2025.09.04

[Swift 알고리즘] LeetCode 3. Longest Substring Without Repeating Characters

△Hash Table, Sliding Window - Medium LeetCode 3. Longest Substring Without Repeating Characters 문제Given a string s, find the length of the longest substring without duplicate characters.이게 슬라이딩 윈도우 문제라고..? 전혀 모르겠는딩.. 1차 시도 - 실패중복이 발생했을 경우에 대한 로직은 잘 구현했으나, 중복이 없을 경우에 처리하는 걸 빠뜨림..→ 마지막에 return 할 때 max(result, arr.count - start) 이렇게 다시 비교를 해줘야 한다. func lengthOfLongestSubstring(_ s: String) -> Int ..

Algorithm/LeetCode 2025.09.01

[Swift 알고리즘] LeetCode 2640. Find the Score of All Prefixes of an Array

○ PrefixSum - Medium (난이도 이상함. Easy 임) LeetCode 2640. Find the Score of All Prefixes of an Array let solution = Solution() print(solution.findPrefixScore([2,3,7,5,10])) //[2,4,8,16,32,64] class Solution { func findPrefixScore(_ nums: [Int]) -> [Int] { var maxNum = nums[0] var conver = 0 var result = [Int]() for i in nums { maxNum = max(maxNum, i) conver += i + maxNum result.append(conver) } retu..

Algorithm/LeetCode 2023.09.10

[Swift 알고리즘] LeetCode 1884. Egg Drop With 2 Eggs and N Floors

× Math, Dynamic Programming | Medium LeetCode 1884. Egg Drop With 2 Eggs and N Floors 뭐라는겨,,, 이해하는 데 오래걸렸다. 구간을 나눠서 계란이 확실히 깨지는 층수를 찾는다는 것 까지는 이해를 했는데... 예를 들어 10으로 나누면 10, 20 ... 100 까지 10번 던진 후, 100에서 깨지면 91~99 까지 9번을 더 던져 최소 19번을 던져야 한다. 구간을 다른 수들로 나눠도 다 19번이 나오는데 어떻게 답이 14가 될 수 있는거지??? 😮 요 영상보고 겨우 이해함 ㅎ https://youtu.be/NGtt7GJ1uiM 핵심은 구간을 일정하게 나누는 것이 아니라, 위로 올라갈수록 구간을 줄여가는 것! 크... 식을 세우면 n ..

Algorithm/LeetCode 2023.07.23

[Swift 알고리즘] LeetCode 2506. Count Pairs Of Similar Strings

○ Hash Table, 조합 | Easy 2506. Count Pairs Of Similar Strings 문제 해결이 어렵진 않은데, 조합 구현 공식은 컨닝함.. ㅎㅎ 영어가 문제인가 문제 설명이 문제인가.. 처음에 읽어보고 뭐라는건지 모르겠어서 포기할 뻔 했쟈나 이해하고 나면 간단함. 1. words 안의 각 문자열들을 Set으로 중복 제거하고, sort로 정렬하고 다시 문자열로 만들어준다. 2. 위의 변환 과정을 거친 문자열을 딕셔너리의 키 값으로 넣어 주고, 같은 문자열일 경우 값을 1씩 더해준다. 3. 딕셔너리에 저장된 중복 문자열로 pair 가 만들어질 수 있는 경우의 수는 nCr 이기 때문에, 조합 공식을 사용해 (n * (n -1)) / 2 로 구해준다. import Foundation ..

Algorithm/LeetCode 2023.04.25

[Swift 알고리즘] LeetCode 2399. Check Distances Between Same Letters

○ Hash Table | Easy LeetCode 2399. Check Distances Between Same Letters 소문자 알파벳의 아스키 값을 사용하는 것이 관건인 문제. 문제 이해하고 나면 어렵진 않다. 근데 문제 설명을 너무 어렵게 해놓음.. ㅡㅡ 내가 이해력이 딸리는건가? ㅎ 0-indexed string 은 무슨말이지? i번째 문자 저게 대체 뭔말인고 ? ㅋㅋㅋㅋ 그런거 다 무시하고 그냥 같은 문자 두개씩 주는데, 같은 문자 두개의 인덱스 차이를 구하면 되는 문제이다. 그 차이가 distance 배열의 알파벳 문자 인덱스와 일치하는지 확인해주면 됨. 1. 문자가 딕셔너리에 없으면 해당 문자의 인덱스를 값으로 추가해주고, 이미 있으면 현재 인덱스와 기존 인덱스의 차를 구해서 같은 문자..

Algorithm/LeetCode 2023.04.25
728x90
반응형