분류 전체보기
-
1/6 기록 - 백준 1297Algorithm 2022. 1. 6. 09:57
오늘부터는 낮은 단계의 문제풀이도 주기적으로 동반하며 머리를 돌려보려고 생각했다. solved.ac에 나와있는 단계를 참고하여 첫날이니 브론즈 단계 정도로 시작해 보았다. 너무 간단해 보이는건 배제하고 글이 조금이라도 있는걸 선택해보자는 마음으로 훑어보다가 백준 1297번 문제를 골라보았다. 문제는 엄청긴데, 막상 읽어보면 별 내용이 없다... 살짝 맥빠짐 ㅋㅋ D, H, W에 입력값을 저장하고 그걸 토대로 값을 버림하여 정수로 출력해주면 끝. 그래도 H, W 가 비율이라 머리로는 계산이 안되어서 종이에 쓰면서 풀어보았다. 피타고라스 정리를 생각해서 문제를 풀어주면 끝날것 같았다. List와 get()을 사용하여 문제를 풀어도 되겠지만, 인덱스 길이가 3개 뿐이라 배열이 더 효율적일 것이라는 생각으로 길..
-
Map 개념 및 예제Algorithm/Hash 2022. 1. 5. 16:53
지난 시간 Set에 이어 이번 시간에는 Map에 대해서 다루어 보았다. Map : 자료를 쌍으로 관리하는데 최적화 된 인터페이스이다. -> key-value쌍으로 이루어져 있고, key값은 유일하고, value 값은 중복될 수 있다. Map map = new HashMap(); 와 같은 형식으로 사용한다. -> put() : set에서 add() 메소드와 유사하며 key, value쌍을 모두 추가해야 하므로 put(~~, ~~) 형식으로 사용한다. remove() : 제거하는 메소드 get(key) : key와 쌍을 이루고 있는 value값 불러오기 containsKey() : 해당 key값이 객체에 존재하는지 확인 간단하게 Map에 대해서 설명을 했고, 예제를 통해 어떤식으로 사용할 수 있는지를 확인해..
-
배열과 List를 활용한 수 찾기Algorithm/List 2021. 12. 31. 11:05
배열과 List를 활용해서 문제를 하나 더 풀어보았다. # 백준 1920 수 찾기 백준 1920번 수 찾기 문제이다. 문제를 정리해 보자면 입력받은 수만큼 정수를 입력받고, 다시 입력받은 수만큼 정수를 입력 받아, 그 두번의 입력과 같은 수는 1, 다른 수는 0을 출력하면 된다. 지난번 포스팅 때 배운 BufferedReader와 StringTokenizer를 이용하여 최대한 시간 단축을 할 수 있는 입력 방식을 적극적으로 사용해 보기로 했다. 처음엔 List를 활용하려고 했으나, ArrayList의 구현 size는 10이기 때문에 길이 5만큼만 입력 받는 이러한 문제에서는 배열이 훨씬 메모리 소모가 적을 것으로 생각하고 배열로 구현했다. 1. 배열을 선언하고 for문과 StringTokenizer를 활..
-
HashSet 예제 #2Algorithm/Hash 2021. 12. 29. 10:59
HashSet을 활용한 예제 하나를 더 풀어보자. #2. 백준 1764 듣보잡 문제 듣도 못한 사람과 보도 못한 사람의 숫자를 입력받고 그만큼씩 사람 이름을 입력받는다. 그리고 듣도 보도 못한 사람을 출력하면 된다. 듣도 못한 사람 집합과 보도 못한 사람 집합의 교집합에 있는 사람 이름을 뽑아 내면 되겠다. 어? 그럼 교집합이면 중복이네? 이 점에 착안해서 set의 contains() 메소드를 활용하여 set에 포함되어 있으면 출력시키는 프로그램을 짜보면 될 듯하다. 시간을 조금 더 단축시키기 위해 BufferedReader와 BufferedWriter를 사용했고, 출력도 bw.write를 사용하여 출력했다. HashSet에 듣도 못한 사람을 입력시키고, str에 보도 못한 사람을 입력시킨다. 그리고 듣..
-
HashSet 개념 및 예제#1Algorithm/Hash 2021. 12. 29. 10:22
저번 시간에 이어 이번 시간에는 Set에 대하여 공부해 보았다. Set인터페이스에는 HashSet, TreeSet, LinkedHashSet이 있는데 오늘은 HashSet에 대해 공부했다. HashSet : List와 비슷한 집합의 개념이지만, 중복을 허용하지 않고 순서가 없다. -> 중복을 허용하지 않는다는 점을 이용하여 ArrayList로 복잡하게 풀 문제들을 HashSet을 이용하여 효율적으로 코딩할 수 있는 경우들이 존재한다. -> 순서가 없다는 점으로 인해 순회를 위해서는 Iterator를 사용한다. 순서가 없어서 index표현이 불가능하므로 get(index) 메소드 대신 hasNext()와 next()를 이용하여 표현을 해야한다. Iterator ir = hashSet.iterator(); ..
-
List 예제 #2Algorithm/List 2021. 12. 25. 14:47
List 예제 #1에 이어서 baekjoon에서 다른 문제도 풀어 보았다. #2. 2605번 줄 세우기 문제 문제를 보자마자 긴 문제에 어려운 건 아닌가 살짝 긴장하고 문제를 읽기 시작했다. 문제를 정리해보면 0을 뽑으면 그 자리에 그대로 있으면 되고, 다른 숫자를 뽑으면 뒤에서 부터 뽑은 숫자만큼 앞으로 가면된다고 한다. 단순히 ArrayList를 구현하고 뒤에서 부터 세어준다는 것만 염두하면 될 것 같았다. list.size() : list의 길이 get(index) : 제거는 하지 않고 해당 index의 수를 뽑아옴 add(num, index) : index에 num을 추가 처음에 푼 방식은 if else 구문을 사용해서 0을 뽑으면 그 자리에 그대로 추가(add), 다른 수를 뽑으면 총 list의..
-
List 개념 및 예제#1Algorithm/List 2021. 12. 25. 14:28
List 인터페이스는 컬렉션 프레임워크의 한 종류로 배열이라고 생각하면 간단하다. 주로 순서가 있는 자료 관리에 유용하게 사용되고 중복을 허용한다. 크게 arrayList, LinkedList, Vector로 구분되고 Stack과 Queue도 List 인터페이스에 속한다. ArrayList : 객체 배열 클래스, List = new ArrayList();와 같은 문법으로 주로 사용 Vector : 멀티 스레드에서 주로 사용하고, 동기화를 제공한다. 동기화 구현시 작업이 이루어지는 자원에 대해 잠금 을 수행하므로 ArrayList보다 수행속도가 느리다. LinkedList : ArrayList보다 중간에 자료를 추가, 제거하는데 시간이 적게 걸린다. LinkedList는 자료의 추가, 제거 시 각 요소가 ..