알고리즘

알고리즘

[백준] 6588번 파이썬 python

시간초과로 고생했다.. 에라토스테네스의 체 구현방법 꼭 숙지하자 import math import sys # 에라토스테네스의 체 == 주어진 범위 내의 소수 리스트 반환하는 효율적인 알고리즘 def sieve_of_eratosthenes(n): prime = [True for _ in range(n+1)] prime[0] = prime[1] = False for i in range(2, int(math.sqrt(n)) + 1): if prime[i] == True: for j in range(i*i, n+1, i): prime[j] = False return prime max_n = 1000000 prime_list = sieve_of_eratosthenes(max_n) while True: n = in..

알고리즘

[프로그래머스] 두 원사이의 정수 쌍/파이썬 python

분류: 구현 ## 틀린 코드 import math def solution(r1, r2): answer = 0 answer += (r2-r1+1)*4 cnt = 0 for i in range(1, r2): for j in range(math.ceil(math.sqrt(r1**(2) - i**(2))),int(math.sqrt((r2**(2) - i**(2))))+1): if j == 0: continue cnt += 1 cnt *= 4 answer = answer + cnt return answer x,y축과 그 사이 점들을 따로 분리해서 구하려고 하였음 - x,y축에 있는 점 갯수 - 1~(r2-1)사이에 있는 점 갯수 x 4 이런식으로 로직을 짰는데 테스트코드에서는 맞았지만 제출 시 런타임 에러.. 일..

알고리즘

[백준] 11726번 파이썬 python

분류 : DP f(n-1)에서 세로 1개씩 붙이는 것과 f(n-2)에서 가로 2개 붙이는 경우를 합하면 된다!

알고리즘

[프로그래머스] 요격 시스템 (파이썬 python)

파란색이 end 지점이 갱신되는 곳임 def solution(targets): targets.sort(key = lambda x: x[0]) end = targets[0][1] answer = 1 for s,e in targets[1:]: if s >= end: answer += 1 end = e else: if e < end: end = e return answer 최단 경로를 구하는 것이 아니므로 BFS는 이 문제 풀이에 맞지 않다..

알고리즘

<백준> 19532번 파이썬 알고리즘

a, b, c, d, e, f = map(int, input().split()) res = [] for x in range(-999, 1000): for y in range(-999, 1000): if (a*x) + (b*y) == c and (d*x) + (e*y) == f: res = [x, y] break for i in range(2): print(res[i], end = ' ') 완전탐색이니까 무식한 방법으로 x,y를 이중 for문으로 돌리면서 해결

알고리즘

[프로그래머스] 폰켓몬 (java 자바)

import java.util.*; class Solution { public int solution(int[] nums) { HashSet set = new HashSet(); // 해쉬셋 초기화 for(int num : nums){ set.add(num); } int cnt = nums.length / 2; if(set.size() > cnt){ return cnt; }else { return set.size(); } } } HashSet 은 중복 제거 해줌

알고리즘

[프로그래머스] 완주하지 못한 선수 (java 자바)

import java.util.*; class Solution { public String solution(String[] participant, String[] completion) { String answer = ""; HashMap map = new HashMap(); // Hash에 추가 for(String tmp : participant){ map.put(tmp, map.getOrDefault(tmp, 0) + 1); } for(String tmp : completion){ map.put(tmp, map.get(tmp) - 1); } for(String tmp : participant){ if(map.get(tmp) == 1){ return tmp; } } return answer; } } ge..

알고리즘

[백준] 13549번 (java 자바)

import java.util.*; class Node{ int end; int time; public Node(int end, int time){ this.end = end; this.time = time; } } public class Main { static final int MAX_N = 100000; static int[] visited; static PriorityQueue q = new PriorityQueue((a, b)->a.time - b.time); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); visited = n..

알고리즘

[백준] 1916번 (java 자바)

import java.util.*; class Node { int end, weight; public Node(int end, int weight){ this.end = end; this.weight = weight; } } public class Main { static final int INF = 987654321; static List[] Graph; static int[] Dist; static int N, M; public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = sc.nextInt(); M = sc.nextInt(); Graph = new ArrayList[N + 1]; Dist = new int[N ..

알고리즘

[백준] 1753번 (java 자바)

내 처음 풀이 [틀림] => 메모리 초과 : 2차원 배열로 인한 듯 하다 import java.util.*; public class Main { static final int INF = 3000000; static int MAX_N = 20001; static int V, E, K; static int[][] Graph; static int[] Dist = new int[MAX_N]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); V = sc.nextInt(); E = sc.nextInt(); K = sc.nextInt(); Graph = new int[V + 1][V + 1]; for(int i = 1; i <..

changha.
'알고리즘' 카테고리의 글 목록