'개발/CodingTest'에 해당되는 글 10건

  1. 2020.06.26 LeetCode - Min Stack
  2. 2020.06.26 LeetCode - Shuffle the array
  3. 2020.06.25 Hackerrank - Utopian Tree
  4. 2020.06.25 Hacckerrank - The Hurdle Race
  5. 2020.06.25 Hacckerrank - Halloween Sale
  6. 2020.06.05 Hackerrank - Prime Checker
  7. 2020.06.03 프로그래머스 - 기능개발
  8. 2020.06.03 프로그래머스 - 탑

음..성능이..

class MinStack {
    
    /** initialize your data structure here. */
    class Node{
        int val;
        Node next;
        
        Node(int val){
            this.val = val;
            this.next = null;
        }
    }
    
    Node top = null;
    public MinStack() {
        
    }
    
    public void push(int x) {
        Node node = new Node(x);
        node.next = top;        
        top = node;
    }
    
    public void pop() {
        top = top.next;
    }
    
    public int top() {
        return top.val;        
    }
    
    public int getMin() {
        Node node = top;
        int min = node.val;
        while(node != null){
            if(min > node.val){
                min = node.val;
            }
            node = node.next;
        }
        return min;
    }
}

'개발 > CodingTest' 카테고리의 다른 글

LeetCode - Shuffle the array  (0) 2020.06.26
Hackerrank - Utopian Tree  (0) 2020.06.25
Hacckerrank - The Hurdle Race  (0) 2020.06.25
Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
Posted by Lumasca
,
class Solution {
    public int[] shuffle(int[] nums, int n) {
        int len = nums.length;
        int[] res = new int[len];
        int resIndex = 0;
        for(int i=0; i < len/2; i++){//배열 중간까지만 진행
            res[resIndex] = nums[i];//첫번째값
            res[++resIndex] = nums[i+n];//두번째값은 n만큼 더한 위치
            resIndex++;
        }
        return res;
    }
}

'개발 > CodingTest' 카테고리의 다른 글

LeetCode - Min Stack  (0) 2020.06.26
Hackerrank - Utopian Tree  (0) 2020.06.25
Hacckerrank - The Hurdle Race  (0) 2020.06.25
Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
Posted by Lumasca
,
    static int utopianTree(int n) {
        
        int h = 0;
        
        for(int i = 0; i <= n; i++){
            if(i==0){
                h = 1;
                continue;
            }
            if(i % 2 == 1){
                h *= 2;
            }else{
                h += 1;
            }
        }
        return h;
    }

'개발 > CodingTest' 카테고리의 다른 글

LeetCode - Min Stack  (0) 2020.06.26
LeetCode - Shuffle the array  (0) 2020.06.26
Hacckerrank - The Hurdle Race  (0) 2020.06.25
Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
Posted by Lumasca
,

 

    static int hurdleRace(int k, int[] height) {
        int n = 0;
        for(int i =0; i < height.length; i++){
            int r = height[i] - k;
            if(r > 0){
                if(n < r){
                    n = r;
                }
            }
        }
        return n;

    }

'개발 > CodingTest' 카테고리의 다른 글

LeetCode - Shuffle the array  (0) 2020.06.26
Hackerrank - Utopian Tree  (0) 2020.06.25
Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
프로그래머스 - 기능개발  (0) 2020.06.03
Posted by Lumasca
,
 static int howManyGames(int p, int d, int m, int s) {
        // Return the number of games you can buy
        int salePrice = p;//세일 시작가격
        int count = 0;//구매한 게임수
        while(s >= salePrice ){
            count++;//게임개수증가
            s -= salePrice;//남은돈
            //System.out.println("Buy salePrice="+salePrice+", count="+count+", s="+s);
            if(salePrice > m){
                salePrice -= d;//세일 적용
            }
            if(salePrice < m){//최저세일가 보장
                salePrice = m;
            }           
        }
        return count;
    }

'개발 > CodingTest' 카테고리의 다른 글

