알고리즘(170)
-
[프로그래머스] [C++] 입국심사*** (binary search)
1. 문제 심사하는 직원들은 각각 심사하는데 시간이 전부 다르다 이 시간들의 배열이 주어지고 입국 심사해야하는 사람의 수 n이 주어진다. 이때 모든 사람을 심사했을때 걸리는 최소의 시간을 구하여라. 2. 큐를 활용한 풀이 => 시간 초과 이는 그리디 적으로 풀 수 있다. 모든 사람이 가장 빨리 끝나는 심사 위원을 선택만 하기만하면된다. 따라서 모든 시간을 우선순위 큐에 넣어놓고 n번을 그리디적으로 선택 하는 것이다. 그 후 가장 긴 시간을 찾으면 된다. 이는 nlogn 으로 입력이 아주 큰 문제이므로 시간초과가 나오게 된다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include #include #include #include us..
2021.03.23 -
[LeetCode] [Medium] ZigZag Conversion
1. Problem "PAYPALISHIRING" => "PAHNAPLSIIGYIR" Given a string and row of zigzag pattern. return a zigzag pattern string for line by line. 2. Sol1 store in order => each row a vector. and sum all vector 0~numRows-1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Solution { public: string convert(string s, int numRows) { if(numRows == 1) return s; vector answer(numRo..
2021.03.21 -
[프로그래머스] [C++] 위장 (map)
1. 문제 [의상 이름, 의상 종류] 로 이루어진 배열이 주어진다. 이때 입을 수 있는 모든 경우의 수를 구하여라 단, 의상은 하나이상 입어야한다, 모든 종류를 다 입지 않아도 된다. 2. unordered_map 이 문제는 의상의 종류를 해쉬맵에 넣으면 간단하게 해결할 수 있다. 의상의 종류를 키로, 값을 한 종류의 의상들의 개수로 설정하면 다음과 같은 점화식을 유도할 수 있다. answer = Π(map[key[i]] + 1) -1 여기서 +1 은 입지않은 경우를 나타내고 -1은 전부 입지 않은 경우를 나타낸다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include #include #include using name..
2021.03.21 -
[프로그래머스] [C++] 순위 (shortest path)
1. 문제 대결 결과 일부가 주어진다. 이 대결 결과는 선수들의 절대적인 실력에 의해 결정된다. (실력이 더 좋다면 무조건 이김) 확실한 순위를 알 수 있는 선수들의 수를 반환하는 프로그램을 작성하여라. 2. 최단거리 이 문제는 모든 쌍의 경로를 구하는 문제이다. 모든 쌍 알고리즘 중에서 유명한 플로이드 알고리즘이나 단일 쌍 알고리즘을 V번 돌리는 알고리즘을 이용하여 문제를 해결 할 수 있다. 3. V * dfs 이 문제는 가중치가 없으므로 그냥 dfs를 V번 반복하였다. dfs에서 해당경로를 갈 수 있음을 보여줄 수 있게 src를 함수 매개변수에 추가적으로 넣었다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29..
2021.03.20 -
[LeetCode] [Medium] Longest Palindromic Substring (dp)
1. Problem Given s string and find longest palindromic substring 2. base: len find palindromic based len: s.size() ~ 1 bottom-up: iteration start => 1 -> s.size() You have to do unnecessary nested calculation. and must use dp algorithm. top-down: i teration start => s.size() -> 1 If find palindromic, immediately return substring. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { public:..
2021.03.20 -
[LeetCode] [Medium] Longest Substring Without Repeating Characters
1. Problems Given a string. Find max length of substring without repeating characters. 2. sol I check all ascii for int array. Visited array has index of first finded index. and if find repeating char, Initialize all elements in front of first fineded index. This time complexity is O(N) because inner for statement operate total maximum n. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ..
2021.03.18