diff --git a/longest-substring-without-repeating-characters/dahyeong-yun.java b/longest-substring-without-repeating-characters/dahyeong-yun.java new file mode 100644 index 0000000000..5eee6c3635 --- /dev/null +++ b/longest-substring-without-repeating-characters/dahyeong-yun.java @@ -0,0 +1,28 @@ +/** + * TC : O(n) + * - 문자열 길이 n 만큼 순회하므로 O(n) + * SC : O(min(m, n)) + * - map 의 최대 크기는 문자열 길이 n과 알파벳 종류 수 m == 26 중 더 작은 수이므로 O(min(m, n)) + */ +class Solution { + public int lengthOfLongestSubstring(String s) { + // "abcabcbb" + // v - cursor = maxLen; + int maxLen = 0; + Map map = new HashMap<>(); + + int anchor = 0; + for(int cursor = 0; cursor < s.length(); cursor++) { + char c = s.charAt(cursor); + + if(map.containsKey(c)) { + anchor = Math.max(anchor, map.get(c) + 1); + } + + map.put(c, cursor); + + maxLen = Math.max(maxLen, cursor - anchor + 1); + } + return maxLen; + } +} diff --git a/number-of-islands/dahyeong-yun.java b/number-of-islands/dahyeong-yun.java new file mode 100644 index 0000000000..e1cee45a3b --- /dev/null +++ b/number-of-islands/dahyeong-yun.java @@ -0,0 +1,43 @@ +/** + * TC : O(m * n) + * - 그리드 원소 갯수 m * n 만큼 순회하므로 + * SC : O(m * n) + * - 그리드 원소 갯수 m * n 만큼 재귀 호출이 발생할 수 있으므로 + */ +class Solution { + char[][] grid; + int rLen = 0; + int cLen = 0; + + public int numIslands(char[][] grid) { + this.grid = grid; + this.rLen = grid.length; + this.cLen = grid[0].length; + int count = 0; + + for(int row=0; row= this.rLen || col >= this.cLen || this.grid[row][col] != '1') { + return; + } + + this.grid[row][col] = '2'; + + // 좌우상하 다 처리 + cover(row + 1, col); + cover(row - 1, col); + cover(row, col + 1); + cover(row, col - 1); + } +} diff --git a/reverse-linked-list/dahyeong-yun.java b/reverse-linked-list/dahyeong-yun.java new file mode 100644 index 0000000000..753490b0db --- /dev/null +++ b/reverse-linked-list/dahyeong-yun.java @@ -0,0 +1,19 @@ +/** + * TC : O(n) + * - 전체 리스트를 한번 순회 하므로 O(n) + * SC : O(1) + * - 임시 변수 하나만 사용하므로 O(1) + */ +class Solution { + public ListNode reverseList(ListNode head) { + ListNode prev = null; + + while(head != null) { + ListNode next = head.next; // 다음 순서의 노드 + head.next = prev; // 현재 노드의 다음을 이전 노드로 + prev = head; // 다음 차례의 이전 노드는 현재 + head = next; // 다음 차례는 임시 저장했던 노느 + } + return prev; + } +} diff --git a/unique-paths/dahyeong-yun.java b/unique-paths/dahyeong-yun.java new file mode 100644 index 0000000000..26230df982 --- /dev/null +++ b/unique-paths/dahyeong-yun.java @@ -0,0 +1,19 @@ +/** + * TC : O(min(m, n)) + * - m, n 중에 더 작은 수의 -1 을 한 만큼 순회하므로 O(min(m, n)) + * SC : O(1) + * - 유의미한 공간 생성이 없음. + */ +class Solution { + public int uniquePaths(int m, int n) { + int selectCount = Math.min(m - 1, n - 1); // 선택할 개수 (r) + int totalCount = m + n - 2; // 선택 가능한 총 개수 (n) + + long combination = 1; + for (int i = 1; i <= selectCount; i++) { + combination = combination * (totalCount - selectCount + i) / i; + } + + return (int) combination; + } +}