issues
search
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
π λκΈ ν νλ¦Ώ
Language : C++
μ±λ₯
μ½λ νμ΄
```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
π λκΈ ν νλ¦Ώ
Language : Java
μ±λ₯
μ½λ νμ΄
```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)μΌλ‘ λ€λ€ νλλΌκ΅¬μ..! κ·Έλ°λ° μ½λλ₯Ό μ‘°κΈλ§ λ€λ₯΄κ²ν΄λ λ°λ‘ μκ° μ΄κ³Όκ° λμ μΉ΄μΉ΄μ€κ° μλν νμ΄λ μ΄κ² μλ κ² κ°μμ.!
π κ΄κ³ μ½μ