전체 글
-
Leetcode - 155. Min Stack알고리즘/알고리즘 문제 복기 2021. 2. 27. 15:56
leetcode.com/problems/min-stack/ Min Stack - 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 Answer Stack을 구현하는 문제. 처음엔 vector를 이용해서 풀어보려 했는데 자바에서 Vector는 Depreciate되어서 지원하지 않는다고 한다.
-
LeetCode - 617. Merge Two Binary Trees알고리즘/알고리즘 문제 복기 2021. 2. 26. 23:50
leetcode.com/problems/merge-two-binary-trees/ Merge Two Binary Trees - 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 Answer 처음에는 output_node를 따로 만들어서 풀어보려 했지만 NullPointerException때문에 계속해서 에러가 나는 바람에 기존의 root1을 변경하는 식으로 코드를 짰다.
-
LeetCode - 543. Diameter of Binary Code알고리즘/알고리즘 문제 복기 2021. 2. 26. 19:42
leetcode.com/problems/diameter-of-binary-tree/ Diameter of Binary Tree - 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 처음에는 무조건 root 노드를 통과해야 가장 긴 diameter가 생성된다고 생각해서 다음과 같이 코드를 짰었다 left depth와 right depth를 더해서 max diameter를 구하는 방식으로 했었는데 아무해도 다음과 같을때 에러가 나는 경우가 있었다. 위와같은 트리가 있을..
-
LeetCode - 141. Linked List Cycle알고리즘/알고리즘 문제 복기 2021. 2. 16. 23:28
leetcode.com/problems/linked-list-cycle/ Linked List Cycle - 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 해당 문제를 풀기 위해 몇가지 방법을 생각했었다. 1. 계속해서 다음 Linked List로 next를 해줘서 반복되는 구간을 찾아보고 어느정도 반복된다면 true를 리턴하기 2. 주소값을 저장하는 배열을 만들고 포인터를 만든 뒤 그 포인터를 추적해서, 이전의 주소의 객체를 방문한다면 true를 리턴하기. 각..
-
LeetCode - 121. Best Time to Buy and Sell Stock알고리즘/알고리즘 문제 복기 2021. 2. 16. 22:49
leetcode.com/problems/best-time-to-buy-and-sell-stock/ Best Time to Buy and Sell Stock - 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 배열문제이고, 최댓값을 구하는 문제이므로 DP를 활용해서 풀어보기로 했다. state인 dp[x]는 x인덱스의 날에 팔았을 때의 PROFIT의 최댓값 이라고 정의를 했다. PROFIT의 최댓값이 되려면, 이전에 stock을 산 값이 최솟값이여야 Profit 즉..
-
Dynamic Programming알고리즘 2021. 2. 16. 11:17
다이나믹 프로그래밍이란 복잡한 문제를 sub-problem들로 나누고 중복된 계산을 피하기 위해 sub-problem들을 풀어낸 결과를 저장하고 사용하는 것을 말한다. DP의 두가지 속성 Overlapping SubProblem Optimal Substructure Property DP의 두가지 속성 1. Overlapping Subproblem 분할정복에서와 같이 DP는 문제를 sub-problem들로 쪼개고 다시 합치는 방식으로 문제를 풀어나가기 때문에 문제를 쪼갤 수 있다면 DP를 적용 할 수 있다. 2. Optimal SubStructure Property 주어진 문제를 sub-problem에서 최적화된 답을 사용해서 최적화된 답을 얻을 수 있다면 Optimal Substructure Proper..
-
Leetcode - 104. Maximum Depth of Binary Tree알고리즘/알고리즘 문제 복기 2021. 2. 14. 17:27
leetcode.com/problems/maximum-depth-of-binary-tree/ Maximum Depth of Binary Tree - 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 Answer 처음에 Left Depth, right depth라는걸 새로 만들어서 구현하려고 했어서 좀 헤매긴 했는데 다행히 DFS에 대해 사전지식이 약간이나마 있어서인지 풀 수 있었다. 하지만 트리라던지, 기존의 알고리즘 배웠던 것들을 까먹은 것들이 많아서 다시 복습을..
-
LeetCode - 70. Climbing Stairs알고리즘/알고리즘 문제 복기 2021. 2. 14. 15:21
leetcode.com/problems/climbing-stairs/ Climbing Stairs - 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 1스텝또는 2스텝으로만 계단을 올라가는 경우의 수를 구하는 문제. 고민을 많이 했었는데 결국 풀어내지 못했고 답지를 봤다. 다이나믹 프로그래밍으로 풀어내는 문제였다. Answer1. Top-down approach 만약 주어진 계단의 수가 5개라고 한다면 내가 할 수 있는 경우의 수는 1. 1개의 계단을 올라간다. ..