2024-TEAM-05 / algorithm-for-kakao

카카였 기좜 문제 κ°€μ¦ˆμ•„πŸ£
0 stars 0 forks source link

[2021 KAKAO BLIND RECRUITMENT] κ΄‘κ³  μ‚½μž… #28

Open uijin-j opened 1 month ago

uijin-j commented 1 month ago

πŸ”— κ΄‘κ³  μ‚½μž…

hye-on commented 1 month ago

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```cpp #include #include #include #include #include using namespace std; vector> t; // i~i+1κ΅¬κ°„κΉŒμ§€ μ€‘μ ‘λœ 개수, μ‹œκ° priority_queue, vector>, greater>> pq; long long timeToInt(string time) { int hs = stoi(time.substr(0,2)) * 60 * 60; int ms = stoi(time.substr(3,2)) * 60; int s = stoi(time.substr(6,2)); return hs + ms + s; } string intToTime(long long num) { string st = ""; int h = num / 3600; num %= 3600; int m = num / 60; int s = num % 60; if (h <= 9) st += '0'; st += to_string(h); st += ":"; if (m <= 9) st += '0'; st += to_string(m); st += ":"; if (s <= 9) st += '0'; st += to_string(s); return st; } void insertAfterInt(string s) { string startTime = s.substr(0,8); string endTime = s.substr(9,8); pq.push({timeToInt(startTime), 0}); //0이면 μ‹œμž‘ μ‹œκ° pq.push({timeToInt(endTime), 1}); //1이면 끝 μ‹œκ° } string solution(string play_time, string adv_time, vector logs) { long long playTimeInt = timeToInt(play_time); long long duration = timeToInt(adv_time); if (logs.empty() || duration >= playTimeInt) { return "00:00:00"; } for (auto lo : logs) { insertAfterInt(lo); } vector totalTime(playTimeInt + 1, 0); while (!pq.empty()) { long long time = pq.top().first; int type = pq.top().second; pq.pop(); if (type == 0) { // μ‹œμž‘ μ‹œκ°„ totalTime[time]++; } else { // μ’…λ£Œ μ‹œκ°„ totalTime[time]--; } } // λˆ„μ  ν•© 계산 for (int i = 1; i <= playTimeInt; i++) { totalTime[i] += totalTime[i-1]; } // ꡬ간 ν•© 계산 // iκΉŒμ§€μ˜ ν•© for (int i = 1; i <= playTimeInt; i++) { totalTime[i] += totalTime[i-1]; } long long maxTime = totalTime[duration - 1]; long long ansInt = 0; for (long long i = duration; i < playTimeInt; i++) { //iκΉŒμ§€μ˜ ν•© - jκΉŒμ§€μ˜ ν•© = j-iꡬ간 ν•© long long currentTime = totalTime[i] - totalTime[i - duration]; if (currentTime > maxTime) { maxTime = currentTime; ansInt = i - duration + 1; } } return intToTime(ansInt); } ```

μ½”λ©˜νŠΈ

uijin-j commented 1 month ago

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```java import java.util.Arrays; class Solution { public String solution(String play_time, String adv_time, String[] logs) { int playSeconds = toSeconds(play_time); int advSeconds = toSeconds(adv_time); int[] watch = new int[playSeconds + 1]; // watch[i]: i ~ i+1초 사이에 μ˜μƒ μ‹œμ²­ 수 for (String log : logs) { String[] splited = log.split("-"); int start = toSeconds(splited[0]); int end = toSeconds(splited[1]); for (int i = start; i < end; i++) { watch[i]++; } } long total = 0; for (int i = 0; i < advSeconds; i++) { total += watch[i]; } int maxIdx = 0; long maxTotal = total; for (int end = advSeconds; end < playSeconds; ++end) { int start = end - advSeconds + 1; total += watch[end]; total -= watch[start - 1]; if (total > maxTotal) { maxTotal = total; maxIdx = start; } } return toHHMMSS(maxIdx); } public int toSeconds(String hhmmss) { String[] info = hhmmss.split(":"); int total = 0; total += Integer.parseInt(info[0]) * 60 * 60; total += Integer.parseInt(info[1]) * 60; total += Integer.parseInt(info[2]); return total; } public String toHHMMSS(int seconds) { int h = seconds / (60 * 60); seconds = Math.max(0, seconds - h * (60 * 60)); int m = seconds / 60; seconds = Math.max(0, seconds - m * (60)); int s = seconds; String hh = (h > 9) ? String.valueOf(h) : "0" + String.valueOf(h); String mm = (m > 9) ? String.valueOf(m) : "0" + String.valueOf(m); String ss = (s > 9) ? String.valueOf(s) : "0" + String.valueOf(s); return hh + ":" + mm + ":" + ss; } } ```

μ½”λ©˜νŠΈ

- μ‹œμ΄ˆλ‚  것 κ°™μ•„μ„œ.. O(n+m)으둜 ν’€λ €κ³  ν–ˆλŠ”λ°, μ•ˆν’€λ €μ„œν’€μ΄λ₯Ό λ³΄λ‹ˆ O(nm)으둜 λ‹€λ“€ ν’€λ”λΌκ΅¬μš”..! 그런데 μ½”λ“œλ₯Ό 쑰금만 λ‹€λ₯΄κ²Œν•΄λ„ λ°”λ‘œ μ‹œκ°„ μ΄ˆκ³Όκ°€ λ‚˜μ„œ μΉ΄μΉ΄μ˜€κ°€ μ˜λ„ν•œ ν’€μ΄λŠ” 이게 μ•„λ‹Œ 것 κ°™μ•„μš”.!