반응형

알고리즘(Algorithm) 163

[LeetCode] 5. Longest Palindromic Substring (Java)

문제 Given a string s, return the longest palindromic substring in s. 문자열 s가 주어지면, s에서 가장 긴 회문(palindromic) 부분 문자열을 반환하세요. 회문이란? 회문(回文) 또는 팰린드롬(palindrome)은 거꾸로 읽어도 제대로 읽는 것과 같은 문장이나 낱말, 숫자, 문자열(sequence of characters) 등이다. 보통 낱말 사이에 있는 띄어쓰기나 문장 부호는 무시한다. 출처: 회문 - 위키백과 Example 1: Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer.Example 2: Input: s = "cbbd" Output: "bb"제약 ..

[LeetCode] 4. Median of Two Sorted Arrays (Java)

문제 Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. 크기가 각각 m 및 n인 두 개의 정렬된 배열 nums1 및 num2가 주어질 때, 두 개의 정렬된 배열의 중간값을 반환합니다. The overall run time complexity should be O(log (m+n)). 전체 시간복잡도는 O(log(m+n))이어야 합니다. Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2.Example 2:..

[LeetCode] 3. Longest Substring Without Repeating Characters (Java)

문제 Given a string s, find the length of the longest substring without repeating characters. 문자열 s가 주어지면, 반복되는 문자가 없는 가장 긴 부분 문자열의 길이를 찾으세요. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer ..

[LeetCode] 2. Add Two Numbers (Java)

문제 You are given two non-empty linked lists representing two non-negative integers. 두 개의 음이 아닌 정수를 나타내는 두 개의 비어 있지 않은 링크드리스트가 제공됩니다. The digits are stored in reverse order, and each of their nodes contains a single digit. 숫자는 역순으로 저장되어 있으며, 각 노드에는 단일 숫자가 포함되어 있습니다. Add the two numbers and return the sum as a linked list. 두 숫자를 더하고 합을 링크드리스트로 반환합니다. You may assume the two numbers do not contain a..

[LeetCode] 1. Two Sum (Java)

문제 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 정수 배열 nums와 정수 대상이 주어지면, 대상을 만들수 있는 두 숫자의 인덱스를 반환하세요. You may assume that each input would have exactly one solution, and you may not use the same element twice. 각 입력에는 정확히 하나의 솔루션이 있다고 가정하고, 동일한 요소를 두 번 사용할 수 없습니다. You can return the answer in any order. 어떤 순서로든 정답을 반환할 수..

[백준 Baekjoon] 1153번 네 개의 소수 - JAVA

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static boolean[] isPrime = new boolean[1_000_001]; public static List list = new ArrayList(); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));..

[백준 Baekjoon] 1713번 후보 추천하기 - JAVA

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); int M = Integer.parseInt(br.readLine()); int[] fram..

[백준 Baekjoon] 1990번 소수인팰린드롬 - JAVA

[백준 Baekjoon] 1990번 소수인팰린드롬 - JAVA 문제 풀이 문제에서 주어진 a와 b의 범위가 최대 100,000,000 - 5로 매우 큽니다. 이렇게 큰 범위에서 소수인지 판별할 때는 에라토스테네스의 체를 사용하는 것이 효율적입니다. 다음과 같이 에라토스테네스의 체를 사용하여 소수인지 저장합니다. public static boolean[] isPrime = new boolean[100_000_001]; public static void eratosthenes() { isPrime[0] = isPrime[1] = true; for (int i = 2; i * i

[백준 Baekjoon] 15486번 퇴사 2 - JAVA

[백준 Baekjoon] 15486번 퇴사 2 - JAVA 문제 풀이 다이나믹 프로그래밍을 사용하여 시간복잡도를 O(n)으로 풀어야 하는 문제입니다. 문제에서 주어진 조건 N이 최대 1,500,000으로 시간복잡도가 O(n^2)만 되더라도 시간초과가 발생하게 됩니다. 다음과 같이 다이나믹 프로그래밍을 사용하여 문제를 해결 하였습니다. int[] dp = new int[N + 1]; int max = 0; for (int i = 0; i

반응형