Hackerrank - Utopian Tree  (0) 2020.06.25
Hacckerrank - The Hurdle Race  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
프로그래머스 - 기능개발  (0) 2020.06.03
프로그래머스 - 탑  (0) 2020.06.03
Posted by Lumasca
,
import static java.lang.System.in;//in cannot find Symbol error

class Prime{
    void checkPrime(int... nums){//가변인자
        for(int num : nums){
            if(isPrime(num)){
                System.out.print(num+" ");//숫자출력 & 공백
            }
        }
        System.out.println("");//줄바꿈
    }

    boolean isPrime(int num){
        if(num < 2){
            return false;
        }
        boolean prime = true;
        for(int i = 2; i < num; i++){
            if(num % i == 0){//1과 자기 자신을 뺀 나머지 숫자로 나누어지면 소수가 아님
                prime = false;
                break;
            }
        }
        return prime;        
    }
}

'개발 > CodingTest' 카테고리의 다른 글

Hacckerrank - The Hurdle Race  (0) 2020.06.25
Hacckerrank - Halloween Sale  (0) 2020.06.25
프로그래머스 - 기능개발  (0) 2020.06.03
프로그래머스 - 탑  (0) 2020.06.03
HackerRank - Anagrams  (0) 2020.06.02
Posted by Lumasca
,

 

import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        
        Queue<Integer> q = new LinkedList();
        
        //남은 작업 일수
        for(int i =0; i<progresses.length; i++){
            int re = 100 - progresses[i];
            int s = speeds[i];
            int d = re / s;
            if(re % s != 0){
                d++;
            }        
            q.offer(d);
        }
        
        
        int[] tmp = new int[q.size()];
        int sd = 0;//배포일
        int count = 0;
        int idx = 0;
        //작업 순서대로 배포날짜를 체크
        while(q.peek() != null){
            int dd = q.poll();
            
            if(sd == 0){
                sd = dd;
                tmp[idx] = ++count;
                continue;
            }
            
            if(sd >= dd){//후행작업의 배포가능날짜가 작으면 현재 작업과 같이 배포(동시배포 개수 증가)               
                tmp[idx] = ++count;
            }else{//후행작업의 배포가능날짜가 크면 늦게 배포(동시 배포 개수 초기화)
                idx++;
                count = 0;
                sd = dd;
                tmp[idx] = ++count;
            }
            
        }
        
        int[] answer = new int[idx+1];
        for(int i =0; i<answer.length; i++){
            answer[i] = tmp[i];
        }
        
        return answer;
    }
}

'개발 > CodingTest' 카테고리의 다른 글

Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
프로그래머스 - 탑  (0) 2020.06.03
HackerRank - Anagrams  (0) 2020.06.02
HackerRank - Counting Valley  (0) 2020.06.01
Posted by Lumasca
,

이중 for는 맘에 안들지만 ..

스택을 어떻게 활용하는 걸까?

import java.util.*;
class Solution {
    public int[] solution(int[] heights) {
        
        int len = heights.length;
        int[] answer = new int[len];
        
        //레이저가 왼쪽으로 발사하니 오른쪽 탑부터..
        for(int i = len-1; i > 0 ; i--){
            boolean recv = false;
            
            for(int j=i-1; j>=0; j-- ){
                if(heights[i] < heights[j]){//현재 탑 높이보다 높으면 수신탑 기록
                    answer[i] = j+1;//탑번호는 1부터 시작하니 더해주고
                    recv = true;
                    break;
                }
            }
            if(!recv){
                answer[i] = 0;
            }
        }
        //System.out.println("result="+Arrays.toString(answer));
        return answer;
    }
}

 

'개발 > CodingTest' 카테고리의 다른 글

Hacckerrank - Halloween Sale  (0) 2020.06.25
Hackerrank - Prime Checker  (0) 2020.06.05
프로그래머스 - 기능개발  (0) 2020.06.03
HackerRank - Anagrams  (0) 2020.06.02
HackerRank - Counting Valley  (0) 2020.06.01
Posted by Lumasca
,