Skip to content

Yeji/programmers #25

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 6 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/main/java/sgyj/programmers/yeji/cs/Solution42839.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sgyj.programmers.yeji.cs;

import java.util.HashSet;
import java.util.Set;

public class Solution42839 {
private static boolean[] visited;
private static Set<Integer> answer = new HashSet<>();

public static int solution(String numbers) {
String[] numberStr = numbers.split("");
int n = numberStr.length;
visited = new boolean[n];
for(int i=0; i<n; i++){
visited[i] = true;
checkPrime(numberStr,i,n,numberStr[i],0);
}
return answer.size();
}

public static void checkPrime(String[] numberStr, int i, int n,String target, int count){
if(i==n && count != n) i=0;
if(i==n || count == n) return;
if(!visited[i]){
target += numberStr[i];
}
if(isPrime(Integer.valueOf(target))){
answer.add(Integer.valueOf(target));
}
visited[i] = true;
checkPrime(numberStr,i+1,n,target,count+1);
visited[i] = false;
}

public static boolean isPrime(int target){
boolean isPrime = true;
if(target == 0 || target == 1) return false;
for(int p=2; p<target; p++){
if(target % p == 0) {
isPrime = false;
}
}
return isPrime;
}

public static void main ( String[] args ) {
String numbers = "17";
System.out.println(solution(numbers));
}
}
30 changes: 30 additions & 0 deletions src/main/java/sgyj/programmers/yeji/cs/Solution42842.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sgyj.programmers.yeji.cs;

