boj-dgsw / ps Goto Github PK
View Code? Open in Web Editor NEWproblem solving ๐๐๐
problem solving ๐๐๐
ํจ์จ์ฑ ํ ์คํธ ์๊ฐ์ด๊ณผ๋๋ค์ ใ ใ
public int solution(int[][] land) {
int answer = 0;
int n = land.length;
int m = land[0].length;
for(int i = 0; i<m; i++) {
int cnt = 0;
boolean[][] visit = new boolean[n][m];
for(int j = 0; j<n; j++) {
if(land[j][i] == 1 && !visit[j][i]) {
Queue<Place> que = new LinkedList<>();
que.add(new Place(j, i));
while(!que.isEmpty()) {
Place place = que.poll();
int x = place.x;
int y = place.y;
if(!visit[x][y]) {
cnt++;
visit[x][y] = true;
if(x + 1 < n && land[x + 1][y] == 1 && !visit[x + 1][y]) {
que.add(new Place(x + 1, y));
}
if(x - 1 > -1 && land[x - 1][y] == 1 && !visit[x - 1][y]) {
que.add(new Place(x - 1, y));
}
if(y + 1 < m && land[x][y + 1] == 1 && !visit[x][y + 1]) {
que.add(new Place(x, y + 1));
}
if(y - 1 > -1 && land[x][y - 1] == 1 && !visit[x][y - 1]) {
que.add(new Place(x, y - 1));
}
}
}
}
}
answer = Math.max(answer, cnt);
}
return answer;
}
static class Place {
int x, y;
Place(int x, int y) {
this.x = x;
this.y = y;
}
}
์ ํ๋ฆด๊น..? ์์๋ ๋ค ๋ง์.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int startX = 0, startY = 0;
char[][] map = new char[r][c];
for(int i = 0; i<r; i++) {
String s = br.readLine();
for(int j = 0; j<c; j++) {
map[i][j] = s.charAt(j);
if(map[i][j] == 'S') {
startX = i;
startY = j;
}
}
}
boolean[][] visit = new boolean[r][c];
Queue<Place> que = new LinkedList<>();
que.add(new Place(startX, startY,0));
int minute = 0;
while(!que.isEmpty()) {
Place place = que.poll();
int x = place.x;
int y = place.y;
int dis = place.dis;
if(minute != dis) {
for(int i = 0; i<r; i++) {
for(int j = 0; j<c; j++) {
if(map[i][j] == '*') {
if(i + 1 < r && map[i + 1][j] == '.') {
map[i + 1][j] = '*';
}
if(i - 1 > -1 && map[i - 1][j] == '.') {
map[i - 1][j] = '*';
}
if(j + 1 < c && map[i][j + 1] == '.') {
map[i][j + 1] = '*';
}
if(j - 1 > -1 && map[i][j - 1] == '.') {
map[i][j - 1] = '*';
}
}
}
}
minute++;
}
if(!visit[x][y]) {
visit[x][y] = true;
if(map[x][y] == 'D') {
System.out.print(dis);
return ;
}
if(x + 1 < r && (map[x + 1][y] == '.' || map[x + 1][y] == 'D')) {
que.add(new Place(x + 1, y, dis + 1));
}
if(x - 1 > -1 && (map[x - 1][y] == '.' || map[x - 1][y] == 'D')) {
que.add(new Place(x - 1, y, dis + 1));
}
if(y + 1 < c && (map[x][y + 1] == '.' || map[x][y + 1] == 'D')) {
que.add(new Place(x, y + 1, dis + 1));
}
if(y - 1 > -1 && (map[x][y - 1] == '.' || map[x][y - 1] == 'D')) {
que.add(new Place(x, y - 1, dis + 1));
}
}
}
System.out.print("KAKTUS");
}
static class Place {
int x, y, dis;
Place(int x, int y, int dis) {
this.x = x;
this.y = y;
this.dis = dis;
}
}
}
๋ง์ํ? ์๊ณ ๋ฆฌ์ฆ์ด ๊ทธ๋ฅ ๋ ์ค๋ฅด๋ ๋๋ก ํด์ ํ๋ ธ์ ์๋ ์์ต๋๋ค.
class Solution{
public int solution(int n, int a, int b){
int answer=0;
for(int i=1;i<n;i*=2){
answer++;
}
int s=1,l=n;
int m=(s+l)/2;
boolean flag=true;
if(a>b){
int temp=a;
a=b;
b=temp;
}
while(flag){
flag=false;
if(s<=a && m>=b){
l=m;
m=(s+l)/2;
flag=true;
}
if(m<a && l>=b){
s=m+1;
m=(s+l)/2;
flag=true;
}
if(flag) answer--;
}
return answer;
}
}
๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/12985#
11%์์ ํ๋ฆฌ๋ค์.. ๋์์ฃผ์ธ์..
๋๋ถ ๋ฏผ์์ด๊ฐ ๊ด๋ฆฌํ๋ ๋์ฅ์ NรM ๊ฒฉ์๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ๋ฏผ์์ด๋ ๋์ฅ์ ๊ด๋ฆฌํ๊ธฐ ์ํด ์ฐ๋ด์ฐ๋ฆฌ๋ง๋ค ๊ฒฝ๋น์๋ฅผ ๋ฐฐ์นํ๋ ค ํ๋ค. ์ด๋ฅผ ์ํด ๋์ฅ์ ์ฐ๋ด์ฐ๋ฆฌ๊ฐ ์ด ๋ช ๊ฐ ์๋์ง๋ฅผ ์ธ๋ ๊ฒ์ด ๋ฌธ์ ๋ค.
์ฐ๋ด์ฐ๋ฆฌ์ ์ ์๋ ๋ค์๊ณผ ๊ฐ๋ค. ์ฐ๋ด์ฐ๋ฆฌ๋ ๊ฐ์ ๋์ด๋ฅผ ๊ฐ์ง๋ ํ๋์ ๊ฒฉ์ ํน์ ์ธ์ ํ ๊ฒฉ์๋ค์ ์งํฉ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. (์ฌ๊ธฐ์ "์ธ์ ํ๋ค"์ ์ ์๋ X์ขํ ์ฐจ์ด์ Y์ขํ ์ฐจ์ด ๋ชจ๋ 1 ์ดํ์ผ ๊ฒฝ์ฐ๋ก ์ ์๋๋ค.) ๋ํ ์ฐ๋ด์ฐ๋ฆฌ์ ์ธ์ ํ ๊ฒฉ์๋ ๋ชจ๋ ์ฐ๋ด์ฐ๋ฆฌ์ ๋์ด๋ณด๋ค ์์์ผํ๋ค.
๋ฌธ์ ๋ ๊ฒฉ์ ๋ด์ ์ฐ๋ด์ฐ๋ฆฌ์ ๊ฐ์๊ฐ ์ด ๋ช ๊ฐ์ธ์ง ๊ตฌํ๋ ๊ฒ์ด๋ค.
์
๋ ฅ :
8 7
4 3 2 2 1 0 1
3 3 3 2 1 0 1
2 2 2 2 1 0 0
2 1 1 1 1 0 0
1 1 0 0 0 1 0
0 0 0 1 1 1 0
0 1 2 2 1 1 0
0 1 1 1 2 1 0
๋ต : 3
O๋ ์ฐ๋ด์ฐ๋ฆฌ, "์ธ์ ํ๋ค"๋ ์ํ์ข์ฐ ๋๊ฐ์
O X X X X X O
X X X X X X O
X X X X X X X
X X X X X X X
X X X X X X X
X X X X X X X
X X O O X X X
X X X X O X X
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] farm = new int[n][m];
for(int i = 0; i<n; i++) {
StringTokenizer st2 = new StringTokenizer(br.readLine());
for(int j = 0; j<m; j++) {
farm[i][j] = Integer.parseInt(st2.nextToken());
}
}
int result = 0;
boolean[][] top = new boolean[n][m];
Set<Grid> topSet = new HashSet<>();
for(int i = 0; i<n; i++) {
for(int j = 0; j<m; j++) {
if(!top[i][j]) {
result++;
topSet.clear();
boolean isTop = true;
boolean[][] visit = new boolean[n][m];
Queue<Grid> que = new LinkedList<>();
que.add(new Grid(i, j));
while(!que.isEmpty()) {
Grid grid = que.poll();
int x = grid.x;
int y = grid.y;
int height = farm[x][y];
if(!visit[x][y]) {
visit[x][y] = true;
int[] xArr = {x - 1, x, x + 1};
int[] yArr = {y - 1, y, y + 1};
topSet.add(new Grid(x, y));
for(int k = 0; k<3 && isTop; k++) {
for(int l = 0; l<3; l++) {
if(k == x && l == y) {
break;
}
if(xArr[k] > -1 && xArr[k] < n && yArr[l] > -1 && yArr[l] < m) {
int compare = farm[xArr[k]][yArr[l]];
if(compare > height) {
result--;
isTop = false;
que.clear();
break;
} else if(compare == height) {
que.add(new Grid(xArr[k], yArr[l]));
}
}
}
}
}
}
if(isTop) {
for(Grid grid : topSet) {
top[grid.x][grid.y] = true;
}
}
}
}
}
System.out.print(result);
}
static class Grid {
int x, y;
Grid(int x, int y) {
this.x = x;
this.y = y;
}
}
}
์ด ์ฝ๋์ ์๊ฐ๋ณต์ก๋๋ฅผ ๊ฐ์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ ๊ณ ๋ฏผํด๋ด ์๋ค. (๋ฌธ์ ํ๊ณ ์ฝ๋ ์ฌ๋ ค์ฃผ์ธ์~ ์ธ์ด ์ ํ X)
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
public class Solution {
public int solution(int[] priorities, int location) {
int answer = 0;
Queue<Process> que = new LinkedList<>();
for(int i = 0; i<priorities.length; i++) {
que.add(new Process(i, priorities[i]));
}
int cnt = 1;
while(!que.isEmpty()) {
AtomicBoolean canWork = new AtomicBoolean(true);
Process process = que.poll();
que.forEach(p -> {
if(p.priority > process.priority) {
canWork.set(false);
}
});
if(canWork.get()) {
if(process.index == location) {
answer = cnt;
}
cnt++;
} else {
que.add(process);
}
}
return answer;
}
static class Process {
int index, priority;
Process(int index, int priority) {
this.index = index;
this.priority = priority;
}
}
}
์๋์ ํ์ด ๋ง๊ณ ๋ ํจ์ฌ ๋ง์ ํ์ด๊ฐ ๋์ฌ ์ ์์๊ฑฐ ๊ฐ์ต๋๋ค.
ํ ๋ฒ ํ์ด๋ณด์ธ์.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int answer = Integer.MAX_VALUE;
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int z = (int) (y * 100.0 / x);
int l = 1;
int r = x;
while(l<=r) {
int m = (l + r) / 2;
int newZ = (int) ((y + m) * 100.0 / (x + m));
if(newZ > z) {
answer = Math.min(answer, m);
r = m - 1;
} else {
l = m + 1;
}
}
System.out.println(answer == Integer.MAX_VALUE ? -1 : answer);
}
}
์๋ฃ๊ตฌ์กฐ์์ด ๊ตฌํํ ์ ์์๊ฒ ๊ฐ์ ํด๋ดค๋๋ฐ, ์ค๋ต๊ณผ ์๊ฐ์ด๊ณผ๊ฐ ๋๋ค์(85์ )
ํ ์ ๊ฐ๋ฅด์ณ์ฃผ์ธ์
class Solution {
public long solution(int cap, int n, int[] deliveries, int[] pickups) {
long answer = 0;
int deliver,pick;
boolean first,last;
while(true){
deliver=cap;
pick=cap;
first=true;
last=true;
for(int i=n-1;i>=0;i--){
if(i==0 && deliveries[i]==0 && pickups[i]==0) return answer;
if(deliveries[i]!=0){
if(first){
first=false;
answer+=(i+1)*2;
}
if(deliveries[i]<=deliver){
deliver-=deliveries[i];
deliveries[i]=0;
}else{
deliveries[i]-=deliver;
deliver=0;
if(last){
n=i+1;
last=false;
}
}
}
if(pickups[i]!=0){
if(first){
first=false;
answer+=(i+1)*2;
}
if(pickups[i]<=pick){
pick-=pickups[i];
pickups[i]=0;
}else{
pickups[i]-=pick;
pick=0;
if(last){
n=i+1;
last=false;
}
}
}
if(pick==0 && deliver==0) break;
}
}
}
}
์์ ์ ์์ค์ ๋ง๊ฒ ๋๋ค ๋ฌธ์ ๋ฅผ ๋ฝ์์ฃผ๋ ํ ํ์ ์ด๋ผ๋ ๋ฐฑ์ค์ฉ ๋ธ๋ผ์ฐ์ ํ์ฅ ํ๋ก๊ทธ๋จ์ ๋๋ค.
์ฌ์ฉ๋ฒ:
ํ ํ์ ์ค์ (ํ์ฅ ํ๋ก๊ทธ๋จ ์ต์
) ๋ด "์๊ณ ๋ฆฌ์ฆ ์ค์ "์์ ์๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ชจ๋ ์ฒดํฌํด์ค ํ
"๋ฌธ์ ์ถ์ฒจ ์ค์ "์์ ๋์ด๋ ๋ฒ์๋ฅผ ์ค์ ํด์ฃผ๋ฉด ๋ฉ๋๋ค. (๋๋จธ์ง๋ ์ ํ ์์ฑ)
๊ทธ๋ฆฌ๊ณ ๋์ ์ถ์ฒจ ์์ฑ์ ๋๋ฅด๋ฉด ์ถ์ฒจ๋ ๋ฌธ์ ๊ฐ ๋์ค๋ ๊ฒ์ด ์๋ ์ถ์ฒจ, ์ฆ ํํฐ๊ฐ ์์ฑ๋ฉ๋๋ค.
๊ทธ ์ํ๋ก ๋ฐฑ์ค์ ์ ์ํ๋ฉด ์ฐํ๋จ์ ํ๋์ ์ ๋ชจ์ ๋ฒํผ์ด ๋์ฌ ๊ฒ์
๋๋ค.
๊ทธ๊ฒ์ ์ฐํด๋ฆญํ๋ฉด 5๊ฐ์ง ์ ๋ ๊ธฐ๋ฅ์ด ๋์ฌํ
๋ฐ ์ฃผ์ฌ์ ๋ฒํผ์ ๋๋ฅด๋ฉด ํ์ฌ ์ค์ ๋ ํํฐ๋ก ๋๋ค ๋ฌธ์ ๊ฐ ๊ฑธ๋ฌ์ ธ์ ๋์ต๋๋ค.
ํํฐ ๋ฐ๊พธ๋ ๊ฒ์ด ๊ท์ฐฎ๋ค๋ฉด ํํฐ์ ์์(๋ฒ์งธ)๋ฅผ ํ์ธํ ํ ๋ฐฑ์ค ์ฌ์ดํธ ๋ด์์ Alt+0~9 ๋ฅผ ๋๋ฌ ๊ทธ ํํฐ๋ก ๊ฑธ๋ฌ์ง ๋ฌธ์ ๊ฐ ๋๋ค์ผ๋ก ๋์ค๊ฒ ํ ์๋ ์์ต๋๋ค.
โ ์ฃผ์์ฌํญ โ (ํผ ์ด)
BOJ ์ค์ ์์ ์๊ณ ๋ฆฌ์ฆ ๋ถ๋ฅ๋ฅผ ๋ฐ๋์ "์ฑ๊ณต์ผ ๊ฒฝ์ฐ๋ง ๋ณด๊ธฐ", "์ฑ๊ณต/๋ถ๋ถ ์ฑ๊ณต์ผ ๊ฒฝ์ฐ๋ง ๋ณด๊ธฐ" ์ค ํ๋๋ก ์ค์ ํด ์ฃผ์ธ์. ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ ํ ํ์ ์ ํด๋น ๋ฌธ์ ๋ฅผ ์ด๋ฏธ ํ๋ฆฐ ๋ฌธ์ ๊ฑฐ๋ ์๊ณ ๋ฆฌ์ฆ ๋ถ๋ฅ๊ฐ ์๋ ๋ฌธ์ ๋ก ๊ฐ์ฃผํ๊ณ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง ์์ต๋๋ค.
๋ง์ฝ BOJ Extended ํ์ฅ ํ๋ก๊ทธ๋จ์ ์ฌ์ฉ ์ค์ด๋ฉด์ ํ ํ์ ํ ๋ง๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ผ์ค ๊ฒฝ์ฐ์๋, BOJ Extended์ ํ ๋ง๋ฅผ '๋ฐ์ ํ ๋ง' ๋ก ์ค์ ํด ์ฃผ์ธ์. ํ ๋ง๊ฐ ์๋ก ์ถฉ๋ํ ์ ์์ต๋๋ค.
์ ์ฉ ํ
๋ง, ์๊ณ ๋ฆฌ์ฆ ๋ณด๊ธฐ ์ ๊ธ๊ณผ ๊ฐ์ ๋๋จธ์ง ๊ธฐ๋ฅ ๋ฐ ์ค์ ๋ฐฉ๋ฒ์ ๊ณต์ ์ฌ์ดํธ๋ฅผ ์ฐธ๊ณ ํด ์ฃผ์ธ์.
์ด๋ฐ ์ค์ ์ด ๊ท์ฐฎ๊ธฐ๋ ํ์ง๋ง ํ ๋ฒ ์ค์ ํด ๋์ผ๋ฉด ๋๋ค ๋ํ์ค ํ๋ ๋ฐ ํธํ๋ ์ฐธ๊ณ ํ์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
๋ฐฑํธ๋ ํน ๊ณต๋ถํ๊ณ ์๋๋ฐ ์ ๊ฐ ๋ถ์กฑํด์ ๋ฐ์์ด ์๋ ์ค๋ฅด๋ค์. ๋์ ๋ถํ๋๋ฆฝ๋๋ค.
https://www.acmicpc.net/problem/15652
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.