알고리즘

알고리즘

[프로그래머스] 올바른 괄호

https://school.programmers.co.kr/learn/courses/30/lessons/12909?language=python3 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 고득점 kit에 있는 문제로 스택을 이용해서 풀이 함 def solution(s): answer = True stk = [] for b in s: if not stk or b == '(': stk.append(b) continue if stk.pop() != "(": return False return len(stk) == 0 괄호가 맞는지 확인하는 문제로 1. 스택..

알고리즘

[leetcode/python] 206번 Reverse Linked List

https://leetcode.com/problems/reverse-linked-list/description/ Reverse Linked List - LeetCode Can you solve this real interview question? Reverse Linked List - Given the head of a singly linked list, reverse the list, and return the reversed list. Example 1: [https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg] Input: head = [1,2,3,4,5] O leetcode.com 문제 요약 : 연결 리스트 뒤집기 풀이 로직은 다음과 같다. - 연..

알고리즘

[leetcode/python] 21번 Merge Two Sorted Lists

https://leetcode.com/problems/merge-two-sorted-lists/ Merge Two Sorted Lists - LeetCode Can you solve this real interview question? Merge Two Sorted Lists - 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 leetcode.com 문제 요약: 정렬된 두 연결 리스트를 하나로 합쳐라(정렬 유지) # De..

알고리즘

[leetcode/python] 234번 Palindrome Linked List

https://leetcode.com/problems/palindrome-linked-list/ 문제 요약 : 연결 리스트가 패린드롬(앞뒤로 읽어도 같은 값)인지 검증해라 로직은 다음과 같다. 1. 연결 리스트를 돌면서, 현재 값을 origin에 쭉 저장하고, 순서를 반대로 해서 reverse에 저장한다. 2. 두개가 같은지 비교해서 리턴한다. # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool:..

알고리즘

[leetcode/python] 561번 Array Partition

https://leetcode.com/problems/array-partition/ 문제 요약 : 배열에서 두 개씩 짝을 지었을 때의 최솟값으로 만들 수 있는 최대 합을 구하라 최솟값끼리 더한 것이 최대 합이 되려면 작은 것끼리 짝을 맞추면 된다. 로직은 다음과 같다. 1. 오름차순으로 정렬한다 2. 두개씩 짝지었을 때 짝수번째가 작은 값이 되기 때문에 이를 합산해 준다. class Solution: def arrayPairSum(self, nums: List[int]) -> int: sum = 0 for i, n in enumerate(sorted(nums)): if i % 2 == 0: sum += n return sum 시간복잡도는 O(n)이다

알고리즘

[leetcode/python] 42번 Trapping Rain Water

https://leetcode.com/problems/trapping-rain-water/ 요약 : 벽 사이에 고인 물의 양을 계산하기 Hard 단계의 문제, 너무 어려웠다. 기본 로직은 다음과 같다 height 리스트를 돈다 스택에 0번째 값이 현재 벽 높이 보다 크거나 같으면 물이 고인다 스택을 pop하면서 물 높이를 더해준다 스택에 벽 높이를 쌓는다 마지막까지 스택이 비어있지 않다면 뒤에서부터 벽 높이를 정해서 고인 물을 더해준다 class Solution: def trap(self, height: List[int]) -> int: result = 0 stk = [] for h in height: if stk and stk[0] = 0: water = max - stk.pop() if water >..

알고리즘

[python/leetcode] 49번 group anagrams 문제 풀이

https://leetcode.com/problems/group-anagrams Group Anagrams - LeetCode Can you solve this real interview question? Group Anagrams - Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase leetcode.com 요약 : 같은 요소로 구성된 문자열을 그룹 짓기 로직은 다음과 같다. 1. 같은 애너그램 형식이라면 문..

알고리즘

[python/leetcode] 819번 most common word 문제 풀이

https://leetcode.com/problems/most-common-word Most Common Word - LeetCode Can you solve this real interview question? Most Common Word - Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and tha leetcode.com 요약 : 금지된 단어를 제외하고, 최빈출 단어 찾기 기본 로직 1. 문장을 단어로 구분하면서 깨..

grin-quokka
'알고리즘' 카테고리의 글 목록