본문 바로가기

내 맘대로 알고리즘/LeetCode 2020 May

(22)
May LeetCoding Challenge[Day14] - Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowercase letters a-z.All inputs are guaranteed to be non-empty strings. Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns false trie.startsWith("app"); // returns true trie.insert("app"); trie.search("app"); // returns..
May LeetCoding Challenge[Day13] - Remove K Digits Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: - The length of num is less than 10002 and will be ≥ k. - The given num does not contain any leading zero. Input: num = "1432219", k = 3 Output: "1219" Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest. 1..
May LeetCoding Challenge[Day12] - Single Element in a Sorted Array You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Find this single element that appears only once. Input: [3,3,7,7,10,11,11] Output: 10 1. 알고리즘을 해결하기 위해서 생각한 방법 - 쉬어가는 날인가보다. - 배열이 주어질 때, 배열 중에서 혼자 짝이 되지 않는 숫자를 찾는 문제이다. - 2020/04/20 - [내 맘대로 알고리즘] - LeetCode[day1] - Single Number - 4월에 첫 번째, Sing..
May LeetCoding Challenge[Day11] - Flood Fill An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image. To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of t..
May LeetCoding Challenge[Day10] - Find the Town Judge In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: 1. The town judge trusts nobody. 2. Everybody (except for the town judge) trusts the town judge. 3. There is exactly one person that satisfies properties 1 and 2. You are given trust, an array of pairs trust[i] = [a, b] representing that the pe..
May LeetCoding Challenge[Day9] - Valid Perfect Square Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Input: 16 Output: true 1. 알고리즘을 해결하기 위해서 생각한 방법 - 해당 문제는 input 숫자가 n*n 인 숫자인가에 관련된 문제이다. - 특정 숫자를 고르는 것이기 때문에, 이진탐색을 생각했다. - 특정 숫자를 고르는 문제와 같이, 이진탐색을 하는 조건을 알게 되었다. - 1~N까지 범위가 주어졌을 때, 숫자 하나를 선택할 때 가장 빠르게 찾기 위해서는 가장 적합하다고 생각하게 되었다. ..
May LeetCoding Challenge[Day8] - Check If It Is a Straight Line You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane. Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]] Output: true Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]] Output: false 1. 알고리즘을 해결하기 위해서 생각한 방법 - 우선 주어진 예제에서는 x와 y가 각각 증가하는 숫자에 대해서 보게 되었다. - [1,1]이 [2..
May LeetCoding Challenge[Day7] - Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree. Return true if and only if the nodes corresponding to the values x and y are cousins...