algorithm 24

133. Clone Graph

leetcode.com/problems/clone-graph/ Clone Graph - 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 그래프를 클론하는 문제다. dfs로 접근했다. top to bottom으로 뻗어나갔다가 바텀 찍고 돌아오면서 자식 노드를 생성과 동시에 리턴하면서 부모로 모으는 식으로 하려 했는데 중복 체크에 있어서 애매한 부분이 있어 top to bottom으로 나가되 노드를 미리 생성하고 빈 껍데기라도 미리 리스팅 해서 부모에 추가하는 식으로..

algorithm 2020.11.01

785. Is Graph Bipartite?

leetcode.com/problems/is-graph-bipartite/ Is Graph Bipartite? - 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 주어진 그래프가 bipartite(이분 그래프)인지 판단하는 문제다. 큐를 이용해서 dfs를 구현하였다. 168 ms, faster than 81.64% from collections import deque class Solution: def isBipartite(self, graph: List[List..

algorithm 2020.11.01

1190. Reverse Substrings Between Each Pair of Parentheses

https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/ Reverse Substrings Between Each Pair of Parentheses - 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 부분 부분 서브스트링이 소괄호로 감싸져있는데, 가장 안쪽 스트링 부터 reverse 시키면서 소괄호를 벗기는 문제 계산하기 편하도록 쪼갠 뒤, stack을 이용해서 넣으며, ..

algorithm 2020.10.25

921. Minimum Add to Make Parentheses Valid

leetcode.com/problems/minimum-add-to-make-parentheses-valid/ Minimum Add to Make Parentheses Valid - 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 ())(() 와 같이 parentheses가 주어졌을 때, valid하게 만들기 위해서 몇 개를 추가해야 하는가? 구하는 문제, medium 으로 되어 있지만 easy 수준으로 정말 간단하다. 32ms class Solution: def ..

algorithm 2020.10.20

496. Next Greater Element I

leetcode.com/problems/next-greater-element-i/ Next Greater Element I - 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 이 문제도 전형적인 스택문제 문제 설명이 명확하지 않아서 헷갈리는 부분이 있을 수 있는데 결국 num2 기준으로 각 숫자 다음에 오는 큰 숫자를 찾아놓고 num1에 각각 매핑되는 큰 숫자를 담아 리턴하는 문제다. class Solution: def nextGreaterElement(self,..

algorithm 2020.10.20

735. Asteroid Collision

https://leetcode.com/problems/asteroid-collision/ Asteroid Collision - 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 스택 문제를 더 연습하고 싶어서 하나 더 골라봤다. 고려해야할 경우의 수를 잘 나눠놓고 접근해야 무한루프에 빠지지 않는다. 스택을 이용하되, 연쇄작용 하는 부분을 신경써야함. 40% class Solution: def asteroidCollision(self, asteroids: List[i..

algorithm 2020.10.18