๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ž…๊ตญ์‹ฌ์‚ฌ (์ด๋ถ„ํƒ์ƒ‰)

๐ŸŽฎinspirer9 2023. 3. 1. 13:55
728x90
๋ฐ˜์‘ํ˜•
  • ์˜ˆ์ „์— C++๋กœ ํ’€์—ˆ๋Š”๋ฐ, ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์—ˆ๋‹ค.
  • ๋‹ค์‹œ๋ด๋„ C++์€ ์–ด์šฐ... ์ด์   ๋ชป ์“ฐ๊ฒ ๋‹ค.
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

long long solution(int n, vector<int> times) {
    long long answer = 0;
    
    sort(times.begin(), times.end());
    
    vector<int> officer = times;
    
    int passed = 0;
    int i = 0;
    
    while(passed < n)
    {
        officer[i]--;

        if(officer[i] <= 0){
            passed++;
            officer[i] = times[i];
        }
        
        i++;
        if(i >= officer.size()){
            answer++;
            i = 0;
        }
    }
    
    return answer+1;
}
  • ์ผ๋‹จ ์ด๊ฑธ ํŒŒ์ด์ฌ์œผ๋กœ ๋จผ์ € ์˜ฎ๊ธด๋‹ค.
  • ๋ฌผ๋ก  ๊ฒฐ๊ณผ๋Š” ๋™์ผ. ์‹œ๊ฐ„์ดˆ๊ณผ.
def solution(n, times):
    answer = 0
    times.sort()
    officer = times.copy()
    passed = 0
    i = 0
    
    while passed < n:
        officer[i] -= 1

        if officer[i] <= 0:
            passed += 1
            officer[i] = times[i]
        
        i += 1
        if i >= len(officer):
            i = 0
            answer += 1
            
    return answer + 1
  • ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ๋Š” ์•ˆ๋˜๋Š” ใ…‹ใ…‹
