diff --git a/hojoo/week6/eng_chain.py b/hojoo/week6/eng_chain.py deleted file mode 100644 index ddc1ed3..0000000 --- a/hojoo/week6/eng_chain.py +++ /dev/null @@ -1,14 +0,0 @@ -# 주어진 단어를 순회하며 끝말잇기 규칙에 어긋나면 게임 종료 -# 시간 복잡도: O(n) - -def solution(n, words): - for i in range(1, len(words)): - if words[i-1][-1] != words[i][0] or words[i] in words[:i]: - return [(i % n) + 1, (i // n) + 1] - - return [0, 0] - -if __name__ == "__main__": - print(solution(3, ["tank", "kick", "know", "wheel", "land", "dream", "mother", "robot", "tank"])) - print(solution(5, ["hello", "observe", "effect", "take", "either", "recognize", "encourage", "ensure", "establish", "hang", "gather", "refer", "reference", "estimate", "executive"])) - print(solution(2, ["hello", "one", "even", "never", "now", "world", "draw"])) \ No newline at end of file diff --git a/hojoo/week6/sheep_wolf-dp.py b/hojoo/week6/sheep_wolf-dp.py deleted file mode 100644 index fce03ac..0000000 --- a/hojoo/week6/sheep_wolf-dp.py +++ /dev/null @@ -1,61 +0,0 @@ -# 비트마스크 + dp -# 시간 복잡도: O(N * 2^N) -def solution(info, edges): - n = len(info) - parent = [-1] * n - for p, c in edges: - parent[c] = p - - total_masks = 1 << n - sheep_count = [0] * total_masks - wolf_count = [0] * total_masks - - # mask를 구성하는 가장 낮은 비트부터 채우며 양/늑대 수 미리 계산 - for mask in range(1, total_masks): - lsb = mask & -mask - node = (lsb.bit_length() - 1) - prev_mask = mask ^ lsb - sheep_count[mask] = sheep_count[prev_mask] + (info[node] == 0) - wolf_count[mask] = wolf_count[prev_mask] + (info[node] == 1) - - candidate = [False] * total_masks - start_mask = 1 # 루트(0번 노드) 방문 상태 - if wolf_count[start_mask] < sheep_count[start_mask]: - candidate[start_mask] = True - - ans = 0 - for mask in range(total_masks): - if not candidate[mask]: - continue - - sheep = sheep_count[mask] - wolf = wolf_count[mask] - - if wolf >= sheep: - continue - - ans = max(ans, sheep) - - # 아직 방문하지 않은 노드 중 부모가 방문된 노드만 확장 - for node in range(n): - if mask & (1 << node): - continue - - parent_node = parent[node] - if parent_node != -1 and not (mask & (1 << parent_node)): - continue - - next_mask = mask | (1 << node) - next_sheep = sheep_count[next_mask] - next_wolf = wolf_count[next_mask] - - if next_wolf >= next_sheep: - continue - - candidate[next_mask] = True - - return ans - - -if __name__ == "__main__": - print(solution([0,0,1,1,1,0,1,0,1,0,1,1], [[0,1],[1,2],[1,4],[0,8],[8,7],[9,10],[9,11],[4,3],[6,5],[4,6],[8,9]])) \ No newline at end of file diff --git a/soyeon/week8/hanoi.cpp b/soyeon/week8/hanoi.cpp new file mode 100644 index 0000000..c15a109 --- /dev/null +++ b/soyeon/week8/hanoi.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +void hanoi(int n, int from, int via, int to, vector>& answer) { + if (n == 0) return; + + hanoi(n - 1, from, to, via, answer); + answer.push_back({from, to}); + hanoi(n - 1, via, from, to, answer); +} + +vector> solution(int n) { + vector> answer; + hanoi(n, 1, 2, 3, answer); + return answer; +} diff --git a/soyeon/week8/snail.cpp b/soyeon/week8/snail.cpp new file mode 100644 index 0000000..311a368 --- /dev/null +++ b/soyeon/week8/snail.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +vector solution(int n) { + vector> a(n, vector(n, 0)); + vector answer; + + int x = -1, y = 0; + int num = 1; + + for (int i = 0; i < n; i++) { + for (int j = i; j < n; j++) { + if (i % 3 == 0) { // 아래 + x++; + } else if (i % 3 == 1) { // 오른쪽 + y++; + } else { // 왼쪽 위 + x--; + y--; + } + a[x][y] = num++; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j <= i; j++) { + answer.push_back(a[i][j]); + } + } + + return answer; +}