Thursday, August 29, 2019

605. can-place-flowers https://leetcode.com/problems/can-place-flowers/

605. can-place-flowers
  • 정방향 탐색하면서 앞뒤가 0이면 1로 변경하고 n–해준다.
  • n <= 작으면 return true
Runtime: 1 ms, faster than 100.00% of Java online submissions for Can Place Flowers.
Memory Usage: 38.6 MB, less than 100.00% of Java online submissions for Can Place Flowers.
class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        if(flowerbed.length ==0){
            return false;
        }
        if(flowerbed.length==1){
            if(flowerbed[0] == 0){
                if(n <=1){
                    return true;
                }else{
                    return false;
                }
            }else{
                return n==0;
            }
        }

        for( int i=0; i<flowerbed.length; i++){
            if(flowerbed[i]==1){
                continue;
            }
            if(i ==0){
                if(flowerbed[i+1]==0){
                    n--;
                    flowerbed[i]=1;
                }
            }else if(i == flowerbed.length-1){
                if(flowerbed[i-1] == 0){
                    flowerbed[i] =1;
                    n--;
                }
            }else{
                if(flowerbed[i-1] == 0 && flowerbed[i+1]==0){
                    flowerbed[i] =1;
                    n--;
                }
            }
        }
        return n<=0;
    }
}

Wednesday, August 28, 2019

665. non-decreasing-array

665. non-decreasing-array
  • Runtime: 1 ms, faster than 99.48% of Java online submissions for Non-decreasing Array.
  • 이 문제 조금 어렵네요
  • for문 한번돌리려고 했는데 너무 로직이 복잡해서 2번돌리는걸로
class Solution {
    public boolean checkPossibility(int[] nums) {
       if(nums.length <= 2){
            return true;
        }

        int count =0;
        boolean pre = true;
        boolean after = true;
        for(int i=0; i<nums.length-1; i++){
            if(nums[i] <= nums[i+1]){

            }else{
                if(count >0){
                    pre =  false;
                    break;
                }
                count++;
                if(i >0){
                    if(nums[i-1] >nums[i+1]){
                        pre= false;
                        break;
                    }
                }
            }
        }
        count =0;
        for(int i=0; i<nums.length-1; i++){
            if(nums[i] <= nums[i+1]){

            }else{
                if(count >0){
                    after =  false;
                    break;
                }
                count++;
                if(i+2 <nums.length){
                    if(nums[i] >nums[i+2]){
                        after= false;
                        break;
                    }
                }
                i++;
            }
        }

        return (pre || after);
    }
}
  • 윗코드 리팩토링
public class Solution {
    public boolean checkPossibility(int[] nums) {

        if(nums.length <= 2){
            return true;
        }

        int count =0;
        for(int i=0; i<nums.length-1; i++){
            if(nums[i] > nums[i+1]){
                if(count++ >0)    return false;
                if(i >0){
                    if(nums[i-1] <=nums[i+1]){
                        nums[i-1] = nums[i];
                        continue;
                    }
                }
                if(i+2 <nums.length){
                    if(nums[i]<=nums[i+2]){
                        nums[i+1] = nums[i];
                        continue;
                    }else{
                        if(i>0){
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
}