본문 바로가기

Algorithm85

LeetCode 334. Increasing Triplet Subsequence [Java] https://leetcode.com/problems/increasing-triplet-subsequence/description/ 시나리오 a와b를 Integer.MAX_VALUE로 초기화한다. 배열을 순회하며 현재 요소가 a보다 작거나 같으면 현재 요소를 a에 갱신. 현재 요소가 a보다는 크나 b보다는 작거나 같으면 b 갱신. a와 b 둘보다 크면 배열 내 triplet subsequence가 존재한다는 것이므로 return true; 순회 완료 전까지 return 되지 않으면 return false해준다. 코드 class Solution { public boolean increasingTriplet(int[] nums) { if (nums.length < 3) { return false; } int.. 2024. 2. 5.
LeetCode 238. Product Of Array Except Self https://leetcode.com/problems/product-of-array-except-self [LeetCode - The World&#39;s Leading Online Programming Learning Platform 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](https://leetcode.com/problems/product-of-array-except-self) 내 코드 class Solution { public int[] productExceptSe.. 2024. 2. 4.
LeetCode 1071. Greatest Common Divisor of Strings [Java] https://leetcode.com/problems/greatest-common-divisor-of-strings/description/?envType=study-plan-v2&envId=leetcode-75 입력으로 주어지는 두 문자열의 최대공약수(GCD)를 구하는 문제이다. 두 문자열의 최대공약수란 두 문자열 내 겹치는 가장 작은 덩어리를 의미한다. 예를 들어 ABABAB ABAB라는 두 문자열이 있을 때 이때의 gcd는 AB가 된다. 내 코드 /** 시나리오 1. str1과 str2의 길이 비를 구함 2. 구한 길이 비를 이용해 각 str의 첫번째 덩어리를 구하고(ex: 길이 비==3이면 해당 문자열을 3덩어리로 쪼갬) 나머지 덩어리도 첫번째 덩어리와 일치하는지 구함 3. 각 str의 위 조건이 .. 2024. 2. 4.
[Java]백준 1987 - 알파벳 https://www.acmicpc.net/problem/1987 최초 코드 - 메모리 초과 발생 import java.util.*; import java.io.*; class Main { private static int[] dy = {1, 0, -1, 0}; private static int[] dx = {0, 1, 0, -1}; private static char[][] grid; private static int R; private static int C; private static int answer; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputS.. 2024. 1. 24.
[Java] 백준 20922 - 겹치는 건 싫어 (투포인터) https://www.acmicpc.net/problem/20922 정답 코드 import java.util.*; import java.io.*; class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] NK = br.readLine().split(" "); int N = Integer.parseInt(NK[0]); int K = Integer.parseInt(NK[1]); String[] seqStr = br.readLine().split(" "); //mapToInt는 IntStre.. 2024. 1. 24.
[Java] 백준 14719 - 빗물 https://www.acmicpc.net/problem/14719 정답 코드 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] size = br.readLine().split(" "); int answer = 0; int H = Integer.parseInt(size[0]); int W = Integer.parse.. 2024. 1. 23.
[Java]프로그래머스 - 인사고과 https://school.programmers.co.kr/learn/courses/30/lessons/152995 최초 코드 import java.util.*; class Solution { private int[] biggestA={0,0}; private int[] biggestB={0,0}; public int solution(int[][] scores) { //1 calcBiggestOne(scores); //2. enQueue PriorityQueue pq = new PriorityQueue(); for(int i=0; ibiggestB[1]){ biggestB=scores[i]; } } } private int calcRank(PriorityQueue pq){ int cnt=0; int ra.. 2024. 1. 20.
[Java] 백준 2583 - 영역 구하기 https://www.acmicpc.net/problem/2583 시나리오 사각형의 왼쪽 아래 좌표와 오른쪽 위 좌표를 입력받아 grid의 가로 시작 좌표, 세로 시작좌표를 구하고 가로 길이와 세로 길이를 구한다. 그 다음 구한 정보를 토대로 각 사각형을 grid에 그린다. fillRect() 사각형을 모두 그린 다음에는 grid를 완전탐색하며 bfs를 통해 빈 영역을 조사한다. 이 때 queue에 들어가는 모든 좌표는 곧 빈 영역의 개수가 되므로, while문 반복횟수만큼 카운트하면 그것이 빈 영역의 넓이가 된다. 위 과정을 통해 분리된 영역의 개수와 각 영역의 넓이를 구할 수 있다. 핵심 아이디어 사각형 그리기: 입력으로 주어진 왼쪽 아래 꼭짓점과 오른쪽 위 꼭짓점을 grid좌표로 매핑하고 가로, 세.. 2024. 1. 18.
[Java] 백준 - 11559 Puyo Puyo https://www.acmicpc.net/problem/11559 구조 while(true){ 순회_후_bfs(); fall(); answer++; if(연쇄여부==false){ break; } } import java.util.*; import java.io.*; class Main { private static final int MAX_ROW = 12; private static final int MAX_COL = 6; private static final char[][] grid = new char[MAX_ROW][MAX_COL]; private static final int[] dy = {1, 0, -1, 0}; private static final int[] dx = {0, 1, 0, -1};.. 2024. 1. 14.