Eat Study Love

먹고 공부하고 사랑하라

coding 76

Max - heap을 Tree형태로 표현하기

C++ 알고리즘의 대표적인 자료구조인 Priority Queue의 근간이 되는 Heap 구조를 Complete Binary Tree형태로 표현하였다. Array가 아닌 Linked list로 표현하여서 Time complexity는 모두 enqueue / dequeue O( log N ) 이고, 코드를 구현하며 주의해야할 점은, heap property를 깨지 않기 위해 en/dequeue를 할 때마다 heapify-up/down을 시행해야 한다는 것이다. Enqueue는 그래서 일단 element를 Tree의 Last Node에 삽입하고, 그 친구를 Heap - up 하며 Dequeue는 일단 root와 last-node를 스위칭 한 다음에 last-node를 제거한다. 그리고 root자리에 있는 친구..

Convert Sorted List to Binary Search Tree(Linked List,Divide and Conquer,Tree,Binary Search Tree,Binary Tree)

https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree. Example 1:Input: head = [-10,-3,0,5,9]Output: [0,-3,9,-10,null,5]Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.Example ..

Coding_Practice 2025.02.17

SSSP(Single Source Shortest Path) / APSP(All-Pairs Shortest Path) 실습

Algorithm의 대명사인 Shortest Path와 관련된 실습이다.SSSP / APSP를 구현해보는 실습을 할 것이다. 대표적으로 FloydWarshall과 BellmanFord 알고리즘을 확인해보자. 실습자료는 위와 같다.#include #include #include #include // 아래 header는 내가 추가#include using namespace std;/*//////////////////////// Description ////////////////////////////There are n cities in a logistics network, numbered from 0 to n−1. The edges array represents the logistics routes bet..

Type Casting & Exception Handling + OOP 실습

C++을 이용해서 Type Casting 과 Exception Handling을 해보겠다. Type Casting이란 특정 Variable을 다른 data type으로 변화시키는 것이며 종류로는 1. C-style Casting2. Static cast3. Dynamic cast4. Const cast5. Reinterpret cast 이렇게 다섯가지 정도를 알아보자.Elec* elec = new pikachu();Fire* char = (Fire*)elec;// Compile error는 없지만 ERROR static_cast 으로 compile time에 casting을 걸어버려서 깔끔하다.  Exception Handling은 Try - Catch 구문이다.일단 try 안쪽에 있는 구문을 실행해보고..

Permutations(Array,Backtracking)

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1:Input: nums = [1,2,3]Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]Example 2:Input: nums = [0,1]Output: [[0,1],[1,0]]Example 3:Input: nums = [1]Output: [[1]] Constraints:1 -10 All the integers of nums are unique.뭔가 기본적인 컨셉의 문제인데, 이게 또 한 번쯤은 집고 넘어가줘야 찝찝한게 없다..

Coding_Practice 2025.02.10