분류 전체보기 202

[알고리즘][Python] 백준 1005 ACM Craft 문제 풀이

문제 출처 : www.acmicpc.net/problem/1005 1005번: ACM Craft 첫째 줄에는 테스트케이스의 개수 T가 주어진다. 각 테스트 케이스는 다음과 같이 주어진다. 첫째 줄에 건물의 개수 N 과 건물간의 건설순서규칙의 총 개수 K이 주어진다. (건물의 번호는 1번부 www.acmicpc.net 문제 해석 : 원하는 건물을 지을 수 있을 때까지 얼마나 시간이 걸리는지 구하는 문제이다. 문제 풀이 : 위상 정렬과 DP를 활용해서 문제를 풀이 할 수 있다. - 진입 차수를 이용해서 위상 정렬로 차례로 건물을 건설한다. - 진입 차수가 0이 되어 추가될때 DP 테이블에 현재 자신의 테이블과 이전과 지금을 합한것을 비교하여 큰 것을 저장한다. 풀이 코드 from collections imp..

[알고리즘][Python] LeetCode 771 Jewels and Stones 문제 풀이

문제 출처 : leetcode.com/problems/jewels-and-stones/ Jewels and Stones - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 내가 총 몇개의 보석을 가졌는지 출력하는 문제이다. 문제 풀이 : 딕셔너리 자료형을 활요해서 쉽게 풀이 할 수 있다. 풀이 코드 import collections class Solution: def numJewelsInStones(self, jewels: str, stones: s..

[알고리즘][Python] LeetCode 622 Design Circular Queue 문제 풀이

문제 출처 : leetcode.com/problems/design-circular-queue/ Design Circular Queue - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 원형 큐 클래스를 작성하는 문제이다. 문제 풀이 : 나머지 연산을 이용해서 두개의 인덱스를 이용해서 구현하면 된다. 풀이 코드 class MyCircularQueue: def __init__(self, k: int): self.Q = [-1] * k self.fron..

[알고리즘][Python] LeetCode 232 Implement Queue Using Stacks 문제 풀이

문제 출처 : leetcode.com/problems/implement-queue-using-stacks/ Implement Queue using Stacks - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : Stack을 이용해서 Queue를 구현 하는 문제이다. 문제 풀이 : 다른 기능들은 쉽게 구현 할 수 있지만 pop을 수행 할때는 순서에 유의해서 temp 배열에 저장하고 원하는 값을 꺼낸뒤에 다시 원상태로 복귀 시켜줘야 한다. 풀이 코드 c..

[알고리즘][Python] LeetCode 225 Implement Stack Using Queues 문제 풀이

문제 출처 : leetcode.com/problems/implement-stack-using-queues/ Implement Stack using Queues - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 큐를 이용해서 스택을 구현하는 문제이다. 문제 풀이 : 큐는 삽입은 뒤에서 삭제는 앞에서 하기 때문에 여기서 신경쓸 것은 Pop 연산이다. 스택은 삭제 또한 뒤에서 하기 때문에 삭제 대상 직전까지 다른 배열에 임시로 저장하고 원하는 값을 제거..