Skip to content

Commit 72439b7

Browse files
authored
Merge pull request #11 from BigOasis/java/EunseoPark
[1주차] EunseoPark - 9문제
2 parents 6a1d007 + 4700d4f commit 72439b7

10 files changed

Lines changed: 456 additions & 7 deletions
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.*;
2+
/*
3+
[스스로 풀이] 0ms 43.52MB
4+
*/
5+
class Solution {
6+
public int search(int[] nums, int target) {
7+
int l = 0, r = nums.length-1;
8+
9+
while(l <= r){
10+
int mid = (l+r)/2;
11+
if(nums[mid] == target) return mid;
12+
13+
else if(nums[mid] < target){
14+
l = mid + 1;
15+
}
16+
else{
17+
r = mid -1;
18+
}
19+
}
20+
return -1;
21+
}
22+
}
23+
import java.util.*;
24+
/*
25+
[2차 메서드] 0ms 45.88MB
26+
이거 메서드있었는데 해서 메서드 바로 적용
27+
*/
28+
class Solution {
29+
public int search(int[] nums, int target) {
30+
int result = Arrays.binarySearch(nums, target);
31+
32+
if(result < 0) return -1;
33+
return result;
34+
}
35+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.*;
2+
/*
3+
[1차 스스로 풀이] 63ms 94.56MB
4+
*/
5+
class Solution {
6+
public List<List<Integer>> combine(int n, int k) {
7+
List<List<Integer>> result = new ArrayList<>();
8+
comb(result, new ArrayList<>(), n, k, 1);
9+
return result;
10+
}
11+
private void comb(List<List<Integer>> result, List<Integer> cur, int n, int k, int now){
12+
if(cur.size() == k) {
13+
result.add(new ArrayList<>(cur));
14+
return;
15+
}
16+
17+
for(int i = now; i <= n; i++){
18+
if(cur.contains(i)) continue;
19+
cur.add(i);
20+
comb(result, cur, n, k, i);
21+
cur.remove(cur.size()-1);
22+
}
23+
}
24+
}
25+
26+
27+
import java.util.*;
28+
/*
29+
[2차 성능 최적화 참고] 18ms 94.56MB
30+
1. contains -> O(N^2) 소요를 제거
31+
2. i => i + 1을 넘겨 중복 방지
32+
*/
33+
class Solution {
34+
public List<List<Integer>> combine(int n, int k) {
35+
List<List<Integer>> result = new ArrayList<>();
36+
comb(result, new ArrayList<>(), n, k, 1);
37+
return result;
38+
}
39+
private void comb(List<List<Integer>> result, List<Integer> cur, int n, int k, int now){
40+
if(cur.size() == k) {
41+
result.add(new ArrayList<>(cur));
42+
return;
43+
}
44+
45+
for(int i = now; i <= n; i++){
46+
cur.add(i);
47+
comb(result, cur, n, k, i + 1);
48+
cur.remove(cur.size()-1);
49+
}
50+
}
51+
}

EunseoPark/Week1/LTC_N-Queen.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.*;
2+
/*
3+
1. 방문 위치를 기록하기 위해 -1로 초기화하고, (행 번호, 놓인 열 위치) 기록
4+
2. -1 && 갈 수 있으면 backtrack
5+
2. canGo :
6+
첫 시도 :
7+
now가 0, n-1, 중간일때 상하좌우 대각선을 while로 업데이트하면 확인
8+
*/
9+
class Solution {
10+
static int[] visited;
11+
12+
public List<List<String>> solveNQueens(int n) {
13+
List<List<String>> result = new ArrayList<>();
14+
visited = new int[n];
15+
Arrays.fill(visited, -1);
16+
backtrack(0, n, result);
17+
return result;
18+
}
19+
private void backtrack(int now, int n, List<List<String>> result){
20+
if(now == n) {
21+
List<String> cur = new ArrayList<>();
22+
23+
for(int idx : visited){
24+
StringBuilder sb = new StringBuilder();
25+
sb.append(".".repeat(n));
26+
sb.setCharAt(idx,'Q');
27+
cur.add(sb.toString());
28+
}
29+
result.add(cur);
30+
return;
31+
}
32+
for(int t = 0; t < n; t++){
33+
if(canGo(now, t, n)){
34+
visited[now] = t;
35+
backtrack(now + 1, n , result);
36+
visited[now] = -1;
37+
}
38+
}
39+
}
40+
41+
// now 번째 queen이 t 인덱스에 갈 수 있는지
42+
// 🔍 이전 행만 확인
43+
private boolean canGo(int now, int t, int n){
44+
for(int pre = 0; pre < now; pre++){
45+
if(visited[pre]==t) return false; //이미 지난
46+
// 🔍 [행 차 == 열 차
47+
if(Math.abs(pre - now) == Math.abs(visited[pre] - t)) return false;
48+
}
49+
return true;
50+
}
51+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
3+
/*
4+
문자열.substring(startIndex, endIndex)
5+
6+
[ 정답 참고 ] 20ms
7+
1. 첫 시도는 a, aa , aab 이렇게 만들어서 새로운 문자열 자체를 계속
8+
넘겨야 하나 해서 헷갈리고 꼬임
9+
10+
// 정답 참고 정리
11+
1. 문자열이 아닌 (start index) 를 넘겨 새로운 문자열을 만들기
12+
*/
13+
class Solution {
14+
public List<List<String>> partition(String s) {
15+
List<List<String>> result = new ArrayList<>();
16+
part(result, new ArrayList<>(), s, 0);
17+
return result;
18+
}
19+
20+
private void part(List<List<String>> result, List<String> cur, String origin, int start){
21+
if(start == origin.length()) {
22+
result.add(new ArrayList<>(cur));
23+
return;
24+
}
25+
26+
for(int i = start + 1, length = origin.length(); i <= length; i++){
27+
String newStr = origin.substring(start, i);
28+
29+
if(isSame(newStr)){
30+
cur.add(newStr);
31+
part(result, cur, origin, i);
32+
cur.remove(cur.size()-1);
33+
}
34+
}
35+
}
36+
private boolean isSame(String s){
37+
StringBuilder sb = new StringBuilder(s);
38+
return sb.reverse().toString().equals(s);
39+
}
40+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public List<List<Integer>> permute(int[] nums) {
5+
List<List<Integer>> result = new ArrayList<>();
6+
per(result, new ArrayList<>(), nums);
7+
return result;
8+
}
9+
10+
private void per(List<List<Integer>> result, List<Integer> cur, int[] nums){
11+
if(cur.size() == nums.length){
12+
result.add(new ArrayList<>(cur));
13+
return;
14+
}
15+
16+
for(int i = 0, length = nums.length; i < length; i++){
17+
if(cur.contains(nums[i])) continue;
18+
cur.add(nums[i]);
19+
per(result, cur, nums);
20+
cur.remove(cur.size()-1);
21+
}
22+
}
23+
}

EunseoPark/Week1/LTC_Subsets.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.*;
2+
3+
/*
4+
[정답 참고] 0ms 43.86MB
5+
sub 넘길때 now+1 로 시도했음
6+
-> 정답 참고 후 i + 1로 변경
7+
*/
8+
class Solution {
9+
public List<List<Integer>> subsets(int[] nums) {
10+
List<List<Integer>> result = new ArrayList<>();
11+
sub(result, new ArrayList<>(), nums, 0);
12+
return result;
13+
}
14+
private void sub(List<List<Integer>> result, List<Integer> cur, int[] nums, int now){
15+
System.out.println(cur);
16+
result.add(new ArrayList<>(cur));
17+
18+
for(int i = now, length = nums.length; i < length; i++){
19+
cur.add(nums[i]);
20+
sub(result, cur, nums, i+1);
21+
cur.remove(cur.size()-1);
22+
}
23+
}
24+
}

EunseoPark/Week1/LTC_Two Sum.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
/*
3+
[ 1차 스스로 풀이 ]
4+
1. 탐색이라 O(1)인 해쉬맵이 떠오름
5+
1. (값, 인덱스) 저장 -> O(N)
6+
2. i 돌면서 target - nums[i] && 자기 자신이 아니면 return 해당 값 -> O(N)
7+
= O(N) + O(N) = O(2N)
8+
*/
9+
class Solution {
10+
public int[] twoSum(int[] nums, int target) {
11+
Map<Integer,Integer> hm = new HashMap<>();
12+
int length = nums.length;
13+
for(int i = 0; i< length; i++){
14+
hm.put(nums[i], i);
15+
}
16+
for(int i = 0; i < length; i++){
17+
int t = target - nums[i];
18+
if(hm.containsKey(t) && hm.get(t) != i){
19+
return new int[]{i, hm.get(t)};
20+
}
21+
}
22+
return new int[]{};
23+
}
24+
}
25+
26+
import java.util.*;
27+
/*
28+
[ 2차 다른 코드 참고 후 최적화 ]
29+
1. 탐색이라 O(1)인 해쉬맵이 떠오름
30+
1차 스스로 풀이에서 2번 예제의 자기 자신 제외를 저렇게 처리하지 말고
31+
1. 넣을때 target -nums[i] 가 있다면 바로 return
32+
2. 없으면 해시 값에 추가
33+
= O(N)
34+
*/
35+
class Solution {
36+
public int[] twoSum(int[] nums, int target) {
37+
Map<Integer,Integer> hm = new HashMap<>();
38+
39+
for(int i = 0, length = nums.length; i< length; i++){
40+
int t = target - nums[i];
41+
if(hm.containsKey(t)){
42+
return new int[]{i, hm.get(t)};
43+
}
44+
hm.put(nums[i], i);
45+
}
46+
return new int[]{};
47+
}
48+
}
49+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.util.*;
2+
/*
3+
[1차 스스로 풀이] 163ms / 42.06 MB
4+
1. 최단거리 문제는 bfs가 더 빠르겠지만, 여기서는 순서대로 밟고 가야 하는 경로가 있으므로 dfs
5+
2. 보완할 점
6+
1) exist함수에서 이중 for문으로 검사하는 것이 아닌 바로 dfs 호출하기 -> 기억에 안남
7+
2) 재귀함수 호출하면서 return 하는 값을 잘 처리하고 싶음.
8+
*/
9+
class Solution {
10+
static int[] dy = {-1,1,0,0}, dx = {0,0,-1,1};
11+
static int r,c;
12+
13+
public boolean exist(char[][] board, String word) {
14+
r = board.length; c = board[0].length;
15+
boolean[][] visited = new boolean[r][c];
16+
int count = 0;
17+
boolean result = false;
18+
19+
for(int i = 0 ; i < r; i++){
20+
for(int j = 0 ; j < c; j++){
21+
if(!visited[i][j] && word.charAt(count) == board[i][j]){
22+
visited[i][j] = true;
23+
result = dfs(count+1, i, j, word, visited, board);
24+
System.out.println("\n");
25+
if(result) return result;
26+
visited[i][j] = false;
27+
}
28+
}
29+
}
30+
return result;
31+
}
32+
private boolean dfs(int count,int y, int x, String word, boolean[][] visited, char[][] board){
33+
if(count == word.length()) return true;
34+
boolean result = false;
35+
36+
for(int d =0; d <4; d++){
37+
int ny = y + dy[d], nx = x + dx[d];
38+
39+
if(inRange(ny,nx) && !visited[ny][nx] && word.charAt(count) == board[ny][nx]){
40+
visited[ny][nx] = true;
41+
result = dfs(count + 1, ny, nx, word, visited,board);
42+
if(result) return result;
43+
visited[ny][nx] = false;
44+
}
45+
}
46+
return result;
47+
}
48+
49+
private boolean inRange(int y, int x){
50+
return -1 < y && y < r && -1 < x && x <c;
51+
}
52+
}
53+
54+
55+
import java.util.*;
56+
/*
57+
[2차 최적화 코드 참고] 149ms / 41.62 MB
58+
1. visited 배열 삭제, board 자체를 변경
59+
2. 불필요 변수 count, result 제거
60+
61+
*/
62+
class Solution {
63+
static int[] dy = {-1,1,0,0}, dx = {0,0,-1,1};
64+
static int r,c;
65+
66+
public boolean exist(char[][] board, String word) {
67+
r = board.length; c = board[0].length;
68+
69+
for(int i = 0 ; i < r; i++){
70+
for(int j = 0 ; j < c; j++){
71+
if(word.charAt(0) == board[i][j]){
72+
if(dfs(1, i, j, word, board)){
73+
return true;
74+
}
75+
}
76+
}
77+
}
78+
return false;
79+
}
80+
private boolean dfs(int count,int y, int x, String word, char[][] board){
81+
if(count == word.length()) return true;
82+
83+
char tmp = board[y][x];
84+
board[y][x] = '#';
85+
86+
for(int d =0; d <4; d++){
87+
int ny = y + dy[d], nx = x + dx[d];
88+
89+
if(inRange(ny,nx) && board[ny][nx]!='#' && word.charAt(count) == board[ny][nx]){
90+
if(dfs(count + 1, ny, nx, word, board)){
91+
return true;
92+
}
93+
}
94+
}
95+
board[y][x] = tmp;
96+
return false;
97+
}
98+
99+
private boolean inRange(int y, int x){
100+
return -1 < y && y < r && -1 < x && x <c;
101+
}
102+
}

0 commit comments

Comments
 (0)