-
-
Notifications
You must be signed in to change notification settings - Fork 339
[hyeri0903] WEEK7 Solutions #2539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a431200
3e704ff
9cd063a
f48c4b5
4c7f657
e34fe26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| class Solution { | ||
| public int lengthOfLongestSubstring(String s) { | ||
| /** | ||
| 1.prob: 중복없는 가장 긴 substring length return | ||
| 2.constraints: | ||
| - alphabet, digit, space 로 구성 | ||
| - s.length min = 0, max = 50,000 | ||
| 3.solution | ||
| - slding window, time: O(n), space: O(n) or O(1) | ||
| */ | ||
|
|
||
| int maxLen = 0; | ||
| int left = 0; | ||
| Set<Character> visited = new HashSet<>(); | ||
|
|
||
| for(int i = 0; i < s.length(); i++){ | ||
| char ch = s.charAt(i); | ||
| while(visited.contains(ch)) { | ||
| visited.remove(s.charAt(left)); | ||
| left += 1; | ||
| } | ||
| visited.add(ch); | ||
| maxLen = Math.max(maxLen, i - left + 1); | ||
| } | ||
|
|
||
| return maxLen; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| class Solution { | ||
|
|
||
| public int numIslands(char[][] grid) { | ||
| /** | ||
| 1.prob:섬 개수 구하기 | ||
| 2.constraints: | ||
| - 원소값 0 or 1 | ||
| - m,n 길이는 최소 = 1, 최대 = 300 | ||
| 3.solution: dfs | ||
| */ | ||
|
|
||
| //m: 세로, n: 가로 길이 | ||
| int m = grid.length; | ||
| int n = grid[0].length; | ||
| int count = 0; | ||
|
|
||
| for(int i = 0; i < m; i++) { | ||
| for(int j = 0; j < n; j++) { | ||
| if(grid[i][j] == '1' && grid[i][j] != '#') { | ||
| dfs(i, j, grid, m, n); | ||
| count += 1; | ||
| } | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| void dfs(int i, int j, char[][] grid, int m, int n) { | ||
| if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0') { | ||
| return; | ||
| } | ||
| //이미 visited | ||
| if(grid[i][j] == '#') { | ||
| return; | ||
| } | ||
| //방문 체크 | ||
| grid[i][j] = '#'; | ||
|
|
||
| dfs(i+1, j, grid, m, n); | ||
| dfs(i-1, j, grid, m, n); | ||
| dfs(i, j+1, grid, m, n); | ||
| dfs(i, j-1, grid, m, n); | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * public class ListNode { | ||
| * int val; | ||
| * ListNode next; | ||
| * ListNode() {} | ||
| * ListNode(int val) { this.val = val; } | ||
| * ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public ListNode reverseList(ListNode head) { | ||
| if(head == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Deque<Integer> stack = new ArrayDeque<>(); | ||
| ListNode node = head; | ||
|
|
||
| while(node != null) { | ||
| stack.push(node.val); | ||
| node = node.next; | ||
| } | ||
|
|
||
| ListNode dummy = new ListNode(); | ||
| ListNode cur = dummy; | ||
|
|
||
| while(stack.size() > 0) { | ||
| int val = stack.pop(); | ||
| cur.next = new ListNode(val); | ||
| cur = cur.next; | ||
| } | ||
|
|
||
| return dummy.next; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| class Solution { | ||
| public void setZeroes(int[][] matrix) { | ||
| /** | ||
| 1.문제: 0이 존재하는 위치의 모든 row, column을 0으로 set | ||
| 2.constraints: | ||
| - m,n min = 1, max = 200 | ||
| - space: O(mn)으로 풀이하지말 것, | ||
| 3.solution | ||
| - 0의 위치를 확인 -> 0의 위치는 여러개일 수 있음 | ||
| - row, col 각각 0의 위치 저장 | ||
| - time: O(mn), space O(m+n) | ||
| */ | ||
| int m = matrix.length; | ||
| int n = matrix[0].length; | ||
| int[] row = new int[m]; //0이 존재하는 row 위치이면 1, 아니면 0 | ||
| int[] col = new int[n]; //0이 존재하는 col 위치이면 1, 아니면 0 | ||
|
|
||
| int x = 0; int y = 0; //0의 위치 | ||
|
|
||
| for(int i = 0; i < m; i++) { | ||
| for(int j = 0; j < n; j++) { | ||
| if(matrix[i][j] == 0) { | ||
| row[i] = 1; | ||
| col[j] = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for(int i = 0; i < m; i++) { | ||
| for(int j = 0; j < n; j++) { | ||
| if(row[i] == 1 || col[j] == 1) { | ||
| matrix[i][j] = 0; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Solution { | ||
| public int uniquePaths(int m, int n) { | ||
| /** | ||
| 1.문제: finish로 가는 unique path 수 | ||
| 2.constraints: | ||
| - m: 세로, n = 가로 | ||
| - m,n min = 1, max = 100 | ||
| - right, down 으로만 움직이기 가능 | ||
| 3.solution | ||
| - dfs ?? => x, 모든 경로 탐색하지 않음 | ||
| - dp => [i][j] = [i-1][j] + [i][j-1] | ||
| - time: O(mn), space: O(n) | ||
| */ | ||
|
|
||
| int[][] dp = new int[m][n]; | ||
|
|
||
| //첫행, 첫열 = 1 | ||
| for(int i =0; i<m; i++) { | ||
| dp[i][0] = 1; | ||
| } | ||
|
|
||
| for(int i = 0; i < n; i++) { | ||
| dp[0][i] = 1; | ||
| } | ||
|
|
||
| for(int i = 1; i< m; i++) { | ||
| for(int j = 1; j < n; j++) { | ||
| dp[i][j] = dp[i-1][j] + dp[i][j-1]; | ||
| } | ||
| } | ||
| return dp[m-1][n-1]; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석