Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

김미서 / 10월 5주차 / 월 #313

Merged
merged 3 commits into from
Nov 2, 2023
Merged

Conversation

seomiii
Copy link
Contributor

@seomiii seomiii commented Oct 30, 2023


🎈boj 22865 - 가장먼곳


🗨 해결방법 :


✔코드 :

import java.util.*;
import java.io.*;

class Node{
    int idx;
    int cost;

    Node(int idx, int cost){
        this.idx = idx;
        this.cost = cost;
    }
}
public class BOJ22865_가장먼곳 {
    static int N,M;
    static int[] abc,a,b,c;
    static ArrayList<ArrayList<Node>> graph;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        abc = new int[3];
        graph = new ArrayList<>();
        for (int i=0; i<N+1; i++){
            graph.add(new ArrayList<>());
        }

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i=0; i<3; i++){
            abc[i] = Integer.parseInt(st.nextToken());
        }

        M = Integer.parseInt(br.readLine());
        for (int i=0; i<M; i++){
            st = new StringTokenizer(br.readLine());
            int from = Integer.parseInt(st.nextToken());
            int to = Integer.parseInt(st.nextToken());
            int dis = Integer.parseInt(st.nextToken());

            graph.get(from).add(new Node(to,dis));
            graph.get(to).add(new Node(from,dis));
        }

        a = new int[N+1];
        b = new int[N+1];
        c = new int[N+1];
        solve(a, abc[0]);
        solve(b, abc[1]);
        solve(c, abc[2]);

        int result = -1;
        int resultIdx = -1;

        for (int i=1; i<=N; i++){
            int aa = a[i];
            int bb = b[i];
            int cc = c[i];

            int dis = Math.min(Math.min(aa,bb), cc);
            if (result < dis){
                result = dis;
                resultIdx = i;
            } else if ( result == dis && resultIdx > i){
                resultIdx = i;
            }
        }
        System.out.println(resultIdx);
    }

    private static void solve(int[] distance, int start){
        for (int i=0; i<N+1; i++){
            distance[i] = Integer.MAX_VALUE; // 최소거리 정보 최소화
        }

        PriorityQueue<Node> q = new PriorityQueue<>(
                (o1,o2)-> Integer.compare(o1.cost, o2.cost));
        q.offer(new Node(start, 0));
        distance[start] = 0; // 출발지점 - 0으로 초기화
        while(!q.isEmpty()){
            Node cur = q.poll();
            if(distance[cur.idx] < cur.cost){
                continue;
            }

            for (int i=0; i<graph.get(cur.idx).size(); i++){
                Node next = graph.get(cur.idx).get(i);
                if (distance[next.idx] > cur.cost + next.cost){
                    distance[next.idx] = cur.cost + next.cost;
                    q.offer(new Node(next.idx, distance[next.idx]));
                }
            }

        }

    }
}


🎈boj 17298 - 오큰수


🗨 해결방법 :


📝메모 :

  • 반드시 순서대로 오큰수를 구해야 한다는 생각을 버리면 좋을 것 같다는 생각을 했습니다.
  • 숫자가 크고, n으로 끝내야 하는 경우라면 스택을 생각해보면 좋을 것 같습니다.

✔코드 :

import java.util.*;
import java.io.*;

/**
 * 반드시 순서대로 오큰수를 구해야 한다는 생각을 버리면 좋을 것 같다는 생각을 했습니다.
 * 숫자가 크고, n으로 끝내야 하는 경우라면 스택을 생각해보면 좋을 것 같습니다.
 */
public class BOJ17298_오큰수 {
    static int N;
    static int[] answers;
    static Stack<int[]> stack = new Stack<>();
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        N = Integer.parseInt(br.readLine());
        answers = new int[N];
        StringTokenizer st = new StringTokenizer(br.readLine());

        for (int i=0; i<N; i++){
            int cur = Integer.parseInt(st.nextToken());

            if (stack.isEmpty()){ // 스택이 비어있으면
                stack.push(new int[] {i, cur}); // 인덱스, 값, 정답
            }
            else{ // 스택이 차있으면
                while(!stack.isEmpty() && stack.peek()[1] < cur){
                    int[] small = stack.pop();
                    answers[small[0]] = cur; // 오큰수
                }
                stack.push(new int[]{i,cur});
            }
        }

        while(!stack.isEmpty()){
            int[] pops = stack.pop();
            answers[pops[0]] = -1;
        }
        for(int i=0; i<N; i++){
            sb.append(answers[i]).append(" ");
        }
        System.out.println(sb.toString());
    }
}

@seomiii seomiii requested review from hgene0929 and nahokyun October 30, 2023 12:52
@seomiii seomiii merged commit 8ba3308 into SSAFY-10th-Seoul17:main Nov 2, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants