본문 바로가기

algorithm/leetcode15

[leetcode] Number of Islands 정리 source 는 Github 에 있습니다. 문제 (Number of Islands) 문제 풀이 연결된 섬들은 1개로 count 합니다. for 문을 2번 돌려서 순회를 하면 연결된 섬에 대해서 찾을 수 없습니다. 전체 grid 를 순회하면서 방문한 곳은 순회하지 않도록 구현해서 처리했습니다. 순회할 때, DFS 를 써서 연결된 것들을 순회하게 했습니다. 순회할 때는 상하좌우로 순회하게 했습니다. source public int numIslands(char[][] grid) { int m = grid.length; int n = grid[0].length; visited = new boolean[m][n]; int count = 0; for (int i = 0 ; i < m ; i++) { for (int.. 2022. 1. 28.
leetcode Employees Earning More Than Their Managers 풀이 self 조인 leetcode 문제 https://leetcode.com/problems/employees-earning-more-than-their-managers/ 풀이 주의할 점은 a.managerId = b.id 임. a.managerId 가 3,4 = b 테이블과 비교했을 때, 1,2번이 조인 결과로 나옴. select a.name as Employee from Employee a inner join Employee b on a.managerId = b.Id where a.salary > b.salary ; 2020. 9. 30.
leetcode Reformat Department Table 풀이 CASE WHEN PIVOT LEETCODE 문제 https://leetcode.com/problems/reformat-department-table/ 요약하면 department 별 1~12월 수입을 출력. 풀이 select id ,sum(case when month = &#39;Jan&#39; then revenue end) as Jan_Revenue ,sum(case when month = &#39;Feb&#39; then revenue end) as Feb_Revenue ,sum(case when month = &#39;Mar&#39; then revenue end) as Mar_Revenue ,sum(case when month = &#39;Apr&#39; then revenue end) as .. 2020. 9. 29.
[LeetCode] ReverseInteger 문제 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer.. 2020. 7. 11.