본문 바로가기

Python9

[LeetCode with Python] 51. N-Queens Difficulty : Hard Approach : Use backtrack like in '52. N-Queens II'. Deliever placement information to next level in backtracking. from typing import List visited = [] answers = [] def make_visited_from(row: int, col: int, n: int) -> None: visited[row][col] = 1 left_row = row + 1 left_col = col - 1 right_row = row + 1 right_col = col + 1 down_row = row + 1 while left_row < n and 0 2020. 7. 14.
[LeetCode with Python] 52. N-Queens II Difficulty : Hard Approach : Use backtracking and suppose &#39;row&#39; as each backtracking level. total_count = 0 visited = [] def make_visited_from(row: int, col: int, n: int) -> None: visited[row][col] = 1 left_row = row + 1 left_col = col - 1 right_row = row + 1 right_col = col + 1 down_row = row + 1 while left_row < n and 0 2020. 7. 13.
[LeetCode with Python] 389. Find the Difference Difficulty : Easy Approach : use alphabet map and save each letter&#39;s count. And compare class Solution: def findTheDifference(self, s: str, t: str) -> str: answer = "" s_dict = {} t_dict = {} for i in range(97, 123): s_dict[chr(i)] = 0 t_dict[chr(i)] = 0 for letter in s: s_dict[letter] += 1 for letter in t: t_dict[letter] += 1 for letter in s_dict: if s_dict[letter] != t_dict[letter]: answer.. 2020. 7. 12.
[LeetCode with Python] 690. Employee Importance Difficulty : Easy Approach : use BFS and search from given id until reaching the most subordinates. """ # Definition for Employee. class Employee: def __init__(self, id: int, importance: int, subordinates: List[int]): self.id = id self.importance = importance self.subordinates = subordinates """ from collections import deque adjacent_list = {} value_map = {} total_importance = 0 def bfs(id): glo.. 2020. 7. 12.