-
Notifications
You must be signed in to change notification settings - Fork 1
[1주차] EunseoPark - 9문제 #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e5174ee
✏️comment: PGS_피로도_주석 추가 및 성능 개선 #2
arty0928 647c74b
✨feat: LTC_Two Sum #2
arty0928 91a3132
feat: LTC_Word Search #2
arty0928 e17a4b5
feat: LTC_Permutations #2
arty0928 2664bba
feat: LTC_Combinations #2
arty0928 f7c5cfc
feat: LTC_Subsets #2
arty0928 7676b34
feat: LTC_Binary Search #2
arty0928 93bb001
feat: LTC_Palindrome Partitioning #2
arty0928 5499b0a
feat: LTC_N-Queen #2
arty0928 4700d4f
feat: PGS_메뉴 리뉴얼 (중간) #2
arty0928 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import java.util.*; | ||
| /* | ||
| [스스로 풀이] 0ms 43.52MB | ||
| */ | ||
| class Solution { | ||
| public int search(int[] nums, int target) { | ||
| int l = 0, r = nums.length-1; | ||
|
|
||
| while(l <= r){ | ||
| int mid = (l+r)/2; | ||
| if(nums[mid] == target) return mid; | ||
|
|
||
| else if(nums[mid] < target){ | ||
| l = mid + 1; | ||
| } | ||
| else{ | ||
| r = mid -1; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
| } | ||
| import java.util.*; | ||
| /* | ||
| [2차 메서드] 0ms 45.88MB | ||
| 이거 메서드있었는데 해서 메서드 바로 적용 | ||
| */ | ||
| class Solution { | ||
| public int search(int[] nums, int target) { | ||
| int result = Arrays.binarySearch(nums, target); | ||
|
|
||
| if(result < 0) return -1; | ||
| return result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import java.util.*; | ||
| /* | ||
| [1차 스스로 풀이] 63ms 94.56MB | ||
| */ | ||
| class Solution { | ||
| public List<List<Integer>> combine(int n, int k) { | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| comb(result, new ArrayList<>(), n, k, 1); | ||
| return result; | ||
| } | ||
| private void comb(List<List<Integer>> result, List<Integer> cur, int n, int k, int now){ | ||
| if(cur.size() == k) { | ||
| result.add(new ArrayList<>(cur)); | ||
| return; | ||
| } | ||
|
|
||
| for(int i = now; i <= n; i++){ | ||
| if(cur.contains(i)) continue; | ||
| cur.add(i); | ||
| comb(result, cur, n, k, i); | ||
| cur.remove(cur.size()-1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| import java.util.*; | ||
| /* | ||
| [2차 성능 최적화 참고] 18ms 94.56MB | ||
| 1. contains -> O(N^2) 소요를 제거 | ||
| 2. i => i + 1을 넘겨 중복 방지 | ||
| */ | ||
| class Solution { | ||
| public List<List<Integer>> combine(int n, int k) { | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| comb(result, new ArrayList<>(), n, k, 1); | ||
| return result; | ||
| } | ||
| private void comb(List<List<Integer>> result, List<Integer> cur, int n, int k, int now){ | ||
| if(cur.size() == k) { | ||
| result.add(new ArrayList<>(cur)); | ||
| return; | ||
| } | ||
|
|
||
| for(int i = now; i <= n; i++){ | ||
| cur.add(i); | ||
| comb(result, cur, n, k, i + 1); | ||
| cur.remove(cur.size()-1); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import java.util.*; | ||
| /* | ||
| 1. 방문 위치를 기록하기 위해 -1로 초기화하고, (행 번호, 놓인 열 위치) 기록 | ||
| 2. -1 && 갈 수 있으면 backtrack | ||
| 2. canGo : | ||
| 첫 시도 : | ||
| now가 0, n-1, 중간일때 상하좌우 대각선을 while로 업데이트하면 확인 | ||
| */ | ||
| class Solution { | ||
| static int[] visited; | ||
|
|
||
| public List<List<String>> solveNQueens(int n) { | ||
| List<List<String>> result = new ArrayList<>(); | ||
| visited = new int[n]; | ||
| Arrays.fill(visited, -1); | ||
| backtrack(0, n, result); | ||
| return result; | ||
| } | ||
| private void backtrack(int now, int n, List<List<String>> result){ | ||
| if(now == n) { | ||
| List<String> cur = new ArrayList<>(); | ||
|
|
||
| for(int idx : visited){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append(".".repeat(n)); | ||
| sb.setCharAt(idx,'Q'); | ||
| cur.add(sb.toString()); | ||
| } | ||
| result.add(cur); | ||
| return; | ||
| } | ||
| for(int t = 0; t < n; t++){ | ||
| if(canGo(now, t, n)){ | ||
| visited[now] = t; | ||
| backtrack(now + 1, n , result); | ||
| visited[now] = -1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // now 번째 queen이 t 인덱스에 갈 수 있는지 | ||
| // 🔍 이전 행만 확인 | ||
| private boolean canGo(int now, int t, int n){ | ||
| for(int pre = 0; pre < now; pre++){ | ||
| if(visited[pre]==t) return false; //이미 지난 | ||
| // 🔍 [행 차 == 열 차 | ||
| if(Math.abs(pre - now) == Math.abs(visited[pre] - t)) return false; | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import java.util.*; | ||
|
|
||
| /* | ||
| 문자열.substring(startIndex, endIndex) | ||
|
|
||
| [ 정답 참고 ] 20ms | ||
| 1. 첫 시도는 a, aa , aab 이렇게 만들어서 새로운 문자열 자체를 계속 | ||
| 넘겨야 하나 해서 헷갈리고 꼬임 | ||
|
|
||
| // 정답 참고 정리 | ||
| 1. 문자열이 아닌 (start index) 를 넘겨 새로운 문자열을 만들기 | ||
| */ | ||
| class Solution { | ||
| public List<List<String>> partition(String s) { | ||
| List<List<String>> result = new ArrayList<>(); | ||
| part(result, new ArrayList<>(), s, 0); | ||
| return result; | ||
| } | ||
|
|
||
| private void part(List<List<String>> result, List<String> cur, String origin, int start){ | ||
| if(start == origin.length()) { | ||
| result.add(new ArrayList<>(cur)); | ||
| return; | ||
| } | ||
|
|
||
| for(int i = start + 1, length = origin.length(); i <= length; i++){ | ||
| String newStr = origin.substring(start, i); | ||
|
|
||
| if(isSame(newStr)){ | ||
| cur.add(newStr); | ||
| part(result, cur, origin, i); | ||
| cur.remove(cur.size()-1); | ||
| } | ||
| } | ||
| } | ||
| private boolean isSame(String s){ | ||
| StringBuilder sb = new StringBuilder(s); | ||
| return sb.reverse().toString().equals(s); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public List<List<Integer>> permute(int[] nums) { | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| per(result, new ArrayList<>(), nums); | ||
| return result; | ||
| } | ||
|
|
||
| private void per(List<List<Integer>> result, List<Integer> cur, int[] nums){ | ||
| if(cur.size() == nums.length){ | ||
| result.add(new ArrayList<>(cur)); | ||
| return; | ||
| } | ||
|
|
||
| for(int i = 0, length = nums.length; i < length; i++){ | ||
| if(cur.contains(nums[i])) continue; | ||
| cur.add(nums[i]); | ||
| per(result, cur, nums); | ||
| cur.remove(cur.size()-1); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import java.util.*; | ||
|
|
||
| /* | ||
| [정답 참고] 0ms 43.86MB | ||
| sub 넘길때 now+1 로 시도했음 | ||
| -> 정답 참고 후 i + 1로 변경 | ||
| */ | ||
| class Solution { | ||
| public List<List<Integer>> subsets(int[] nums) { | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| sub(result, new ArrayList<>(), nums, 0); | ||
| return result; | ||
| } | ||
| private void sub(List<List<Integer>> result, List<Integer> cur, int[] nums, int now){ | ||
| System.out.println(cur); | ||
| result.add(new ArrayList<>(cur)); | ||
|
|
||
| for(int i = now, length = nums.length; i < length; i++){ | ||
| cur.add(nums[i]); | ||
| sub(result, cur, nums, i+1); | ||
| cur.remove(cur.size()-1); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import java.util.*; | ||
| /* | ||
| [ 1차 스스로 풀이 ] | ||
| 1. 탐색이라 O(1)인 해쉬맵이 떠오름 | ||
| 1. (값, 인덱스) 저장 -> O(N) | ||
| 2. i 돌면서 target - nums[i] && 자기 자신이 아니면 return 해당 값 -> O(N) | ||
| = O(N) + O(N) = O(2N) | ||
| */ | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| Map<Integer,Integer> hm = new HashMap<>(); | ||
| int length = nums.length; | ||
| for(int i = 0; i< length; i++){ | ||
| hm.put(nums[i], i); | ||
| } | ||
| for(int i = 0; i < length; i++){ | ||
| int t = target - nums[i]; | ||
| if(hm.containsKey(t) && hm.get(t) != i){ | ||
| return new int[]{i, hm.get(t)}; | ||
| } | ||
| } | ||
| return new int[]{}; | ||
| } | ||
| } | ||
|
|
||
| import java.util.*; | ||
| /* | ||
| [ 2차 다른 코드 참고 후 최적화 ] | ||
| 1. 탐색이라 O(1)인 해쉬맵이 떠오름 | ||
| 1차 스스로 풀이에서 2번 예제의 자기 자신 제외를 저렇게 처리하지 말고 | ||
| 1. 넣을때 target -nums[i] 가 있다면 바로 return | ||
| 2. 없으면 해시 값에 추가 | ||
| = O(N) | ||
| */ | ||
| class Solution { | ||
| public int[] twoSum(int[] nums, int target) { | ||
| Map<Integer,Integer> hm = new HashMap<>(); | ||
|
|
||
|
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. 기존 코드도 깔끔한데 추가로 최적화하는 방안 생각하는 거 좋습니다 |
||
| for(int i = 0, length = nums.length; i< length; i++){ | ||
| int t = target - nums[i]; | ||
| if(hm.containsKey(t)){ | ||
| return new int[]{i, hm.get(t)}; | ||
| } | ||
| hm.put(nums[i], i); | ||
| } | ||
| return new int[]{}; | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import java.util.*; | ||
| /* | ||
| [1차 스스로 풀이] 163ms / 42.06 MB | ||
| 1. 최단거리 문제는 bfs가 더 빠르겠지만, 여기서는 순서대로 밟고 가야 하는 경로가 있으므로 dfs | ||
| 2. 보완할 점 | ||
| 1) exist함수에서 이중 for문으로 검사하는 것이 아닌 바로 dfs 호출하기 -> 기억에 안남 | ||
| 2) 재귀함수 호출하면서 return 하는 값을 잘 처리하고 싶음. | ||
| */ | ||
| class Solution { | ||
| static int[] dy = {-1,1,0,0}, dx = {0,0,-1,1}; | ||
| static int r,c; | ||
|
|
||
| public boolean exist(char[][] board, String word) { | ||
| r = board.length; c = board[0].length; | ||
| boolean[][] visited = new boolean[r][c]; | ||
| int count = 0; | ||
| boolean result = false; | ||
|
|
||
| for(int i = 0 ; i < r; i++){ | ||
| for(int j = 0 ; j < c; j++){ | ||
| if(!visited[i][j] && word.charAt(count) == board[i][j]){ | ||
| visited[i][j] = true; | ||
| result = dfs(count+1, i, j, word, visited, board); | ||
| System.out.println("\n"); | ||
| if(result) return result; | ||
| visited[i][j] = false; | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| private boolean dfs(int count,int y, int x, String word, boolean[][] visited, char[][] board){ | ||
| if(count == word.length()) return true; | ||
| boolean result = false; | ||
|
|
||
| for(int d =0; d <4; d++){ | ||
| int ny = y + dy[d], nx = x + dx[d]; | ||
|
|
||
| if(inRange(ny,nx) && !visited[ny][nx] && word.charAt(count) == board[ny][nx]){ | ||
| visited[ny][nx] = true; | ||
| result = dfs(count + 1, ny, nx, word, visited,board); | ||
| if(result) return result; | ||
| visited[ny][nx] = false; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private boolean inRange(int y, int x){ | ||
| return -1 < y && y < r && -1 < x && x <c; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| import java.util.*; | ||
| /* | ||
| [2차 최적화 코드 참고] 149ms / 41.62 MB | ||
| 1. visited 배열 삭제, board 자체를 변경 | ||
| 2. 불필요 변수 count, result 제거 | ||
|
|
||
| */ | ||
| class Solution { | ||
| static int[] dy = {-1,1,0,0}, dx = {0,0,-1,1}; | ||
| static int r,c; | ||
|
|
||
| public boolean exist(char[][] board, String word) { | ||
| r = board.length; c = board[0].length; | ||
|
|
||
| for(int i = 0 ; i < r; i++){ | ||
| for(int j = 0 ; j < c; j++){ | ||
| if(word.charAt(0) == board[i][j]){ | ||
| if(dfs(1, i, j, word, board)){ | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| private boolean dfs(int count,int y, int x, String word, char[][] board){ | ||
| if(count == word.length()) return true; | ||
|
|
||
| char tmp = board[y][x]; | ||
| board[y][x] = '#'; | ||
|
|
||
| for(int d =0; d <4; d++){ | ||
| int ny = y + dy[d], nx = x + dx[d]; | ||
|
|
||
| if(inRange(ny,nx) && board[ny][nx]!='#' && word.charAt(count) == board[ny][nx]){ | ||
| if(dfs(count + 1, ny, nx, word, board)){ | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| board[y][x] = tmp; | ||
| return false; | ||
| } | ||
|
|
||
| private boolean inRange(int y, int x){ | ||
| return -1 < y && y < r && -1 < x && x <c; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
행 차 열 차 절댓값으로 계산하는 건 생각 못했던 부분이라서 괜찮은 것 같아요