본문 바로가기

Algorithm101

[스택, 큐] 일일 온도 Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead. Example 1: Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] Example 2: Input: temperat.. 2023. 3. 17.
[스택, 큐] 중복 문자 제거 pg. 247 316 | Remove Duplicate Letters https://leetcode.com/problems/remove-duplicate-letters/ Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: s = "bcabc" Output: "abc" Example 2: Input: s = "cbacdcbc" Output: "acdb" 작성 코드 (50분) - 실패 cl.. 2023. 3. 13.
[연결 리스트] 두 정렬 리스트의 병합 14번 pg.213 21 | Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/description/ You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a 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. Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1.. 2023. 3. 9.
[LeetCode] 217 | Contains Duplicate 217 | Contains Duplicate https://leetcode.com/problems/contains-duplicate/description/ 작성 코드 class Solution: def containsDuplicate(self, nums: List[int]) -> bool: nums_set = set(nums) return len(nums) != len(nums_set) 2023. 3. 9.
[BOJ] 16455 | K번째 수 찾는 함수 16455 | K번째 수 찾는 함수 https://www.acmicpc.net/problem/16455 # 크기가 N인 수열 A1, A2, ..., AN이 주어진다. A를 오름차순 정렬했을 때, # 앞에서부터 K번째 있는 수를 리턴하는 함수를 작성하시오. # -> 주어진 배열에서 오름차순으로 정렬했을 때, K 번째 있는 수를 return 하는 문제 입니다 # a 는 배열, k 는 찾고자 하는 위치 값 (k번째) def kth(a, k): # [0, 0, 0 ....] -> 0이 44725개 있는 배열 cnt1 = [0] * 44725 cnt2 = [0] * 44725 # 배열 a 를 돌며 하나씩 원소를 꺼냄 for i in a: # 인덱스 : i를 44725로 나눈 몫에 22359를 더한 값 # 해당 인.. 2022. 8. 31.
[LeetCode] 1588 | Sum of All Odd Length Subarrays 1588 | Sum of All Odd Length Subarrays https://leetcode.com/problems/sum-of-all-odd-length-subarrays/ 작성 코드 class Solution: def sumOddLengthSubarrays(self, arr: List[int]) -> int: answer = 0 for i in range(1, len(arr) + 1, 2): for start in range(len(arr) - i + 1): answer += sum(arr[start : start+i]) return answer 접근 방법 슬라이싱할 간격을 i 로 지정, 시작 값을 start 로 지정 이상 코드 class Solution: def sumOddLengthSuba.. 2022. 7. 5.
반응형