diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index c9f090dc2..b04bcfb49 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -103,7 +103,7 @@ "state": { "type": "backlink", "state": { - "file": "src/content/blog/boj-1339-단어-수학.md", + "file": "src/content/blog/boj-2529-부등호.md", "collapseAll": false, "extraContext": false, "sortOrder": "alphabetical", @@ -120,7 +120,7 @@ "state": { "type": "outgoing-link", "state": { - "file": "src/content/blog/boj-1339-단어-수학.md", + "file": "src/content/blog/boj-2529-부등호.md", "linksCollapsed": false, "unlinkedCollapsed": true } @@ -143,7 +143,7 @@ "state": { "type": "outline", "state": { - "file": "src/content/blog/boj-1339-단어-수학.md" + "file": "src/content/blog/boj-2529-부등호.md" } } } @@ -166,10 +166,10 @@ "table-editor-obsidian:Advanced Tables Toolbar": false } }, - "active": "71e0df042bc7a561", + "active": "bb9e313e9ec35bc3", "lastOpenFiles": [ - "src/content/blog/boj-2529-부등호.md", "src/content/blog/boj-1339-단어-수학.md", + "src/content/blog/boj-2529-부등호.md", "src/content/blog/boj-2660-회장뽑기.md", "src/content/blog/boj-2011-암호코드.md", "src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md", diff --git "a/src/content/blog/boj-2529-\353\266\200\353\223\261\355\230\270.md" "b/src/content/blog/boj-2529-\353\266\200\353\223\261\355\230\270.md" index f4849f329..2f2f07d84 100644 --- "a/src/content/blog/boj-2529-\353\266\200\353\223\261\355\230\270.md" +++ "b/src/content/blog/boj-2529-\353\266\200\353\223\261\355\230\270.md" @@ -30,7 +30,7 @@ ogImage: "" ![](https://res.cloudinary.com/gyunseo-blog/image/upload/f_auto/v1720540092/image_fux9f7.png) 그런데, 아직 원인 파악은 못했습니다. -계속 시간 초과가 뜨네요... +계속 시간 초과가 뜨네요... (`eval`함수가 병목 구간이었습니다 ㅎㅎ) 그래서 아쉬운 대로 DFS + 백트래킹으로 구현했습니다! ## 구현 @@ -76,3 +76,51 @@ if __name__ == "__main__": print(maxStr) print(minStr) ``` + +```python +import sys +from itertools import permutations + +input = sys.stdin.readline + +def check(a, b, op): + if op == "<": + return a < b + else: + return a > b +if __name__ == "__main__": + K = int(input().strip()) + operators = input().strip().split() + + maxSeqSum = 0 + minSeqSum = 9999999999 + maxSeq, minSeq = None, None + seqCandsGen = permutations(range(10), K + 1) + + for candSeq in seqCandsGen: + isValid = True + for i, operator in enumerate(operators): + if not check(candSeq[i], candSeq[i + 1], operator): + isValid = False + break + + if isValid: + seqSum = 0 + factor = 1 + for idx in range(K, -1, -1): + seqSum += candSeq[idx] * factor + factor *= 10 + # print(f"seqSum: {seqSum}") + if seqSum > maxSeqSum: + maxSeq = candSeq + maxSeqSum = seqSum + if seqSum < minSeqSum: + minSeq = candSeq + minSeqSum = seqSum + # print(f"minSeq: {minSeq}") + + # print(maxSeq, minSeq) + print("".join(map(str, maxSeq))) + print("".join(map(str, minSeq))) + +```