목록알고리즘 (547)
N
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://programmers.co.kr/learn/courses/30/lessons/86052 코딩테스트 연습 - 빛의 경로 사이클 각 칸마다 S, L, 또는 R가 써져 있는 격자가 있습니다. 당신은 이 격자에서 빛을 쏘고자 합니다. 이 격자의 각 칸에는 다음과 같은 특이한 성질이 있습니다. 빛이 "S"가 써진 칸에 도달한 경우, 직진 programmers.co.kr BFS 알고리즘. 처음 풀 때는 DFS를 사용했지만, 런타임 에러 즉, 콜스택이 초과되기 때문에 BFS를 사용해야 한다. 각 좌표마다 시작하는 방향에 따라 사이클의 여부가 달라지기 때문에, 3차원 배열을 이용해 visited를 처리했다. 우선 이중 for문으로 grid를 순회한다. 순회를 하면서 각 좌표마다 4방향으로 탐색을 진행한다..
https://programmers.co.kr/learn/courses/30/lessons/68936 코딩테스트 연습 - 쿼드압축 후 개수 세기 [[1,1,0,0],[1,0,0,0],[1,0,0,1],[1,1,1,1]] [4,9] [[1,1,1,1,1,1,1,1],[0,1,1,1,1,1,1,1],[0,0,0,0,1,1,1,1],[0,1,0,0,1,1,1,1],[0,0,0,0,0,0,1,1],[0,0,0,0,0,0,0,1],[0,0,0,0,1,0,0,1],[0,0,0,0,1,1,1,1]] [10,15] programmers.co.kr 분할 정복 알고리즘 적용. check 함수를 이용해 분할을 할지 결정한다. check의 시작은 출발좌표인 0, 0과 arr의 길이를 넘겨주도록 한다. check 함수. 분할 ..
https://leetcode.com/problems/path-with-maximum-probability/ Path with Maximum Probability - 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 다익스트라 알고리즘. start에서 end까지 갈 수 있는 확률 중 가장 높은 path를 찾아야 한다. 일반적인 다익스트라처럼 큐를 이용. 우선 n 크기의 배열을 선언하고, 빈 배열로 초기화 하여 graph라는 이름으로 선언. 각 지점까지의 거리를 나타..
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://programmers.co.kr/learn/courses/30/lessons/77486 코딩테스트 연습 - 다단계 칫솔 판매 민호는 다단계 조직을 이용하여 칫솔을 판매하고 있습니다. 판매원이 칫솔을 판매하면 그 이익이 피라미드 조직을 타고 조금씩 분배되는 형태의 판매망입니다. 어느정도 판매가 이루어진 후, programmers.co.kr Map을 이용한 문제 풀이. map = enroll과 referral 배열의 부모-자식 관계를 나타내기 위한 Map. profit = 각 사람들의 판매 이익을 나타내기 위한 Map. enroll을 순회하면서 부모-자식 관계를 나타내도록 하자. enroll[i]를 key, referral[i]를 value로 가지도록 하여 map에 저장한다. 이후 seller..
https://leetcode.com/problems/letter-combinations-of-a-phone-number/ Letter Combinations of a Phone Number - 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 각 번호에 맵핑된 문자의 모든 조합을 찾아내는 문제. 완전 탐색으로 풀이. dfs에 넘겨주는 인자는 (현재 문자열, 현재 digits의 인덱스, 현재 문자의 길이)다. dfs 함수. 우선 digits[index]를 numbe..
https://leetcode.com/problems/spiral-matrix/ Spiral Matrix - 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 주어진 배열을 Spiral로 순회하면서 수를 저장하는 문제. m과 n을 각각 matrix의 가로, 세로 길이로 저장. direct는 spiral로 순회하기 위한 방향 배열. visited는 방문 여부 확인 배열. x와 y는 현재 좌표를 뜻하며, d는 direct의 인덱스를 나타낸다. rangeCheck() 함..