def solution(n, times):
    answer = 0
    times.sort()
    officer = times.copy()
    len_officer = len(officer)
    passed = 0
    
    while passed < n:
        min_time = min(officer)
        for i in range(len_officer):
            officer[i] -= min_time
            if officer[i] == 0:
                passed += 1
                officer[i] = times[i]
        answer += min_time
            
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (6.88ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (1065.73ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ํ‚...
def solution(n, times):
    answer = 0
    times.sort() # ์ฒ˜๋ฆฌ ์‹œ๊ฐ„
    
    officers = [] # ์ฒ˜๋ฆฌํ•œ ์ธ์› ์ˆ˜
    officers.append(int(n/len(times)))
    
    passed_time = times[0] * officers[0]
    
    for i in range(1, len(times)):
        officers.append(int(passed_time / times[i]))
    
    for i in range(0, len(times)):
        print("์†๋„:", times[i],"์ฒ˜๋ฆฌ ์ธ์›:", officers[i],"์†Œ์š” ์‹œ๊ฐ„:", times[i] * officers[i])
    print("๋‚จ์€ ์ธ์›:", n-sum(officers))
    
    return answer
    
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	6, [7, 10]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	28
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	์‹คํ–‰ํ•œ ๊ฒฐ๊ด๊ฐ’ 0์ด ๊ธฐ๋Œ“๊ฐ’ 28๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
์†๋„: 7 ์ฒ˜๋ฆฌ ์ธ์›: 3 ์†Œ์š” ์‹œ๊ฐ„: 21
์†๋„: 10 ์ฒ˜๋ฆฌ ์ธ์›: 2 ์†Œ์š” ์‹œ๊ฐ„: 20
๋‚จ์€ ์ธ์›: 1
  • ํž... ์™œ ์•ˆ๋˜๋Š”๊ฑฐ์•ผ;
import numpy as np

def solution(n, times):
    answer = 0
    times.sort() # ์ฒ˜๋ฆฌ ์‹œ๊ฐ„
    
    officers = [] # ์ฒ˜๋ฆฌํ•œ ์ธ์› ์ˆ˜
    officers.append(int(n/len(times)))
    officers_time = []
    
    passed_time = times[0] * officers[0]
    
    for i in range(1, len(times)):
        officers.append(int(passed_time / times[i]))
    
    fast_officer = min(officers)
    for i in range(len(times)):
        #print("์†๋„:", times[i],"์ฒ˜๋ฆฌ ์ธ์›:", officers[i],"์†Œ์š” ์‹œ๊ฐ„:", times[i] * officers[i])
        officers_time.append(times[i] * officers[i])
    #print("๋‚จ์€ ์ธ์›:", n-sum(officers))
    
    answer = min(officers_time)
    #print(answer)
    estimated_officers_time = officers_time.copy()
    n = n - sum(officers)
    
    for i in range(len(officers_time)):
        estimated_officers_time[i] += times[i]

    sorted_officers_time  = np.sort(estimated_officers_time)
    sorted_officers_index = np.argsort(estimated_officers_time)

    for i in range(n):
        #officers[sorted_officers_index[i]] += 1
        officers_time[sorted_officers_index[i]] += times[sorted_officers_index[i]]
    
    return max(officers_time)

ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	6, [7, 10]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	28
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	7, [7, 10]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	30
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (172.63ms, 42.7MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
  • ๋ผ์šด๋“œ ๋กœ๋นˆ ๋ฐฉ์‹ ๊ฐ™์€๊ฑด ๊ทธ๋ƒฅ ์•ˆ๋œ๋‹ค.
    • ์‹œ๊ฐ„ ์ดˆ๊ณผ...
import numpy as np

def solution(n, times):
    num_people = len(times)
    current_time = [0] * num_people
    while n > 0:
        for i in range(num_people):
            if n <= 0:
                break
            current_time[i] += times[i]
            n -= 1
    return max(current_time)
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (0.01ms, 27.9MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (0.13ms, 28.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (0.80ms, 28.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (15.61ms, 35.6MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (54.60ms, 39.8MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ํ•œ ๋ฒˆ ๋” ํŠธ๋ผ์ด!
import numpy as np

def solution(n, times):
    num_people = len(times)
    average_time = 0
    officers = [[0,0] for i in range(num_people)]
    for i in range(num_people):
        average_time += 1/times[i]
        
    target = int(n / average_time + 0.9) + 1
    
    for i in range(num_people):
        officers[i][1] = target // times[i]
        officers[i][0] = times[i] * (target // times[i])
        n -= officers[i][1]
    
    remind = [[0,0] for i in range(num_people)]
    if n > 0:
        for i in range(num_people):
            next_time = officers[i][0] + times[i]
            remind[i][0] = next_time
            remind[i][1] = i #index

        remind.sort()
    
    if remind[n-1][0] != 0:
        return remind[n-1][0]
    else:
        return max(officers[i][0] for i in range(num_people))
  • ์ด๊ฒŒ ์•ˆ๋˜๋„ค?
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	์‹คํŒจ (0.02ms, 28.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (0.24ms, 28.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (1.26ms, 28.5MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (239.64ms, 55.6MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (217.75ms, 60.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (273.62ms, 61.8MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (335.51ms, 63.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (270.54ms, 63.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ)
  • ๊ฒฐ๊ตญ ์‹œํ‚ค๋Š” ๋Œ€๋กœ ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ใ… .ใ… 
    • n๋ช…์ด ์ž…๊ตญ์‹ฌ์‚ฌ๋ฅผ ๋ฐ›๋Š” ์ตœ์†Œ ์‹œ๊ฐ„ = ์ œ์ผ ๋น ๋ฅธ ์‹ฌ์‚ฌ๋Œ€
    • n๋ช…์ด ์ž…๊ตญ์‹ฌ์‚ฌ๋ฅผ ๋ฐ›๋Š” ์ตœ๋Œ€ ์‹œ๊ฐ„ = ์ œ์ผ ๋Š๋ฆฐ ์‹ฌ์‚ฌ๋Œ€ * n๋ช…
def solution(n, times):
    times.sort()
    min_time = times[0]
    max_time = times[-1]* n
        
    def binSearch(low, high):
        mid = (high-low)//2
        if mid == 0:
            return high
        mid += low
        
        passed = 0
        for t in times:
            passed += mid // t
            
        if passed >= n:
            return binSearch(low, mid)
        else:
            return binSearch(mid, high)
        
    return binSearch(min_time, max_time)
  • ๊ดœํ•œ ๊ณ ์ง‘์ด์—ˆ๋‹ค.
    • ์‹œํ‚ค๋Š”๋Œ€๋กœ ํ•˜๋Š”๊ฒŒ ์ œ์ผ ์‰ฝ๋‹ค.
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (4.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (381.12ms, 14MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (488.09ms, 14.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (338.49ms, 14.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (659.35ms, 14MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (652.15ms, 14MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด
    • ์‹ ๊ธฐํ•œ ๋ฐฉ์‹...
def count(period, times):
    cnt = 0
    for time in times:
        cnt += period // time
    return cnt

def solution(n, times):
    digit = 50
    t = 0
    for i in range(digit, -1, -1):
        temp  = t + 2**i
        cnt = count(temp, times)
        if cnt >= n:
            if count(temp-1, times) < n:
                return temp
        else:
            t = temp
            
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.66ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (7.16ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (659.91ms, 14.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (853.45ms, 14.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (634.52ms, 14.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1062.84ms, 14.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (995.41ms, 14.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.2MB)
  • ๋น„์Šท๋น„์Šทํ•˜๋‹ค.
def solution(n, times):
    answer = 0
    begin = 0
    end = 1 * 10 ** 18
    times.sort()
    while (begin <= end):
        to_do_job = [n][0]
        mid = (begin + end) // 2
        for task_time in times:
            to_do_job -= mid // task_time
        if to_do_job <= 0:
            end = mid -1
        elif to_do_job > 0:
            begin = mid +1
    return begin
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.46ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (9.54ms, 9.99MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (992.07ms, 14.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (764.32ms, 14.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (1183.25ms, 14.5MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1187.45ms, 14MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (624.74ms, 14.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
  • ๋‚˜์ค‘์— ์‹œ๊ฐ„ ๋˜๋ฉด ํ‰๊ท  ์†๋„๋กœ ์ฐพ์•„๋ณด๋Š”๊ฑฐ ๋‹ค์‹œ ์งœ๋ด์•ผ์ง€
728x90
๋ฐ˜์‘ํ˜•