Problem Solving with Algorithms

반응형

나는 최근에 이상한 일을 하고 있다. 예전의 나와 경쟁...

 

오늘은 2019년 2월 10일 나와의 경쟁.

 

leetcode.com/contest/weekly-contest-123

 

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

 

 

class Solution {
	    public List<Integer> addToArrayForm(int[] A, int K) {
	        int n = A.length;
	        int c = K;
	        for(int i = A.length-1;i >= 0;i--){
	        	A[i] += c;
	        	c = A[i] / 10;
	        	A[i] %= 10;
	        }
	        List<Integer> ret = new ArrayList<>();
	        while(c > 0){
	        	ret.add(0, c%10);
	        	c /= 10;
	        }
	        for(int v : A)ret.add(v);
	        return ret;
	    }
	}	

 

 

 

 

 

똑같군 ㅋㅋ

 

인간적으로 이문제 어려움 ㅠ

 

leetcode.com/contest/weekly-contest-123/problems/satisfiability-of-equality-equations/

 

Account Login - 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 boolean equationsPossible(String[] equations) {
	        DJSet ds = new DJSet(26);
	        for(String e : equations){
	        	if(e.charAt(1) == '='){
	        		int l = e.charAt(0)-'a';
	        		int r = e.charAt(3)-'a';
	        		ds.union(l, r);
	        	}
	        }
	        for(String e : equations){
	        	if(e.charAt(1) == '!'){
	        		int l = e.charAt(0)-'a';
	        		int r = e.charAt(3)-'a';
	        		if(ds.equiv(l, r)){
	        			return false;
	        		}
	        	}
	        }
	        return true;
	    }
	    
	    public class DJSet {
			public int[] upper;

			public DJSet(int n) {
				upper = new int[n];
				Arrays.fill(upper, -1);
			}

			public int root(int x) {
				return upper[x] < 0 ? x : (upper[x] = root(upper[x]));
			}

			public boolean equiv(int x, int y) {
				return root(x) == root(y);
			}

			public boolean union(int x, int y) {
				x = root(x);
				y = root(y);
				if (x != y) {
					if (upper[y] < upper[x]) {
						int d = x;
						x = y;
						y = d;
					}
					upper[x] += upper[y];
					upper[y] = x;
				}
				return x == y;
			}

			public int count() {
				int ct = 0;
				for (int u : upper)
					if (u < 0)
						ct++;
				return ct;
			}
		}

	}
반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band