312.Burst Balloons

https://leetcode.com/problems/burst-balloons/
DP 구 해http://blog.csdn.net/swartz2015/article/details/50561199
class Solution {  
public:  
    int maxCoins(vector& nums) {  
        int arr[nums.size()+2];  
          
        for(int i=1;i

dfs 폭력 구 해:
class Solution {
    vector visited;
    int res;
    int n;
    
    int findLeft(int i){
        int j = i - 1;
        while(j >= 0 && visited[j]) j--;
        return j;
    }
    
    int findRight(int i){
        int j = i + 1;
        while(j < n && visited[j]) j++;
        return j;
    }
    
    void helper(vector& nums, int left, int coin){
        if(left == 0) {
            res = max(res, coin);
            return;
        }
        
        for(int i = 0; i < n; i++) {
            if(!visited[i]) {
                visited[i] = true;
                int li = findLeft(i);
                int ri = findRight(i);
                int l = li < 0 ? 1:nums[li];
                int r = ri >= n ? 1:nums[ri];
                int m = l*nums[i]*r;
                helper(nums, left - 1, coin + m);
                visited[i] = false;
            }
        }
    }
    
public:
    int maxCoins(vector& nums) {
        visited.resize(nums.size(),false);
        res = 0;
        n = nums.size();
        helper(nums,n,0);
        return res;
    }
};

좋은 웹페이지 즐겨찾기