목록leet code (85)
N
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/ Flatten a Multilevel Doubly Linked List - 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 주어진 양방향 리스트를 평탄화를 하여 리턴해야 한다. 우선 예외처리로 head가 비어있다면 그대로 비어있는 head를 리턴한다. 그렇지 않다면 DFS로 모든 노드를 배열에 담아주도록 한다. 담는 순서는 현재 방문한 노..
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ Find First and Last Position of Element in Sorted Array - 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 이진 탐색 알고리즘 활용. 주어진 nums에서 target을 찾고, 해당 인덱스의 처음과 끝을 answer에 저장하면 된다. 만약 nums가 비어있다면 바로 리..
https://leetcode.com/problems/swapping-nodes-in-a-linked-list/ Swapping Nodes in a Linked List - 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 연결 리스트. 앞에서 k번째에 있는 노드와 뒤에서 k 번째에 있는 노드를 서로 바꿔주면 된다. array를 이용하여 문제 해결. arr 배열 안에 head에 있는 노드를 모두 넣어준다. 이후 p1 = k - 1, p2 = arr.length - ..
https://leetcode.com/problems/cheapest-flights-within-k-stops/ Cheapest Flights Within K Stops - 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 다익스트라 알고리즘 활용. 우선 graph라는 이름의 n 크기의 배열을 선언하여 []로 초기화. 또한, n 크기의 dist 배열도 선언하여 infinity로 초기화. flights를 순회하여 그래프를 그려준다. 단방향이기 때문에 from, to,..
https://leetcode.com/problems/flood-fill/ Flood Fill - 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 BFS 알고리즘. m x n 크기의 visited 배열을 선언하여 false로 초기화. 4 방향을 탐색하기 위한 direct 배열 또한 선언. color는 image[sr][sc]에 해당하는 숫자로, 탐색을 할 때 해당 숫자와 같으면 newColor로 바꾼다. image[sr][sc] = newColor로 하고, que..
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Populating Next Right Pointers in Each Node - 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 BFS를 이용. BFS에 이용할 queue와 같은 레벨에 있는 노드를 담을 stack을 빈 배열로 선언. answer = root로 정의. 또 다른 pointer를 root로 지정하고, count는 현재..
https://leetcode.com/problems/di-string-match/ DI String Match - 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 투포인터 알고리즘 적용. 답을 리턴할 배열 answer을 선언. front와 back을 이용해 답을 찾을 것이다. front = 0, back = s.length으로 선언한 후 for문 진행. 만약 현재 문자가 "I"라면 front++를 answer에 push. 그렇지 않고 "D"라면 back--를 a..
https://leetcode.com/problems/lemonade-change/ Lemonade Change - 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 그리디 알고리즘. 5, 10, 20달러를 받았을 때 거스름돈을 남길 수 있는지 확인하면 된다. money라는 이름으로 3의 길이만큼 선언. 0의 인덱스부터 5, 10, 20 달러를 가진다. for문으로 bills를 순회. 만약 5달러를 받았다면 거스름돈은 없기 때문에 money[0]++. 10달러를 받..