본문 바로가기

알고리즘

(100)
플로이드의 토끼와 거북이 알고리즘(Floyd's hare and tortoise) 증명 토끼와 거북이 알고리즘은 선형으로 이루어졌거나 링크드리스트 자료구조에서 Cycle 이 존재하는지 찾아내고 그 Cycle 의 시작지점을 알아내는데 유용하다 이 알고리즘을 요약하면 거북이는 한 번에 한 칸을 가고 토끼는 한 번에 두 칸을 보낼때 만약 Cycle 이 존재한다면 둘은 결국 만난다. Cycle 이 존재하지않는다면 토끼가 null 이 된다. 이 알고리즘을 수학적으로 증명해보자 Cycle 이 존재하지 않는 경우 토끼가 리스트의 끝으로 가서 결국 null 을 가리키게되는 건 자명하므로 생략한다. 토끼가 두 칸을 가고 거북이가 한 칸을 갈 때 둘은 무조건 만난다. 용어를 정의하자 토끼가 움직이는 거리를 h(hare 의 줄임말) 거북이가 움직이는 거리를 t(tortoise 의 줄임말) 라고 하자 그러면 h..
Leetcode 2359. Find Closest Node to Given Two Nodes - kotlin https://leetcode.com/problems/find-closest-node-to-given-two-nodes/ Find Closest Node to Given Two Nodes - LeetCode Find Closest Node to Given Two Nodes - You are given a directed graph of n nodes numbered from 0 to n - 1, where each node has at most one outgoing edge. The graph is represented with a given 0-indexed array edges of size n, indicating that there is a dire leetcode.com class Soluti..
leetcode snake and ladders - kotlin https://leetcode.com/problems/snakes-and-ladders Snakes and Ladders - LeetCode Snakes and Ladders - You are given an n x n integer matrix board where the cells are labeled from 1 to n2 in a Boustrophedon style [https://en.wikipedia.org/wiki/Boustrophedon] starting from the bottom left of the board (i.e. board[n - 1][0]) and alternati leetcode.com class Solution { fun snakesAndLadders(board: Arra..
Leetcode 127 Word Letter https://leetcode.com/problems/word-ladder/ Word Ladder - 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 Solving this, I got so many things. First, if we go BFS, we can use only one Queue. val q:Queue = LinkedList(0 while(q.isNotEmpty()){ var size = q.size() while(size--){ ... } } ..
백준 2098 외판원 순회문제 https://www.acmicpc.net/problem/2098 2098번: 외판원 순회 첫째 줄에 도시의 수 N이 주어진다. (2 ≤ N ≤ 16) 다음 N개의 줄에는 비용 행렬이 주어진다. 각 행렬의 성분은 1,000,000 이하의 양의 정수이며, 갈 수 없는 경우는 0이 주어진다. W[i][j]는 도시 i에서 j www.acmicpc.net import kotlin.math.min class Solution { val IMPOSSIBLE = 100000000 var nOfCity: Int = 0 lateinit var city: Array lateinit var dp: Array fun input() { nOfCity = readLine()!!.toInt() city = Array(nOfCity)..
[LeetCode] 13. Roman to Integer https://leetcode.com/problems/roman-to-integer/ Roman to Integer - 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 이거보다 좋은 코드를 잘 모르겠다 ㅋㅋ object Solution { def romanToInt(s:String): Int = { if(s.isEmpty) 0 else if (s.startsWith("CM")) 900 + romanToInt(s.substring(2)) else if (s.star..
백준 20055 컨베이어 벨트 위의 로봇(Java) https://www.acmicpc.net/problem/20055 20055번: 컨베이어 벨트 위의 로봇 길이가 N인 컨베이어 벨트가 있고, 길이가 2N인 벨트가 이 컨베이어 벨트를 위아래로 감싸며 돌고 있다. 벨트는 길이 1 간격으로 2N개의 칸으로 나뉘어져 있으며, 각 칸에는 아래 그림과 같이 1부�� www.acmicpc.net 삼성 기출문제다.(2021년 상반기용) 주어진 조건대로 구현하면 된다! import java.io.*; import java.lang.*; import java.util.*; public class boj20055 { static int N, K, turn; static int[] conveyor; static ArrayList robots = new ArrayList()..
백준 15653 구슬 탈출 4(JAVA) https://www.acmicpc.net/problem/15653 15653번: 구슬 탈출 4 첫 번째 줄에는 보드의 세로, 가로 크기를 의미하는 두 정수 N, M (3 ≤ N, M ≤ 10)이 주어진다. 다음 N개의 줄에 보드의 모양을 나타내는 길이 M의 문자열이 주어진다. 이 문자열은 '.', '#', 'O', 'R', 'B' www.acmicpc.net 면접 준비때문에 너무 바쁜걸... 간만에 한 문제 풀었다 ㅠㅠ 이 문제는 주어진 조건대로 풀면된다! 간단함! import java.io.*; import java.util.Arrays; import java.util.Objects; import java.util.StringTokenizer; public class boj15653 { static ..
백준 13334 철로(JAVA) https://www.acmicpc.net/problem/13334 13334번: 철로 입력은 표준입력을 사용한다. 첫 번째 줄에 사람 수를 나타내는 양의 정수 n (1 ≤ n ≤ 100,000)이 주어진다. 다음 n개의 각 줄에 정수 쌍 (hi, oi)가 주어진다. 여기서 hi와 oi는 −100,000,000이상, 100,000,0 www.acmicpc.net 끝점을 기준으로 정렬하고 우선순위 큐를 활용한다. 끝점을 기준으로 오름차순 정렬을 하면 아래 그림처럼 된다. 이 상태에서 집과 회사 중 더 작은 값을 기준으로 정렬한다. 여기서 유의해야하는게, 무조건 집이 회사보다 왼쪽에 있다고 생각하기 쉬운데 그렇지 않다. 그림으로 보자면 이렇게 두 가지 경우가 가능하다. 그러므로 입력값을 받을 때 대소 비교를..
백준 1938 통나무 옮기기 https://www.acmicpc.net/problem/1938 1938번: 통나무 옮기기 첫째 줄에 주어진 평지의 한 변의 길이 N이 주어진다. (4