내 맘대로 알고리즘

    June LeetCoding Challenge[Day2] - Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list -- head = [4,5,1,9], which looks like following: 0. 알고리즘을 해결하기 위해서 생각한 방법 - 해당 문제는 현재 노드의 삭제 로직을 구현하는 문제입니다. - node.next = node.next.next; 를 통해서, 다음다음 노드를 가리키게 합니다. - node.val = node.next.val; 을 통해서, 현재 value를 없애주고, 다음 value를 덮어쓰게 되면서 현재 노드는 기존의 다다음노드를 가리키고, 현재 노드가 다음 노드..

    May LeetCoding Challenge[Day22] - Sort Characters By Frequency

    Given a string, sort it in decreasing order based on the frequency of characters. Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. 1. 알고리즘을 해결하기 위해서 생각한 방법 - 우선, 정말 단순하게 생각했다. - 앞에서부터 counting을 하고, counting한 문자에 대한 max를 찾아서 하나씩 retunr하는 것이다. - t:1, r:1, e:2와 같이 map을..

    June LeetCoding Challenge[Day1] - Invert Binary Tree

    Invert Binary Tree 0. 이번 달 목표 - 4월달부터 알고리즘을 시작했습니다. - 5월달에는 4월달만큼, 열심히 하지 않았습니다. - 열심히 하지 않았으니까, 앞으로도 열심히 하지 않을 것 같으니 6월달 목표는 하루 1문제 푸는 것만을 목표로 합니다. 1. 알고리즘을 해결하기 위해서 생각한 방법 - node.left와 node.right를 바꿈 - explore(node.left), explore(node.right)를 통해서 left가 head가 되고, right가 head가 되는 재귀함수를 만든다. 2. 알고리즘 작성 중, 어려운 점 & 깨달은 점 - 없음 3. 내가 작성한 알고리즘 /** * Definition for a binary tree node. * public class Tr..

    May LeetCoding Challenge[Day21] - Count Square Submatrices with All Ones

    Given a m * n matrix of ones and zeros, return how many square submatrices have all ones. Input: matrix = [ [0,1,1,1], [1,1,1,1], [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15. 1. 알고리즘을 해결하기 위해서 생각한 방법 - 해당 문제는 0과 1로 이루어진 m * n 매트릭스가 주어질 때, 사각형이 몇 개인지 찾는 문제이다. - 길이가 1인 사각형부..

    May LeetCoding Challenge[Day20] - Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Input: root = [3,1,4,null,2], k = 1 3 1 4 2 Output: 1 1. 알고리즘을 해결하기 위해서 생각한 방법 - BST가 주어질 때, k번째로 작은 숫자를 찾는 문제 - 잘 모르겠으니, 보고 풀어보자. https://www.youtube.com/watch?v=xBkzsYbLjAA 2. 알고리즘 작성 중, 어려운 점 & 깨달은 점 - 어느 정도, 재귀함수를 사용해서 풀으려고 했는데, 답을 찾고 어떻게 return 해야할 지, 카운트를 어떤 방식으로 누적해야할 지, 왼쪽과 오른쪽 노드로 이동할 때, 체크해..

    May LeetCoding Challenge[Day19] - Online Stock Span

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day. The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price. For example, if the price of a stock over the next 7..

    May LeetCoding Challenge[Day18] - Permutation in String

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string. Input: s1 = "ab" s2 = "eidbaooo" Output: True Explanation: s2 contains one permutation of s1 ("ba"). 1. 알고리즘을 해결하기 위해서 생각한 방법 - 해당 문제는 s1의 짧은 문자열과 s2의 긴 문자열이 있을 때, anagram이 성립되면 true, 그렇지 않으면 flase를 반환 값으로..

    May LeetCoding Challenge[Day17] - Find All Anagrams in a String

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter. Input: s: "cbaebabacd" p: "abc" Output: [0, 6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". The substrin..