전체 글
-
[LeetCode] Wildcard Matching알고리즘 2019. 10. 24. 22:39
문제 Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letter..
-
[LeetCode] Trapping Rain Water알고리즘 2019. 10. 23. 17:56
문제 Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Appr..
-
[LeetCode] First Missing Positive알고리즘 2019. 10. 22. 23:56
문제 Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1] Output: 2 Example 3: Input: [7,8,9,11,12] Output: 1 Note: Your algorithm should run in O(n) time and uses constant extra space. Approach 메모리를 O(n)만큼 더 쓰면 map등을 사용해서 쉽게 풀겠지만 상수 크기만큼밖에 사용하지 못하므로 어려운 문제. 답은 Hash를 제시된 배열에 직접 구현하는 것이었다. 배열을 탐색하는 index를 i, 제시..
-
[LeetCode] Count and Say알고리즘 2019. 10. 22. 16:48
문제 The count-and-say sequence is the sequence of integers with the first five terms as following:1. 1 2. 11 3. 21 4. 1211 5. 111221 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. Note: Each term of the sequence of integers will be represented..
-
[LeetCode] Valid Sudoku알고리즘 2019. 10. 21. 20:31
문제 Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. A partially filled sudoku which is valid. The Sudoku board could be par..
-
[LeetCode] Find First and Last Position of Element in Sorted Array알고리즘 2019. 10. 21. 17:08
문제 Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] Approach 배열..
-
[LeetCode] Search in Rotated Sorted Array알고리즘 2019. 10. 15. 02:50
문제 Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Your algorithm's runtime complexity must be in the order of O(log n). Example 1: Input: nums = [..
-
[LeetCode] Longest Valid Parentheses알고리즘 2019. 10. 9. 16:41
문제 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: "(()" Output: 2 Explanation: The longest valid parentheses substring is "()" Example 2: Input: ")()())" Output: 4 Explanation: The longest valid parentheses substring is "()()" Approach 우선 Valid parentheses의 조건을 잘 생각해봐야한다. ')'가 나왔을 때 그 바로 전에 '(..