Problem Solving with Algorithms

반응형

leetcode.com/contest/weekly-contest-218

 

Contest - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

1번

leetcode.com/problems/goal-parser-interpretation/

 

Goal Parser Interpretation - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 string 과 stringbuilder를 쓰는것의 차이.

class Solution {
    public String interpret(String command) {
        StringBuilder sb = new StringBuilder();
        int cur = 0;
        while (cur < command.length()) {
            if (command.charAt(cur) == 'G') {
                sb.append('G');
                cur++;
            } else if (command.charAt(cur+1) == ')') {
                sb.append('o');
                cur+=2;
            } else {
                sb.append("al");
                cur+=4;
            }
        }
        return sb.toString();
    }
}

 

 

 

 

 

2번

leetcode.com/problems/max-number-of-k-sum-pairs/

 

Max Number of K-Sum Pairs - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution {
    public int maxOperations(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            int current = nums[i];
            int complement = k - current;
            if (map.getOrDefault(complement, 0) > 0) {
                // remove complement from the map
                map.put(complement, map.get(complement) - 1);
                count++;
            } else {
                 // add current element in the map
                map.put(current, map.getOrDefault(current, 0) + 1);
            }
        }
        return count;
    }
}

 

 

이제 내 코드들은 솔루션과 거의 똑같아졌다.

complement 이런 수학적인 변수명도 좀 더 반영하자.

 

 

 

 

3번

leetcode.com/problems/concatenation-of-consecutive-binary-numbers/

 

Concatenation of Consecutive Binary Numbers - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

% MOD 직전에 숫자가 커지므로 일단 long으로 유지하여야한다.

class Solution {
    public int concatenatedBinary(int n) {
        final int MOD = 1000000007;
        int length = 0; // bit length of addends
        long result = 0; // long accumulator
        for (int i = 1; i <= n; i++) {
            // when meets power of 2, increase the bit length
            if ((i & (i - 1)) == 0) {
                length++;
            }
            result = ((result << length) | i) % MOD;
        }
        return (int) result;
    }
}

 

 

 

 

4번

HARD는 아직..

leetcode.com/problems/minimum-incompatibility/

 

Minimum Incompatibility - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band