leetcode
-
[leetcode 234] Palindrome Linked Listleetcode 2022. 5. 19. 01:58
를 보고 공부한 내용입니다. https://leetcode.com/problems/palindrome-linked-list/ Palindrome Linked List - 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 연결리스트가 팬린드롬인지 확인하는 문제이다. 이전에 주어진 리스트가 팬린드롬인지 확인하는 문제가 있었는데 그와 같은 방법으로 deque에 넣어서 풀이 하였다. deque를 이용한 풀이 # Definition for singly-linked list...
-
[leetcode 121] Best Time to Buy and Sell Stockleetcode 2022. 4. 12. 00:21
를 보고 공부한 내용입니다. https://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 처음에는 브루스포스로 모든 경우를 계산해 최대이익을 찾았는데 O(n^2)으로 시간초과가 발생했다. 다른 방법으로는 prices를 for문으로 돌면서 최소price와 최대이익을 계속 비교하며 갱신해나가는 것이다. 이 방..
-
[leetcode 238] Product of Array Except Selfleetcode 2022. 4. 1. 02:11
를 보고 공부한 내용입니다. https://leetcode.com/problems/product-of-array-except-self/ Product of Array Except Self - 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 모든 요소의 합을 곱해놓은 다음 각 자리의 값으로 나누는 풀이가 가장 먼저 떠올랐다. 하지만 문제에 명시되어 있듯이 나누기를 쓰면 안되고 또한 0이라는 요소가 있을경우 저렇게 접근하면 복잡해질거 같아 다른방향으로 생각해보기로 했..
-
[leetcode 561] Array Partition Ileetcode 2022. 3. 23. 03:24
를 보고 공부한 내용입니다. https://leetcode.com/problems/array-partition-i Array Partition 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 좀만 생각하니까 바로 어떻게 풀지 생각이 나는 쉬운문제라고 생각한다. 두개씩 쌍을 이룬 것의 최솟값의 합이 최대가 되어야 한다. 따라서 최대한 수가 차이 안나는것 끼리 pair로 묶어야 최대가 될것이라고 생각했고 차이가 안나는것끼리 모여놓게 하려고 정렬을 해주었다. 배..
-
[leetcode 15] 3Sumleetcode 2022. 3. 19. 03:01
를 보고 공부한 내용입니다. https://leetcode.com/problems/3sum 3Sum - 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 문제를 보고 지난번에 풀었던 2Sum과 비슷한 풀이로 풀면 되겠다고 생각했다. 그러나 이 문제의 경우 시간복잡도가 n(O^3)를 넘어갈 경우에 시간초과가 떠서 브루트포스 말고 다른 방법으로 풀어야 한다. 어떤 방법으로 풀까 생각하던 중에 이 문제는 인덱스에 관련한 문제가 아니기 때문에 저번에 2Sum에서 써먹지 못..
-
[leetcode 1] Two Sumleetcode 2022. 3. 15. 18:02
를 보고 공부한 내용입니다. https://leetcode.com/problems/two-sum Two Sum - 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) 가장 먼저 생각난것은 다 대입해 보는 브루트 포스 방식이었다. class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)-1): for j in range(i+1,len(n..
-
[leetcode 5] Longest Palindromic Substringleetcode 2022. 3. 15. 00:50
를 보고 공부한 내용입니다. https://leetcode.com/problems/longest-palindromic-substring/ Longest Palindromic Substring - 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 나의 풀이 가장 긴 팬린드롬 부분을 찾는것이므로 문자열길이가 긴 순으로 검사한뒤 그 문자열이 팬린드롬이면 return하도록 하였다. 예를들어 "babad"에서 팬린드름을 찾는다면 문자열길이가 5(len)인 "babad"가 팬..
-
[leetcode 49] Group Anagramsleetcode 2022. 3. 13. 21:27
를 보고 공부한 내용입니다. https://leetcode.com/problems/group-anagrams/submissions/ Group Anagrams - 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 애너그램이라는것을 판단하는 핵심아이디어는 정렬하여 비교하는 것이다. 정렬된 문자열을 키로 하여 딕셔너리에 추가하고 이때 존재하지 않는 키값을 넣어도 에러가 발생하지 않도록 defaultdict()로 선언해준다. class Solution: def group..