diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/3014. Minimum Number of Pushes to Type Word I.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/3014. Minimum Number of Pushes to Type Word I.py" new file mode 100644 index 00000000..f0124b07 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/3014. Minimum Number of Pushes to Type Word I.py" @@ -0,0 +1,15 @@ +class Solution: + def minimumPushes(self, word: str) -> int: + counter = Counter(word) + freq_num = [[freq, num] for num, freq in counter.items()] + freq_num.sort() + + ans = 0 + counter = 0 + + while freq_num: + freq, num = freq_num.pop() + cost = counter // 8 + 1 + counter+=1 + ans+=cost * freq + return ans diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/3701. Compute Alternating Sum.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/3701. Compute Alternating Sum.py" new file mode 100644 index 00000000..54ae41b1 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/3701. Compute Alternating Sum.py" @@ -0,0 +1,16 @@ +# + +''' +1. 아이디어 : + + +2. 시간복잡도 : + O(n + n) + +3. 자료구조/알고리즘 : + + +''' +class Solution: + def alternatingSum(self, nums: List[int]) -> int: + return sum([nums[i] if i%2==0 else -nums[i] for i in range(len(nums))]) diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/785. Is Graph Bipartite?.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/785. Is Graph Bipartite?.py" new file mode 100644 index 00000000..51d6eb9e --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/785. Is Graph Bipartite?.py" @@ -0,0 +1,45 @@ +# + +''' +1. 아이디어 : +Bipartite는 그래프에서 노드들을 2가지 색을 칠했을때, 인접한 노드와 다른 색일때 성립. +bfs를 통해 노드들의 색갈을 0 또는 1로 칠한다. +그래프가 끊긴 문제는 모든 노드들을 순회하게끔 한다. + +2. 시간복잡도 : + O(V + E) 노드, 간선 + +3. 자료구조/알고리즘 : +BFS + +''' +class Solution: + def isBipartite(self, graph: List[List[int]]) -> bool: + n = len(graph) + colors = [-1] * n + + for i in range(n): + if colors[i] != -1: + continue + + colors[i] = 0 + queue = deque() + queue.append(i) + + while queue: + start = queue.popleft() + start_color = colors[start] + + for dest in graph[start]: + dest_color = colors[dest] + if start_color == dest_color: + return False + + if dest_color == -1: + queue.append(dest) + colors[dest] = (1 + start_color) % 2 + # print(colors) + return True + + +