robert-min / dev-blog

김민준 - project 정리한 repo입니다.
0 stars 0 forks source link

231114 - 탐욕법 : 배열 -> 리스트.contians(), 아스키코드, 좌우이동, 큰 수 만들기 #109

Open robert-min opened 1 year ago

robert-min commented 1 year ago

https://school.programmers.co.kr/learn/courses/30/lessons/42862?language=java

체육복 (배열 -> 리스트.contians())

class Solution {

public int solution(int n, int[] lost, int[] reserve) {
    // 1
    int[] student = new int[n+2];
    List<Integer> lostList = new ArrayList<>(Arrays.stream(lost)
            .boxed().collect(Collectors.toList()));
    List<Integer> reserveList = new ArrayList<>(Arrays.stream(reserve)
            .boxed().collect(Collectors.toList()));

    // 2
    for (int i = 1; i <= n; i++) {
        student[i] = 1;
        if (lostList.contains(i)) {
            student[i]--;
        }
        if (reserveList.contains(i)) {
            student[i]++;
        }
    }

    // 3

    for (int i = 1; i <= n; i++) {
        if (student[i] == 0) {
            if (student[i-1] >= 2) {
                student[i-1]--;
                student[i]++;
            } else if (student[i+1] >= 2) {
                student[i+1]--;
                student[i]++;
            }
        }
    }

// System.out.println(Arrays.toString(student)); int answer = 0; for (int i = 1; i <= n; i++) { if (student[i] >= 1) { answer++; } }

    return answer;
}

}

robert-min commented 1 year ago

https://school.programmers.co.kr/learn/courses/30/lessons/42860?language=java

조이스틱(아스키코드, 좌우이동)

robert-min commented 1 year ago

https://school.programmers.co.kr/learn/courses/30/lessons/42883?language=java

큰 수 만들기

robert-min commented 1 year ago

https://school.programmers.co.kr/learn/courses/30/lessons/42885?language=java#

구명보트

class Solution { public int solution(int[] people, int limit) {

    Arrays.sort(people);
    // 1. 최대 2명만 탑승할 수 있기 때문에 가장 가벼운 1명 + 무거운 1명이 탑승할 수 있는지 확인
    int answer = 0;
    int start = 0;
    int end = people.length - 1;
    while (start <= end) {
        if (people[start] + people[end] <= limit) {
            start++;
        }
        // 2. limit을 초과하면 무거운 사람만 배를 탐.
        end--;
        answer++;
    }

    return answer;
}

}

robert-min commented 1 year ago

https://school.programmers.co.kr/learn/courses/30/lessons/42861

섬 연결하기

// 0 class Node implements Comparable { int nodeNum; int weight;

public Node(int nodeNum, int weight) {
    this.nodeNum = nodeNum;
    this.weight = weight;
}

@Override
public int compareTo(Node o) {
    return weight - o.weight;
}

}

class Solution {

static List<ArrayList<Node>> graph;
static boolean[] visited;
static int[] dist;

static void dikstra(int n,int start){
    PriorityQueue<Node> Q = new PriorityQueue<>();
    visited = new boolean[n];
    dist = new int[n];

    Q.add(new Node(start, 0));
    while (!Q.isEmpty()) {
        Node node = Q.poll();
        int now = node.nodeNum;
        int w = node.weight;

        if (!visited[now]) {
            visited[now] = true;
            dist[now] = w;

            for (Node nx : graph.get(now)) {
                Q.add(new Node(nx.nodeNum, nx.weight));
            }
        }
    }

}

public int solution(int n, int[][] costs) {

    // 1
    graph = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        graph.add(new ArrayList<>());
    }
    for (int i = 0; i < costs.length; i++) {
        int u = costs[i][0];
        int v = costs[i][1];
        int w = costs[i][2];

        graph.get(u).add(new Node(v, w));
        graph.get(v).add(new Node(u, w));
    }

    // 2
    dikstra(n, 0);
    // System.out.println(Arrays.toString(dist));

    // 3
    int answer = 0;
    for (int i = 1; i < n; i++) {
        answer += dist[i];
    }

    return answer;
}

}