본문 바로가기

Algorithm/LeetCode59

LRU cache (Swift) LRU Cache - 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 cache 가 full 이면 가장 오래전에 사용한 애를 cache 에서 쫒아내야한다. O(1) 로 구현해야 한다. 필요한 조건 분석 fast lookup O(1) fast removal O(1) least recently used 기록 fast access / look up 을 생각하면 HashMap 이 생각나야 한다. fast removal / least recently used 를 생각하면 .. 2021. 1. 29.
876. Middle of the Linked List Middle of the 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 링크드 리스트의 중간 값을 들고오는 문제이다. 한바퀴 돌면서 size 를 알고 중간 index까지 한번더 순회 extra array 저장해서 count/2 둘다 딱히 정답같은 느낌은 아니다. 주먹구구 구현 class Solution { func middleNode(_ head: ListNode?) -> ListNode? { var curr = head var arr .. 2021. 1. 28.
이진탐색 Binary Search - 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 탐색 알고리즘 순차탐색 해시구조 BST 이진탐색 class Solution { func search(_ nums: [Int], _ target: Int) -> Int { var head = 0 var tail = nums.count-1 while head 2021. 1. 19.
Traversing Tree (recursive, iterative) Tree traverse 할때 iterative 하게 하려면 Stack 사용 level traverse 의 경우 Queue 사용 Binary Tree 일 경우 node nil 로 loop 가능 N-ary Tree Preorder Traversal - 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 /** * Definition for a Node. * public class Node { * public var val: Int * public var children.. 2021. 1. 18.
LeetCode - Remove Duplicates from Sorted Array 문제 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It does.. 2020. 9. 11.
LeetCode - Fibonacci Number, Climbing Stairs 문제 The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given N, calculate F(N). Example 1: Input: 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: 3 Output: 2 Explanation: F(3) = F(2.. 2020. 9. 10.
LeetCode - Pascal's Triangle II 문제 Given an integer rowIndex, return the rowIndexth row of the Pascal's triangle. Notice that the row index starts from 0. Follow up: Could you optimize your algorithm to use only O(k) extra space? Example 1:Input: rowIndex = 3 Output: [1,3,3,1] Example 2: Input: rowIndex = 0 Output: [1] Example 3: Input: rowIndex = 1 Output: [1,1] Constraints: 0 2020. 9. 10.
LeetCode - Search in Binary Search Tree 문제 Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL. For example, Given the tree: 4 / \ 2 7 / \ 1 3 And the value to search: 2 You should return this subtree: 2 / \ 1 3 In the example above, if we want to sea.. 2020. 9. 10.
LeetCode - Reverse Linked List 🤦🏻 문제 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? Iteratively - O(N) Time, O(1) Space /** * Definition for singly-linked list. * public class ListNode { * public var val: Int * public var next: ListNode? * public init() { self.val = 0; self.next = n.. 2020. 9. 9.
LeetCode - Swap Nodes in Pairs Swap Nodes in Pairs - 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 문제 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be changed. Example: Given 1->2->3->4, you should return the lis.. 2020. 9. 3.
LeetCode - Reverse String Reverse String - 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 문제 Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) e.. 2020. 9. 3.
LeetCode - Largest Time for Given Digits Largest Time for Given Digits - 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 문제 Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elap.. 2020. 9. 1.
LeetCode - Running Sum of 1d Array Running Sum of 1d Array - 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 문제 Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums. Example 1: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running .. 2020. 8. 30.
LeetCode - Detect Capital Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cas.. 2020. 8. 7.
💯 Daily LeetCode Challenge Day_20 - Remove Linked List Elements Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 Swift 로 풀려니 Optional 처리가 걸린다... 2020. 7. 29.
💯 Daily LeetCode Challenge Day_19 - Add Binary Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11",.. 2020. 7. 29.
💯 Daily LeetCode Challenge Day_18 - Course Schedule 2 Account Login - 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 문제 There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given th.. 2020. 7. 24.
💯 Daily LeetCode Challenge Day_17 - Top K Frequent Elements Account Login - 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 문제 Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Note: You may assume k is always .. 2020. 7. 18.
💯 Daily LeetCode Challenge Day_16 - Pow(x, n) Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Output: 1024.00000 Example 2: Input: 2.10000, 3 Output: 9.26100 Exampl.. 2020. 7. 18.
💯 Daily LeetCode Challenge Day_15 - Reverse Words in a String Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 Given an input string, reverse the string word by word. Example 1: Input: "the sky is blue" Output: "blue is sky the" Example 2: Input: " hello world! " Output: "world!.. 2020. 7. 16.
💯 Daily LeetCode Challenge Day_14 - Angle between hands of a clock Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제 Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand. Example 1: Input: hour = 12, minutes = 30 Output.. 2020. 7. 14.