본문 바로가기

코딩/leetcode

(8)
[leetcode]22 일일 온도 https://leetcode.com/problems/daily-temperatures/ Daily Temperatures - LeetCode Can you solve this real interview question? Daily Temperatures - Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer leetcode.com 코드 class Solution(object): def dailyTemperatures(..
[leetcode]2번 두 수의 덧셈, 파이썬 문제 https://leetcode.com/problems/add-two-numbers/ Add Two Numbers - LeetCode Can you solve this real interview question? Add Two Numbers - 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 leetcode.com 문제 : 두 수의 덧셈 : 역순으로 저장된 연결 리스트의 숫자를 더하여라 입력 :(2->4->..
[leetcode]206 연결리스트 뒤집기 https://leetcode.com/problems/reverse-linked-list/ Reverse Linked List - LeetCode Can you solve this real interview question? Reverse Linked List - Given the head of a singly linked list, reverse the list, and return the reversed list. Example 1: [https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg] Input: head = [1,2,3,4,5] O leetcode.com 문제 : 단일 연결 리스트를 뒤집어라. 입력 : 1 ->2 ->3->4->5->NULL ..
[leetcode]21 두 연결리스트의 병합 https://leetcode.com/problems/merge-two-sorted-lists/submissions/ LeetCode - The World's Leading Online Programming Learning Platform 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 문제 : 정렬되어 있는 두 연결 리스트를 합쳐라 입력 : 1->2->4, 1->3->4 출력 : 1 ->1->2->3->4->4 문제 해결 방법 1. 두 연결 리스트를 리스트로 변환 후 다시 연결..
[leetcode]234 팰린드롬 연결리스 https://leetcode.com/problems/palindrome-linked-list/ Palindrome Linked List - LeetCode Can you solve this real interview question? Palindrome Linked List - Given the head of a singly linked list, return true if it is a palindrome or false otherwise. Example 1: [https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg] Input: hea leetcode.com 문제 : 연결리스트가 팰린드롬 구조인지 판별하라. 입력 : 1 ->2 ->2-..
[leetcode]238 자신을 제외한 배열의 곱 문제 : 배열을 입력 받아 output[i]가 자신을 제외한 나머지 모든 요소의 곱셈 결과가 되도록 출력하라 입력 :[1,2,3,4] 출력 : [24,12,8,60] 문제를 푸는 과정 1. 리스트의 모든 곱을 구한다. 2. 나로 나누어준다. 3. 나로 나누는 과정에서 중요한 점 -> 0으로 나눌 수 없음 따라서 가장 중요한 것은 리스트에 0이 있느냐 없느냐 1. 리스트에 0이 있는 경우 리스트에 0만 존재하는 경우 리스트에 0과 다른 숫자가 함께 존재하는 경우 리스트에 0이 1개만 존재하는 경우 -> 나를 제외한 곱이기 때문에 0에서는 곱셈 결과가 나올 수 있다. 리스트에 0이 2개 이상 존재하는 경우 -> 0을 하나 제외하더라도 곱셈이기 때문에 다른 0이 있어서 결과에서 언제나 0이 나옴 2. 리스트에..
leetcode 42 , 빗물 트래킹 문제 : 높이를 입력 받아 비 온 후 얼마나 많은 물이 쌓일 수 있는지 계산하라 입력 : [0,1,0,2,1,0,1,3,2,1,2,1] 출력 : 6 코딩을 할 때마다 하는 생각이지만 문제를 풀 때는 항상 수학적 사고력이 가장 중요한 것 같다. 이 문제에 어떻게 접근을 할 것인가. 이후 나의 사고를 파이썬이라는 언어로 새롭게 만들어내는 것이다. 투포인터 알고리즘이란 리스트에 순차적으로 접근해야 할 때 두 개의 점의 위치를 기록하면서 처리하는 알고리즘이다. 병합정렬의 영역의 기초임 한 칸을 기준으로 물을 담을 수 있는 영역은 왼쪽에서 가장 높은 칸, 그리고 오른쪽에서 가장 높은 칸, 그 중 더 작은칸에서 자신이 원래 높이를 뺀 만큼 물을 담을 수 있게 된다. 따라서 자신을 기준으로 왼쪽에서 가장 높은칸을, ..
[leetcode 104] 이진 트리의 최대 깊이 문제 이진 트리의 최대 깊이를 구하라 3 9 20 15 7 문제를 풀기 전에 알고 가야 할 개념 BFS 이진 트리 BFS란? Breadth First Search 의 약자로 너비 우선 탐색 알고리즘이다. 가까운 노드부터 탐색하는 알고리즘 주로 큐(선입선출 방식)인 자료구조를 이용하는 것이 정석 인접한 노드를 큐에 넣도록 알고리즘을 작성하면, 먼저 들어온 것이 먼저 나가게 되며 가까운 노드부터 탐색하게 된다. 기본 구조 from collections import deque # 큐 활용을 deque 라이브러리 활용 def bfs(graph, node, visited): queue = deque([node]) visited[node] = True # 현재 노드를 방문 처리 # 큐가 빌 때까지 반복 while ..