https://leetcode.com/problems/construct-the-rectangle/submissions/

그냥 질문 그대로 구현했다.

좀 느릴 것 같다.

class Solution {
    public int[] constructRectangle(int area) {
        int value[] = new int[2];
        if(area == 0){
            value[0] = 0;
            value[1] = 0;
            return value;
        }
        else if(area == 1){
            value[0] = 1;
            value[1] = 1;
            return value;
        }

        int difference = area;
        int start = 1;
        int end = area;
        int a, b;
        for(int i = start; i <= end; i++){
            if(area % i == 0){
                a = i;
                b = area / i;
            
                if((a >= b)&&((a-b)<difference)){
                    difference = value[0] - value[1];
                    value[0] = a;
                    value[1] = b;
           
                }
            }
        }
        return value;
    }
}

Posted by 공놀이나하여보세
,

4. 485 : Max Consecutive Ones

https://leetcode.com/problems/max-consecutive-ones/submissions/

 

Loading...

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(object):
    def findMaxConsecutiveOnes(self, nums):
        cnt = 0
        max = 0
        
        for i in range(len(nums)):
            if nums[i] == 1:
                cnt += 1
            else:
                if max < cnt:
                    max = cnt
                cnt = 0
        
        if max < cnt:
            max = cnt
                
        return max        
        """
        :type nums: List[int]
        :rtype: int
        """

 

Posted by 공놀이나하여보세
,

3. 290 : word pattern

s = str.split(' ')

class Solution(object):
    def wordPattern(self, pattern, str):
        s = str.split(' ')
        
        if len(s) != len(pattern):
            return False
       
        for i in range(len(s)):
            for j in range(i + 1, len(s)):
                if pattern[i] == pattern[j]:
                    if s[i] != s[j]:
                        return False
                else:
                    if s[i] == s[j]:
                        return False
            
        return True
        """
        :type pattern: str
        :type str: str
        :rtype: bool
        """

 

Posted by 공놀이나하여보세
,