From 5be83caaede9fd7a48db0144393b3e83b88a3983 Mon Sep 17 00:00:00 2001 From: nkey Date: Thu, 9 Jul 2026 14:15:35 +0900 Subject: [PATCH] =?UTF-8?q?easy/array=2066=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easy/array/66.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 easy/array/66.py diff --git a/easy/array/66.py b/easy/array/66.py new file mode 100644 index 0000000..bfbee94 --- /dev/null +++ b/easy/array/66.py @@ -0,0 +1,25 @@ +# Plus One + +class Solution: + def plusOne(self, digits: list[int]) -> list[int]: + for i in range(len(digits)-1, -1, -1): + digits[i] += 1 + if digits[i] == 10: + digits[i] = 0 + continue + else: + break + + if digits[0] == 0: + digits.insert(0, 1) + return digits + +""" +걸린 시간: 14분 + +복잡도: 최악의 경우 전체 자리를 확인하거나, 맨 앞이 0일 경우 insert의해 전체 숫자가 시프트되므로 시간복잡도는 O(n)이다. +digit 리스트 내용을 확인하거나 insert 한번만 하기 때문에 공간복잡도는 O(1)이다. + +해설: 9인 경우 1이 올림되는 상황을 어떻게 처리할 것인지에 대한 구현만 하면 되는 문제이므로 올림해야 하는 경우만 계속 반복문을 돌도록 설계했다. +예외 상황으로 반복이 끝나고 맨 앞자리가 0이라는 것은 올림을 했다는 뜻이므로 앞에 1을 추가해준다. +""" \ No newline at end of file