알고리즘 16

[java] Rotate Array

[참고문제] : https://leetcode.com/problems/rotate-array/ Rotate 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 문제 풀이 생각의 흐름 - Circular Linked List 다. 보자마자, Circular Linked List로 구현하면 될 것이란 건 알았다. 다만, 자료구조를 만드는게 목표가 아니니 list로 처리하기로 함. 생각의 흐름 - 어디부터 끊어지는 지 보자. k는 받지만, 모듈러 연산은 해야..

Algorithm 2022.06.24

[java] Median of Two Sorted Arrays

[참고문제] : https://leetcode.com/problems/median-of-two-sorted-arrays/ Median of Two Sorted Arrays - 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 문제 풀이 생각의 흐름 - 이미 정렬되어 있다. 이미 정렬이 되어 있으니, 각각의 배열에서 계속 더 작은 수를 확인한다. 생각의 흐름 - 중앙값 까지만 확인해도 된다. 어짜피 필요한 건 중앙값이고, 중앙값까지만 확인하면 계산할 수 있다. 생각의..

Algorithm 2022.06.23

[java] Longest Substring Without Repeating Characters

[참고 문제] : https://leetcode.com/problems/longest-substring-without-repeating-characters/ Longest Substring Without Repeating Characters - 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 문제 풀이 생각의 흐름 - 저장하자. 필요한 건 가장 긴 길이가 무엇이냐? 그리고 이 문자가 나왔는가? 이 2가지를 저장할 필요가 있었다. 가장 긴 길이 여부는 max 계산을..

Algorithm 2022.06.23

[java] jump game 2

[참고 문제] : https://leetcode.com/problems/jump-game-ii/ Jump Game II - 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 문제 풀이 생각의 흐름 - DP Jump 하여 다음에 갈 위치와, 현재의 Score를 계산하는 방식이 필요할 것이라고 생각했다. 고로, DP로 풀이가 가능할 것이라고 판단 생각의 흐름 - 메모이제이션 무엇을 저장해야 할까가 그 다음 스텝이였다. 위에서 Score를 떠올렸으므로 nums와 같은 길..

Algorithm 2022.06.22

[Python / C++] 스택 수열

[참고 문제 : https://www.acmicpc.net/problem/1874] Target으로 들어오는 값이 현재 보고있는 오름차수 숫자 값과 같거나 더 크면 계속해서 스택에 쌓인다. 더 이상 스택에 쌓일 수 없을 때도 Target이 스택의 Top과 다르면, 뽑을 수 없는 수가 되고, 항상 마지막에는 Pop을 통해 최상위 값을 빼낸다. (Target이거나, 스택에 들어있을 것이므로) N = int(input()) stk = list() result = list() cnt = 1 resultCnt = 0 while resultCnt < 2 * N: target = int(input()) while cnt

Algorithm 2019.05.23

[Python] 소수 찾기

[참고 문제 : https://www.acmicpc.net/problem/1929 ] 문제 풀이 에라스토테네스의 체를 활용 2부터, 알고 싶은 값의 sqrt 값보다 작은 값까지를 범위로 하여, 해당 값이 나누어지는지 비교해보면, 소수 여부를 알 수 있다는 것이다. import math M, N = map(int, input().split()) arr = [] for i in range(M, N+1): if i != 1: flag = True for j in range(2, int(math.sqrt(i)) + 1): if i % j == 0: flag = False break if flag: print(i)

Algorithm 2019.05.23