Leetcode 23th August

Leetcode 23th August Two Sum IV - Input is a BST 이 문제는 Tree에서 두개를 선택해서 합이 K가 되는 경우의수가 존재하는지 확인하는 문제입니다. This problem is checking condition that when you choose the two element in tree the sum of the elements value are same as K 이경우 저는 일단 Tree을 순회하면서 값을 List에 추가한 후에 이중 for loop를 통해 두개의 합이 k가 되는 값이 있는지 체크 하는 방법을 사용했습니다. for this case, first of all i gathering a every elements value in tree use dfs algorithm, then try to found a has a case that the sum of two elements is same as k Java /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean findTarget ( TreeNode root , int k ) { List < Integer > list ...

Leetcode 21th August

Leetcode 21th August Sudoku Solver 이 문제는 9x9 배열의 sudoku 문제 주워지고 그 빈값에 알맞은 답을 적용하는 방식입니다. This is the problem that return matrix that set correct value 이 경우에도 저는 grid 한 방식으로 9X9 배열을 돌면서 값이 없을 경우 1~9까지의 숫자를 돌면서 해당 답이 valid 할 경우 다음 row, col에 값을 대입하는 형태로 문제를 풀었습니다. i use previous way for solving this problem, so looping matrix then if the value is empty, i found a correct value from 1 ~9 Python class Solution : N = 9 def is_valid ( self , board , row , col , num ) : """ This function is for checking this board is valid or not :return: boolean """ # TODO: checking row is valid for x in range ( 9 ) : if board [ row ] [ x ] == num : return False # TODO: chcking colum in valid for x in range ( 9 ) : if board [ x ] [ col ] == num : return False # TODO: checking box is valid ...

Leetcode 20th August

Leetcode 20th August Valid Sudoku 이 문제는 제공된 9x9 matrix sudoku 문제가 valid 한지 안한지 리턴하는 문제 입니다. This is the problem to check whether given matrix is valid or not 이 경우는 간단히 O(N^2) 을 써서 사용했습니다. valid 한 조건의 경우 row, col, box에 같은 변수값이 있는지 없는지로 추가하였습니다. for solving problem i use that way that is looping given matrix and if the value is exists checking that value is valid or not. if the result is invalid then return False. and if don’t found a any invalid value in loop matrix then return True. Python class Solution : def is_valid ( self , board , row , col , num ) : """ This is the function to checking given num is valid or not. return: boolean """ # TODO: checking row is valid for x in range ( 9 ) : if x != col and board [ row ] [ x ] == num : return False # TODO: chcking colum in valid for x in range ( 9 ) : if x ...

Leetcode 17th August

Leetcode 17th August Leetcode 17th August Count Good Nodes in Binary Tree 이 문제는 Tree 를 순회하면서 Node의 상태가 good인 node의 갯수를 리턴하는 문제입니다. This is the Question return count of good node in tree 상태가 good 인 node란? tree의 parent의 값 들이 모두 자기 자신보다 작거나 같은 경우 What is the meaning of good node? the node’s value are greater than or equal to all of the values of parent node in tree 그래서 저는 dfs를 통해 Tree를 순회하면서 node의 parent의 값을 비교하는 방식으로 구현하였습니다. So i use dfs algorithm for access node by tree, and then calculate this node is good or not. if the node is good node then i adding 1 in answer Python # 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 class Solution : def goodNodes ( self , root : TreeNode ) - > int : queue = deque ( ) root . parent = None queue . append ( root ) ...

Leetcode 15th August

Leetcode 15th August Leetcode 15th August Set Matrix Zeroes 이 문제는 matrix 가 주워졌을때 cell 중에 0 이포함되어있으면 그 해당 rows, cols의 값을 0으로 변경하는 문제 입니다. This question is Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0’s, and return the matrix. 그래서 저의 경우 rows, cols sets를 만든 다음에 matrix[i][j] 값이 0 일 경우 rows 에 i, cols에 j를 추가하고, 그담에 loop를 통해서 i가 rows 에 포함되어있거나 cols 가 j에 포함되어있을 경우 값을 0으로 변경하는 방식으로 해결했습니다. For solving this problem i create two sets named rows , cols and add i in rows and j in cols if the cell value is 0, after set up rows, cols values, set matrix value 0 if i value in rows or j value in cols Python Time complexity: O(M * N) class Solution : def setZeroes ( self , matrix : List [ List [ int ] ] ) - > None : """ Do not return anything, modify matrix in-place instead. """ n = len ( matrix ) m = len ( matrix [ 0 ] ) res = ...

Leetcode 14th August

Leetcode 14th August Group Anagrams 이 문제는 anagrams 가 같은 문자들 끼리 그룹화 해서 보여주는 문제 This question is return string list group by same anagrams 그래서 단순하게 저의 경우는 정렬 된 문자를 키로 가지는 map 만들고, 후에 값만 리턴 하는방식으로 문제를 해결하였습니다. For solving this problem, i used make a Map, that is key, value store, then the key is sorted string, and value is string list, after generate map i return values. Python class Solution : def groupAnagrams ( self , strs : List [ str ] ) - > List [ List [ str ] ] : res = defaultdict ( list ) for s in strs : ss = "" . join ( sorted ( s ) ) res [ ss ] . append ( s ) return [ v for i , v in res . items ( ) ] 요새 자바도 연습하고 있어서 똑같은 방식으로 자바 코드로도 한번 짜봤습니다. i also try to write a code in java, because for now i learning Java Java class Solution { public List < List < String >> groupAnagrams ( String [ ] strs ) { Map < String , List < String >> map = n...

Leetcode Weekly Context #250 회고

이미지
 Leetcode Weekly Context #250 회고 Leetcode Weekly Context #250 관련된 코드 리뷰 및 회고 내용입니다. 오늘은 요새 자꾸 회사에서도 개발보다는  기술 검토 및 아키텍쳐 설계를 담당하고 있고,  내 자신도 코로나 때문인지 밖에 잘 못나가서 그런지 답답한 감이 생겨서 오랜만에 leetcode Weekly Context를 간단하게 해볼까 합니다.  아마 다 풀지는 못할텐데,  일단 풀수 있는 만큼 만 풀고 나서 그것에 대한 회고 및 코드를 적을 까 합니다.   그리고 이걸 하는 이유는 점점 그날이 다가 오고 있기 때문이기도 하구요. leetcode Weekly Context 이건 leetcode 에서 매 주마다 4개의 알고리즘 문제를 출제하고 주어진 시간안에 누가 빠른 시간안에 푸는지를 경쟁하는 프로그램입니다. 원래 대로 라면 9시30분에 아침 운동 끝나고 나서 씻고 10시부터 12시까지 두시간 동안 진행해야 하는게 맞는데, 늦게 일어나는 바람에 남은 시간이 30분 밖에 없었네요. 30분 동안 2문제는  pass하고 나머지 한 문제는 풀었는데,  optimized 가 안되어있어서 Time limit이 걸렸네요.  이제 문제에 대해 설명 드리고, 어떻게 풀었는지 코드 및 설명 적도록 하겠습니다. 1935. Maximum Number of Words You Can Type 이건 text, brokenLetters 가 주워지면 text 의 letter 별로 brokenLetters가 포함되어있지 않은 letter에 대한 갯수를 리턴하는 문제입니다. 저는 간단하게 brokenLetters, letter를 set으로 만들고 둘 사이에 intersection 이 없으면 count ++ 를 하는 형태로 코드 구현하였습니다. 코드는 python으로 구현하였습니다. c++이나 자바가 더 빠르다고 하는데, 해당 언어에 대해서는 익숙하거나 그렇지 않아서...