PL(Programming Language)/Java

[HackerRank] Java Warmup

탱젤 2021. 10. 14. 17:56

https://www.hackerrank.com/domains/algorithms?filters%5Bsubdomains%5D%5B%5D=warmup 

 

Solve Algorithms | HackerRank

Solve Algorithms | HackerRank We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

www.hackerrank.com

SimpleArraySum

  • ArrayList의 크기는 .size()로 구하기 (.length 못씀!!)
  • ArrayList의 원소 접근 .get()
  • for-each문 잊지말기
public static int simpleArraySum(List<Integer> ar) {
    // Write your code here
    int sum = 0;
    int size = ar.size();
    
    // arraylist의 크기 size()로 구하기
    // 원소 접근 get()

    for (int i = 0; i < size; i++)
        sum += ar.get(i);
    return sum;
    
    /* for-each 문으로 합 구하기 !!
    int sum = 0;
        for(int i : ar)
        {
            sum += i;
        }
        return sum;
    */

}

A Very Big Sum

  • 32bit int말고 62bit long 타입 잊지 말기
public static long aVeryBigSum(List<Long> ar) {
    // Write your code here
        long sum = 0; // int말고 long타입 쓰기
        for(int i = 0; i < ar.size(); i++){
            sum += ar.get(i);
        }
        return sum;
    }

Compare the Triplets

  • ArrayList 원소 값 변경은 .set(index, 값)
public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
    // Write your code here
        List<Integer> c = new ArrayList<>();
        c.add(0);
        c.add(0);
        for(int i = 0; i < a.size(); i++)
        {
            if(a.get(i) > b.get(i)) c.set(0, c.get(0) + 1);
            else if(a.get(i) < b.get(i)) c.set(1, c.get(1) + 1);
            else continue;
        }
        return c;
    }

Diagonal Difference

  • 절대값 Math.abs
public static int diagonalDifference(List<List<Integer>> arr) {
    // Write your code here
        int diff = 0;
        int left = 0;
        int right = 0;
        for(int i = 0; i < arr.size(); i++)
        {
            left += arr.get(i).get(i);
            right += arr.get(i).get(arr.size() - i - 1);
        }
        diff = Math.abs(left - right);
        return diff;
    }

Mini Max Sum

  • ArrayList 선언 방법 다시 보기 !!
public static void miniMaxSum(List<Integer> arr) {
    // Write your code here
        List<Long> c = new ArrayList<>();
        long sum = 0;
        for(int i = 0; i < arr.size(); i++)
        {
            sum += arr.get(i);
        }
        for(int i = 0; i < arr.size(); i++)
        {
            c.add(sum - arr.get(i));
        }
        long min = c.get(0);
        long max = c.get(0);
        for(int i = 0; i < arr.size(); i++)
        {
            if(min >= c.get(i)) min = c.get(i);
            if(max <= c.get(i)) max = c.get(i);
        }
        System.out.println(min + " " + max);
    }

Plus Minus

  • double 타입 사용
public static void plusMinus(List<Integer> arr) {
    // Write your code here
        int count = arr.size();
        double positive = 0;
        double negative = 0;
        double zeros = 0;
        
        for (int i = 0; i < count; i++) {
            if (arr.get(i) > 0) {
                positive ++;
            } else if (arr.get(i) < 0) {
                negative ++;
            } else {
                zeros ++;
            }
        }
 
        System.out.println(positive / (double) count);
        System.out.println(negative / (double) count);
        System.out.println(zeros / (double) count);
    }

StairCase

public static void staircase(int n) {
    // Write your code here
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if ((i + j) > n) {
                    System.out.print("#");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

Time Conversion

  • 자바 문자열함수 기억하기
  • Integer.parseInt 문자열을 int로 형식 변환
/*
12:01:00PM
Return '12:01:00'.
12:01:00AM
Return '00:01:00'.
*/

public static String timeConversion(String s) {
    // Write your code here
        int hour = Integer.parseInt(s.substring(0, 2)) % 12;
        if (s.endsWith("PM"))
            hour += 12;
        return String.format("%02d", hour) + s.substring(2, 8);
    }

https://www.hackerrank.com/challenges/grading?isFullScreen=true 

 

Grading Students | HackerRank

Round student grades according to Sam's rules.

www.hackerrank.com

  • ArrayList 복사해 사용하기
public static List<Integer> gradingStudents(List<Integer> grades) {
    // Write your code here
        List<Integer> r_grade = new ArrayList<>(grades);
        for(int i = 0; i < grades.size(); i++)
        {
            if(5 * ((int)(grades.get(i) / 5) + 1) - grades.get(i) < 3){
                r_grade.set(i, 5 * ((int)(grades.get(i) / 5) + 1));
            }
            else if(5 * ((int)(grades.get(i) / 5) + 1) - grades.get(i) >= 3){
                continue;
            }
            if(grades.get(i) < 38) r_grade.set(i, grades.get(i));
        }
        return r_grade;
    }

 

728x90