본문 바로가기
반응형

혼자 공부하는 것들/알고리즘27

221. Maximal Square 진짜 오래간만에 알고리즘을 풀어봤습니다! 오래간만에 풀어보니, 어렵네요... https://leetcode.com/problems/maximal-square/ Maximal Square - LeetCode Maximal Square - Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example 1: [https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg] Input: matrix = [["1","0","1","0","0"],["1", leetcode.com 문제 자체는 심플합니다. 해당.. 2023. 1. 9.
120. Triangle (DP, DFS로 풀어보자!) https://leetcode.com/problems/triangle/ Triangle - 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 triangle array, return the minimum path sum from top to bottom. For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on.. 2022. 7. 3.
11. Container With Most Water (+Python) 문제: Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. Notice that you may not slant the container. Example 1: Input: height = [1,8,6,2,5.. 2021. 11. 8.
94. Binary Tree Inorder Traversal (leetcode Python) +DFS로 풀기 학부에서 공부했던 Binary Tree에서 lnorder 형식으로 탐색하는 문제가 나왔습니다. 문제: Given the root of a binary tree, return the inorder traversal of its nodes' values. Example Input: root = [1,null,2,3] Output: [1,3,2] tree의 입력값을 받으면 inorder탐색을 하여 경로를 반환하는 문제입니다. 저는 문제를 보고 DFS로 풀면 좋겠다고 생각했습니다. # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.. 2021. 11. 2.
200. Number of Islands (leetcode python) +DFS로 풀기 모 기업의 코딩 테스트 문제로 나왔었는데... DFS만 알면 쉽게 푸는 문제였는데 너무 아쉽다.... 알고리즘은 꾸준히 하는 게 답인 거 같다....ㅠㅠ 문제: Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. example 1 In.. 2021. 10. 29.
2. Add Two Numbers (leetcode python) 문제: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. 두 개의 링크 리스트를 받아와 각 리스트를 역순으로 뒤집어 합을 구한 뒤 다시 링크 리스트로 반환해주는 문제입니다. 알고리즘을 풀면서 .. 2021. 10. 27.
[JS] 프로그래머스 약수의 개수와 덧셈 문제 설명 두 정수 left와 right가 매개변수로 주어집니다. left부터 right까지의 모든 수들 중에서, 약수의 개수가 짝수인 수는 더하고, 약수의 개수가 홀수인 수는 뺀 수를 return 하도록 solution 함수를 완성해주세요. function solution(left, right) { var answer = 0; let arr = []; let arr1 =[]; for(let i =left;i 2021. 10. 21.
유클리드 호제법 (최대공약수, +최소공배수) 유클리드 호제법이란 두 정수의 대해서 최대공약수를 찾아내는 알고리즘입니다. 2개의 자연수(또는 정식) a, b에 대해서 a를 b로 나눈 나머지를 r이라 하면(단, a> b), a와 b의 최대공약수는 b와 r의 최대공약수와 같다. 이 성질에 따라, b를 r로 나눈 나머지 r'를 구하고, 다시 r을 r'로 나눈 나머지를 구하는 과정을 반복하여 나머지가 0이 되었을 때 나누는 수가 a와 b의 최대공약수입니다. const greateat_common = (a,b) => { if(b === 0) return a return greateat_common(b, a%b); } console.log(greateat_common(10,20)); 재귀 함수로 풀어보았습니다. 두 정수 a b를 할당받습니다. 최대공약수를 구할.. 2021. 10. 3.
에라토스테네스의 체 프로그래머스 소수찾기 문제를 풀면서 예전에 사용해보았던 에라토스테네스의 체라는 알고리즘을 다시 한 번 사용해보았습니다. 에라토스테네스의 체를 그림으로 한번 살펴볼까요? 이런 식으로 2의 배수부터 지워나가 3의 배수, 4의 배수... 증가시켜 제거하는 방법입니다. 코드 리뷰를 통해서 확인해보겠습니다. function solution(n) { let arr = Array(n+1).fill(true).fill(false,0,2); //n의 숫자의 크기만큼 array를 할당해주고 true로 채워줍니다. 0,1은 소수가 아니기 때문에 제외해줍니다. for(let i =2; i*i 2021. 9. 2.
반응형