전체 글
-
198. House Robber릿코드(LEETCODE) 2020. 2. 8. 14:34
https://leetcode.com/problems/house-robber/ House Robber - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 강도가 밤사이에 거리에 있는 집을 털을 계획을 하고 있다. 인접한 두 집을 훔치게 되면 자동으로 경찰에 연락이 감으로 인접하지 않은 집을 털 때 가장 많은 금액을 출력하여라. 아무 생각 없이 푼 코드 인접하지만 않게 푼 코드. 역시 무엇인가 놓치는 게 있다 아래 코드는 틀렸다. class Solution { pu..
-
66. Plus One릿코드(LEETCODE) 2020. 2. 7. 17:52
https://leetcode.com/problems/plus-one 불러오는 중입니다... 입력으로 들어오는 배열에서 마지막 자리수에 1을 더한값을 출력 하는 문제. 마지막 글자가 9 인 경우 10이 됨으로 올림을 해줘야 한다. reverse 를 사용하여 배열의 요소를 반대로 돌리고 올림이 생기면 계속 추가 해주는 식으로 풀이 vector plusOne(vector& digits) { // 리버스를 사용하여 배열을 뒤집는다. reverse(digits.begin(), digits.end()); bool first = true; // 처음 1회 마지막 요소에 덧셈을 하기 위한 flag bool up = false; for (int i = 0; i < digits.size(); i++) { int cand..
-
290. Word Pattern릿코드(LEETCODE) 2020. 2. 7. 17:32
https://leetcode.com/problems/word-pattern/ Word Pattern - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com pattern 의 문자열과 str 의 문자열이 pattern 을 이루는지 확인하는 문제 올바른 경우 pattern str 판정 abba cat dog dog cat O aba cat dog cat O aabb cat cat dog dog O 틀린 경우 pattern str 판정 abba dog dog dog do..
-
258. Add Digits릿코드(LEETCODE) 2020. 2. 7. 16:15
https://leetcode.com/problems/add-digits/ Add Digits - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 38 -> 3 + 8 -> 11 -> 1 + 1 -> 2 주어진 숫자를 모두 더해서 한자리 숫자로 만드는 문제 n이 10 이하가 될 때까지 재귀 함수를 통해서 합산한 풀이는 아래와 같다. class Solution { public: int rec(int n) { if (n < 10) { return n; } int re..
-
1317. Convert Integer to the Sum of Two No-Zero Integers릿코드(LEETCODE) 2020. 2. 7. 15:04
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/ Convert Integer to the Sum of Two No-Zero Integers - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 입력으로 들어오는 n의 값을 두 개의 Integer 합의 값을 찾는 문제로 정수에서는 0 이 포함되지 않아야 한다. n의 범위는 2
-
383. Ransom Note릿코드(LEETCODE) 2020. 2. 7. 13:37
https://leetcode.com/problems/ransom-note/ Ransom Note - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 이해 ransomNote의 있는 문자열이 개별 요소가 magazine 문자열에 모두 포함되는지 확인하는 문제 Bruth-force 풀이 ransomeNote, magazine 문자열의 방문 여부를 판단하는 array를 하나 만들고 전체 탐색을 하여 모두 다 방문했는지 못했는지 판단하는 방법으로 1차 풀이는 아래..
-
백준 1547번: 공시뮬레이션(Simulation) 2020. 2. 6. 16:55
https://www.acmicpc.net/problem/1547 1547번: 공 첫째 줄에 컵의 위치를 바꾼 횟수 M이 주어지며, M은 50보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에는 컵의 위치를 바꾼 방법 X와 Y가 주어지며, X번 컵과 Y번 컵의 위치를 서로 바꾸는 것을 의미한다. 컵을 이동시키는 중에 공이 컵에서 빠져나오는 경우는 없다. X와 Y의 값은 3보다 작거나 같고, X와 Y가 같을 수도 있다. www.acmicpc.net swap 을 사용하여 현재 공이 있는 위치를 찾는 문제 키워드 - 구현, 시뮬레이션 Source #include #include #include #include #include #include #include #include #include #include ..
-
백준 1335번: 트럭큐(Queue) 2020. 2. 6. 16:53
https://www.acmicpc.net/problem/13335 13335번: 트럭 문제 강을 가로지르는 하나의 차선으로 된 다리가 하나 있다. 이 다리를 n 개의 트럭이 건너가려고 한다. 트럭의 순서는 바꿀 수 없으며, 트럭의 무게는 서로 같지 않을 수 있다. 다리 위에는 단지 w 대의 트럭만 동시에 올라갈 수 있다. 다리의 길이는 w 단위길이(unit distance)이며, 각 트럭들은 하나의 단위시간(unit time)에 하나의 단위길이만큼만 이동할 수 있다고 가정한다. 동시에 다리 위에 올라가 있는 트럭들의 무게의 합은 다리의 최 www.acmicpc.net 큐를 이용하면 쉽게 풀리는 문제 L -> 다리가 견딜수 있는 무게 W -> 다리의 길이 키워드 - queue Source #include..