오늘의 문제 _ 121. Best Time to Buy and Sell Stock(LeetCode)

나의 풀이

class Solution {  
  
    public int maxProfit(int[] prices) {  
        int buy = Integer.MAX_VALUE;  
        int sell = -1;  
        for (int price : prices) {  
            buy = Math.min(price, buy);  
            sell = Math.max(sell, price - buy);  
        }  
        return sell;  
    }  
}

'TIL' 카테고리의 다른 글

TIL _ 동적계획법 _ 0831 _ SAT  (0) 2024.08.31
TIL _ 동적계획법 _ 0830 _ FRI  (0) 2024.08.31
TIL _ 탐욕법(Greedy) _ 0829 _ THU  (0) 2024.08.30
TIL _ 탐욕법(Greedy) _ 0828 _ WED  (0) 2024.08.29
TIL _ 완전탐색 _ 0827 _ TUE  (0) 2024.08.28

오늘의 문제 _ 1137. N-th Tribonacci Number(LeetCode)

나의 풀이

class Solution {
    int memo[];
    public int tribonacci(int n) {
        memo = new int[38];
        Arrays.fill(memo, -1);
        memo[0] = 0;
        memo[1] = 1;
        memo[2] = 1;
        return helper(n);
    }
    public int helper(int n){
        if(memo[n] > -1)return memo[n];
        for(int i = 1 ;i < 4 ;i++)
            memo[n-i] = helper(n-i);
        return memo[n] = memo[n-1] + memo[n-2] + memo[n-3];
    }
}

'TIL' 카테고리의 다른 글

TIL _ 동적계획법 _ 0901 _ SUN  (0) 2024.09.01
TIL _ 동적계획법 _ 0830 _ FRI  (0) 2024.08.31
TIL _ 탐욕법(Greedy) _ 0829 _ THU  (0) 2024.08.30
TIL _ 탐욕법(Greedy) _ 0828 _ WED  (0) 2024.08.29
TIL _ 완전탐색 _ 0827 _ TUE  (0) 2024.08.28

오늘의 문제 _ 746. Min Cost Climbing Stairs(LeetCode)

나의 풀이

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int N = cost.length;

        for (int i = 2; i < N; i++){
            cost[i] = Math.min(cost[i-2], cost[i-1]) + cost[i];
        }
        return Math.min(cost[N-1], cost[N-2]);
    }
}

 

'TIL' 카테고리의 다른 글

TIL _ 동적계획법 _ 0901 _ SUN  (0) 2024.09.01
TIL _ 동적계획법 _ 0831 _ SAT  (0) 2024.08.31
TIL _ 탐욕법(Greedy) _ 0829 _ THU  (0) 2024.08.30
TIL _ 탐욕법(Greedy) _ 0828 _ WED  (0) 2024.08.29
TIL _ 완전탐색 _ 0827 _ TUE  (0) 2024.08.28

+ Recent posts