public class Solution42842 {
public static int[] solution(int brown, int yellow) {
int[] answer = new int[2];
int karo = yellow;
int sero = 1;

while(sero <= karo){
int comapreBrown = (karo * 2) + (2 * sero) + 4;
if(comapreBrown == brown){
answer[0] = karo+2;
answer[1] = sero+2;
return answer;
}
sero++;
karo = (yellow / sero) + (yellow % sero);
}

return answer;
}

public static void main ( String[] args ) {
int brown = 10;
int yellow = 2;
for(int s : solution(brown,yellow)){
System.out.print( s + " " );
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/sgyj/programmers/yeji/cs/Solution87946.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sgyj.programmers.yeji.cs;

public class Solution87946 {
private static boolean[] check;
private static int answer = -1;

public static int solution(int k, int[][] dungeons) {
check = new boolean[dungeons.length];

dfs(k,dungeons,0);

return answer;
}

public static void dfs(int tired, int[][] dungeons, int cnt){
for(int i=0; i<dungeons.length; i++){
if(!check[i] && dungeons[i][0]<=tired){
check[i] = true;
dfs(tired-dungeons[i][1],dungeons,cnt+1);
check[i] = false;
}
}
answer = Math.max(answer,cnt);
}
public static void main ( String[] args ) {
int k = 80;
int[][] dungeons = {
// [[80,20],[50,40],[30,10]]
{80,20},{50,40},{30,10}
};

System.out.println( solution(k,dungeons) );
}
}
61 changes: 61 additions & 0 deletions src/main/java/sgyj/programmers/yeji/dfsbfs/Solution1844.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package sgyj.programmers.yeji.dfsbfs;

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution1844 {
private static int[] dx = {1,0,-1,0};
private static int[] dy = {0,-1,0,1};
private static boolean[][] visited;

public static int solution(int[][] maps) {
int answer = 1;
int n = maps.length;
int m = maps[0].length;
visited = new boolean[n][m];
Deque<MapNode> q = new ArrayDeque<>();
q.offer( new MapNode( 0,0 ) );
visited[0][0]=true;
while ( !q.isEmpty() ){
int len = q.size();
for(int i=0; i<len; i++){
MapNode curNode = q.poll();
for(int d=0; d<dx.length; d++){
int x = curNode.x + dx[d];
int y = curNode.y + dy[d];
if(x==n-1 && y==m-1) return answer+1;
if(x>=0 && x<n && y>=0 && y<m && !visited[x][y] && maps[x][y] == 1){
visited[x][y] = true;
q.offer( new MapNode( x,y ) );
}
}
}
answer++;
}

return -1;
}

public static void main ( String[] args ) {
// [[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]]
int[][] maps= {
{1,0,1,1,1},
{1,0,1,0,1},
{1,0,1,1,1},
{1,1,1,0,1},
{0,0,0,0,1}
};
System.out.println(solution(maps));
}

}

class MapNode{
int x;
int y;

MapNode(int x, int y){
this.x = x;
this.y = y;
}
}
55 changes: 55 additions & 0 deletions src/main/java/sgyj/programmers/yeji/dfsbfs/Solution43162.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sgyj.programmers.yeji.dfsbfs;

import java.util.ArrayList;
import java.util.List;

public class Solution43162 {
private static boolean[] visited;
static List<List<Integer>> computerList;

private static int answer = 0;
public static int solution(int n, int[][] computers) {
visited = new boolean[n+1];
computerList = new ArrayList<>(n+1);
for(int i=0; i<=n; i++){
computerList.add( new ArrayList<>(n+1) );
}

for(int i=0; i<computers.length; i++){
for(int j=0; j<computers[i].length; j++){
if(computers[i][j] != 0){
computerList.get( i+1 ).add( j+1 );
}
}
}

for(int i=1; i<computerList.size(); i++){
if(!visited[i] ){
visited[i] = true;
findNetwork(computerList.get( i ),0);
answer++;
}
}

return answer;
}

private static void findNetwork(List<Integer> netWork, int index){
if(index != netWork.size()){
int target = netWork.get( index );
if ( !visited[target] ){
visited[target] = true;
findNetwork( computerList.get( target ),0 );
}
findNetwork( netWork,index+1 );
}
}

public static void main ( String[] args ) {
int[][] computers = {
{1,1,0},{1,1,1},{0,0,1}
};
int n = 3;
System.out.println(solution(n,computers));
}
}
62 changes: 62 additions & 0 deletions src/main/java/sgyj/programmers/yeji/dfsbfs/Solution43163.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package sgyj.programmers.yeji.dfsbfs;

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution43163 {
private static boolean[] visited;
private static int answer = 1;

public static int solution(String begin, String target, String[] words) {
int n = words.length;
visited = new boolean[n];
Deque<Word> q = new ArrayDeque<>();
q.offer( new Word( begin,0 ) );
while ( !q.isEmpty() ){
int len = q.size();
for(int i=0; i<len; i++){
Word cur = q.poll();
if(cur.count <= n){
for(String compare : words){
if(compareAlphabet(cur.word,compare)){
if(compare.equals( target )) return cur.count+1;
q.offer( new Word( compare,cur.count+1 ) );
}
}
}

}
}

return 0;
}

private static boolean compareAlphabet(String word, String compare){
int count = 0;
for(int i=0; i<word.length(); i++){
if(word.charAt( i ) != compare.charAt( i )){
count++;
}
}

return count == 1;
}


public static void main ( String[] args ) {
String begin = "hit";
String target = "cog";
String[] words = {"hot", "dot", "dog", "lot", "log"};
System.out.println(solution(begin,target,words));
}
}

class Word{
String word;
int count;

Word(String word, int count){
this.word = word;
this.count = count;
}
}
73 changes: 73 additions & 0 deletions src/main/java/sgyj/programmers/yeji/dfsbfs/Solution43164.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package sgyj.programmers.yeji.dfsbfs;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

public class Solution43164 {

private static Map<String,List<String>> ticketMap = new HashMap<>();
private static PriorityQueue<String> pQ = new PriorityQueue<>();

public static String[] solution(String[][] tickets) {

for(String[] ticket : tickets){
List<String> getList = ticketMap.getOrDefault( ticket[0], new ArrayList<>() );
getList.add( ticket[1] );
ticketMap.put( ticket[0] ,getList);
}

List<String> answer = new ArrayList<>();
pQ.offer( "ICN" );

while ( !pQ.isEmpty() ){
int len = pQ.size();
for(int i=0; i<len; i++){
String cur = pQ.poll();
answer.add( cur );
if(!answer.contains( cur )) {
List<String> strings = ticketMap.get( cur );
for ( String string : strings ) {
pQ.offer( string );
}
}
}
}

return answer.toArray( new String[0] );
}

public static void main ( String[] args ) {
String[][] tickets = {
{"ICN", "JFK"},{"HND", "IAD"},{"JFK", "HND"}
};

for(String ticket : solution(tickets)){
System.out.print(ticket + " ");
}
}
}

class AirPort implements Comparable<AirPort>{
String airPort;
boolean visited;

AirPort(String airPort){
this.airPort = airPort;
}

@Override
public int compareTo ( AirPort o ) {
return this.airPort.compareTo( o.airPort );
}

public void visited (){
this.visited = true;
}
}
Loading