알고리즘
-
[LeetCode] Palindrome Partitioning알고리즘 2019. 12. 7. 15:57
문제 Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example: Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ] Approach DFS로 string을 i글자씩 잘라가며 palindrome인지 확인한 후 palindrome이면 자르고 남은 string을 재귀함수를 통해 다시 잘라 확인하여 최종적으로 자른게 모두 palindrome이면 답 배열에 추가한다. Code https://github.com/chi3236/algorithm/blob/master/Leet..
-
[LeetCode] Surrounded Regions알고리즘 2019. 12. 7. 04:12
문제 Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. Example: X X X X X O O X X X O X X O X X After running your function, the board should be: X X X X X X X X X X X X X O X X Explanation: Surrounded regions shouldn’t be on the border, which means that any 'O' on the border..
-
[LeetCode] Longest Consecutive Sequence알고리즘 2019. 12. 6. 21:54
문제 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. Approach 배열이 sorting되어있으면 아주 쉽다. priority queue를 활용한 heap에 배열을 옮겨 담고 하나씩 빼면서 연속인지 아닌지 확인하면 됨 하지만 이 방법은..
-
[LeetCode] Single Number알고리즘 2019. 12. 6. 16:14
문제 Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1,2] Output: 4 Approach xor 비트연산으로 푼다. Code https://github.com/chi3236/algorithm/blob/master/LeetCode_SingleNumber.c c..
-
[LeetCode] Word Ladder알고리즘 2019. 12. 6. 02:40
문제 Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time. Each transformed word must exist in the word list. Note that beginWord is not a transformed word. Note: Return 0 if there is no such transformation sequence. All words have the same length. Al..
-
[LeetCode] Valid Palindrome알고리즘 2019. 12. 5. 17:00
문제 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a canal: Panama" Output: true Example 2: Input: "race a car" Output: false Apporach isdigit(), isalpha()로 알파벳이나 숫자가 아닌것은 다 거르면서 앞글자 뒷글자를 맞춰보면 된다 isdigit(), isalpha() 두개..
-
[LeetCode] Binary Tree Maximum Path Sum알고리즘 2019. 12. 5. 01:53
문제 Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root. Example 1: Input: [1,2,3] 1 / \ 2 3 Output: 6 Example 2: Input: [-10,9,20,null,null,15,7] -10 / \ 9 20 / \ 15 ..
-
[LeetCode] Best Time to Buy and Sell Stock II알고리즘 2019. 12. 4. 18:35
문제 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [7,1,5..