본문 바로가기
반응형

알고리즘211

[알고리즘] 가장 먼 노드 포인트 1. 가장 먼 노드 의미는 무엇인가? 어떻게 보면 2가지로 해석을 할 수 있다. 1. BFS를 해서 제일 마지막 거리에 있는 노드들 2. 다익스트라 알고리즘을 써서 최단 거리가 max 인 것들의 노드들 2. 따라서 2가지 방법으로 풀 수 있습니다. 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. BFS #include #include #include #include #include using namespace std; vector map[20010]; int dist[20010]; int bfs() { memset(dist, -1, sizeof (dist)); int max_value = 0; queue q; q.push ({ 1, 0 }); dist[1] = 0; //거리를 이용하여 b.. 2021. 3. 9.
[알고리즘] Jewels and Stones 포인트 1. 해시를 이용하여 Counter 및 개수를 확인하는 방법 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. c++/cpp #include using namespace std; class Solution { public: int numJewelsInStones(string jewels, string stones) { unordered_map map; for (auto ele : stones) { map[ele] += 1; } int cnt = 0; for (auto ele : jewels) { cnt += map[ele]; } return cnt; } }; python class Solution: def topKFrequent(self, nums: List[int], k: int) -> .. 2021. 3. 8.
[알고리즘] Top K Frequent Elements 포인트 1. 힙을 사용하여 어떤 식으로 가장 많은 것들을 받아 올 수 있는지를 확인할 수 있다. 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. c++/cpp #include using namespace std; class Solution { public: vector topKFrequent(vector& nums, int k) { unordered_map map; for(int num : nums){ map[num]++; } vector result; priority_queue pq; for(auto it = map.begin(); it != map.end(); it++){ pq.push(make_pair(it->second, it->first)); if(pq.size() > map.size(.. 2021. 3. 8.
[알고리즘] 로봇 청소기 포인트 1. 로봇 청소기 문제, 구현 순서에 따라서 해결하면 되는 문제 입니다. 🧶문서는 항상 수정 될 수 있습니다. 비판은 환영합니다. python import sys sys.stdin = open('input.txt') dx = [-1, 0, 1, 0] dy = [0, 1, 0, -1] n, m = map(int, input().split()) x, y, dir = map(int, input().split()) data = [list(map(int, input().split())) for _ in range(n)] def turn(d: int) -> int: """ 방향을 바꿔주는 함수 :param d:int :return:int """ if d == 0: return 3 else: return d .. 2021. 3. 7.
반응형