Algorithm/LeetCode 이모저모
-
52. N-Queens II javascriptAlgorithm/LeetCode 이모저모 2023. 5. 19. 11:45
https://leetcode.com/problems/n-queens-ii/description/ /** * @param {number} n * @return {number} */ var cnt = 0; var totalNQueens = function(n) { cnt = 0; let arr = []; nqueen(arr, n); return cnt }; const nqueen = (temp, n) => { if (temp.length === n) { cnt += 1 return } for(let i = 0; i < n; i++) { if (isPossible(temp, i)) { temp.push(i) nqueen(temp, n) temp.pop(i) } } } const isPossible = (ar..
-
51. N-Queens javascriptAlgorithm/LeetCode 이모저모 2023. 5. 19. 11:39
https://leetcode.com/problems/n-queens/description/ /** * @param {number} n * @return {string[][]} */ var solveNQueens = function(n) { let ans = []; let temp = []; nQueen(n, ans, temp); return ans; }; const nQueen = (n, ans, temp) => { if (temp.length === n) { makeAns(ans, temp); return } for(let start = 0; start < n; start++) { if (isPossible(start, temp)) { temp.push(start); nQueen(n, ans, tem..
-
1143. Longest Common Subsequence javascriptAlgorithm/LeetCode 이모저모 2023. 5. 16. 23:11
https://leetcode.com/problems/longest-common-subsequence/ Longest Common Subsequence - LeetCode Can you solve this real interview question? Longest Common Subsequence - Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string genera leetcode.com /** * @param {string} text1 * @p..
-
300. Longest Increasing Subsequence javascriptAlgorithm/LeetCode 이모저모 2023. 5. 16. 22:20
https://leetcode.com/problems/longest-increasing-subsequence/description/ Longest Increasing Subsequence - LeetCode Can you solve this real interview question? Longest Increasing Subsequence - Given an integer array nums, return the length of the longest strictly increasing subsequence. Example 1: Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest leetcode.com # 틀린 풀이 : O(n),..
-
13. Roman to Integer javascriptAlgorithm/LeetCode 이모저모 2023. 5. 12. 15:57
https://leetcode.com/problems/roman-to-integer/description/ /** * @param {string} s * @return {number} */ var romanToInt = function(s) { let map = new Map(); map.set('M', 1000); map.set('D', 500); map.set('C', 100); map.set('L', 50); map.set('X', 10); map.set('V', 5); map.set('I', 1); let sum = 0; for (let i = 0; i < s.length; i++) { if (i < s.length - 1 && map.get(s[i]) < map.get(s[i+1])) { sum..
-
289. Game of Life pythonAlgorithm/LeetCode 이모저모 2023. 4. 26. 00:56
https://leetcode.com/problems/game-of-life/description/ None: """ Do not return anything, modify board in-place instead. """ import copy dxs = [-1, -1, -1, 0, 0, 1, 1, 1] dys = [-1, 0, 1, -1, 1, -1, 0, 1] changedBoard = copy.deepcopy(board) def go_outside(i, j): if i = len(board) or j = len(board[0]): return True return False def check(i, j): sum = 0 for dx, dy in zip(dxs, dy..
-
223. Rectangle Area pythonAlgorithm/LeetCode 이모저모 2023. 4. 9. 23:18
https://leetcode.com/problems/rectangle-area/description/ class Solution: def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int: coxl=max(ax1,bx1) coxr=min(ax2,bx2) coyl=max(ay1,by1) coyr=min(ay2,by2) dx=coxr-coxl dy=coyr-coyl comm=0 if dx>0 and dy>0: comm=dx*dy a=abs(ax2-ax1)*abs(ay2-ay1) b=abs(bx2-bx1)*abs(by2-by1) area=a+b-comm return area
-
226. Invert Binary Tree pythonAlgorithm/LeetCode 이모저모 2023. 4. 9. 22:34
https://leetcode.com/problems/invert-binary-tree/description/ # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right from collections import deque class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if root == None: return if root.right or root.left: temp..