알고리즘 공부 2020. 8. 24. 23:13

https://leetcode.com/problems/word-break-ii/

 

Word Break II - 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

/**
 * @param {string} s
 * @param {string[]} wordDict
 * @return {boolean}
 */
var wordBreak = function(s, wordDict) {
    var res=[];
    var map=[];
    for( var i=0; i<s.length+1; ++i )     map[i] = 0;
    
    var find = function( p_str, p_arr )
    {
        if( s.indexOf(p_str) != 0)      
            return false;  
        
        if( p_str.length > s.length )   
            return false;        
        else
        {
            if( p_str == s )
            {
                res.push(p_arr.join(' '));            
                return true;
            }
            else
            {            
                var refstr = '';
                for( var i=0; i<wordDict.length; ++i )
                {
                    refstr = p_str+wordDict[i];                   
                    
                    if( s.indexOf(refstr) >= 0 )
                    {
                        if( map[refstr] == 1 )  return false;
                        result = find( refstr, p_arr.concat([wordDict[i]]) );                        
                        if( result === false )               map[refstr] = 1;
                    }
                }
                
                return !(res.length==0);
            }
        }           
    }    
    find('',[]);
    return res;
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 24. 23:12

https://leetcode.com/problems/word-break/

 

Word Break - 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

/**
 * @param {string} s
 * @param {string[]} wordDict
 * @return {boolean}
 */
var wordBreak = function(s, wordDict) {
    var res=false;
    var map=[];
    for( var i=0; i<s.length; ++i )     map[i] = 0;
    
    var find = function( p_str )
    {
        if( s.indexOf(p_str) != 0)      return false;        
        if( p_str.length > s.length )   return false;        
        
        if( p_str == s )
        {
            res=true;
            return true;
        }
        else
        {
            var result=''
            for( var i=0; i<wordDict.length; ++i )
            {
                if( s.indexOf(p_str+wordDict[i]) >= 0 )
                {                    
                    if( map[p_str+wordDict[i]] == 1 )   return false;
                    else                                result = find( p_str+wordDict[i] );

                    if( result == false )               map[p_str+wordDict[i]] = 1;
                    
                    if( result==true)                   return true;
                    else                                res=false;
                }
            }
            return res;
        }        
    }    
    find('');
    return res;
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 24. 23:08

https://leetcode.com/problems/generate-parentheses/

 

Generate Parentheses - 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

/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function(n) {    
    const res = [];
    const find = function( p_open, p_close, p_str )
    {   
        if( p_open < n )    
            find( p_open+1, p_close, p_str.concat('(') );

        if( p_close < p_open  )
            find( p_open, p_close+1, p_str.concat(')') );

        if( !(p_open - n) && !(p_open - p_close) )        
            res.push( p_str );
    }
    
    find( 0, 0, '' );
    return res;
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 24. 23:07

https://leetcode.com/problems/count-and-say/

 

Count and Say - 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

/**
 * @param {number} n
 * @return {string}
 */
const hash = {};
hash[0] = '';
hash[1] = '1';
hash[2] = '11';
hash[3] = '21';
hash[4] = '1211';
hash[5] = '111221';

const  say = function(p_num)
{   
    const l_number = p_num.toString();
    var res = "";
        

    var cnt     = 1;
    var before  = l_number.charAt(0);
    var elem    = '';

    
    for( var i=1; i<l_number.length; ++i )
    {
        elem = l_number.charAt(i);
        if( elem == before )
            ++cnt;
        else
        {
            res += cnt + '' + before;
            before  = elem;
            cnt     = 1;
        }
    }

    res += cnt.toString() + before.toString();
    return res;
}

var countAndSay = function(n) {    
    const find = function(p_num)
    {
        return hash[p_num] ? hash[p_num]:( hash[p_num] = say( find(Number(p_num)-1) ) );        
    }    
    
    return find(n);
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 20. 22:40

https://leetcode.com/problems/subsets/

 

Subsets - 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

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function(nums) {
    var res=[];
    var length = nums.length;
    var find = function( p_idx, p_arr )
    {
        if( p_idx >= length )
        {            
            res.push(p_arr);
        }        
        else
        {   
            find( p_idx+1, p_arr.concat( nums[p_idx] ) );
            find( p_idx+1, p_arr );            
        }
        return ;
    }
    
    find(0,[]);
    return res;
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 20. 22:38

https://leetcode.com/problems/letter-combinations-of-a-phone-number/

 

Letter Combinations of a Phone Number - 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

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function(digits) {
    if( digits === "" )
        return [];
    
    const num_string = { '2':'abc', '3':'def', '4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz' };
    
    const res = [];    
    const find = function( p_num_idx, p_str )
    {        
        if( p_str.length === digits.length )        
            res.push( p_str )        
        else
        {
            const str = num_string[ digits[p_num_idx] ];
            for( let i=0; i<str.length; ++i )            
                find( p_num_idx+1, p_str.concat(str[i]) );            
        }
    }
    find(0,'');
    
    return res;
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 20. 22:37

https://leetcode.com/problems/n-queens-ii/

 

N-Queens II - 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

/**
 * @param {number} n
 * @return {number}
 */
var totalNQueens = function(n) {
    var map = Array(n).fill(-1);
    var res = 0;
    
    var colhash = Array(n).fill(0);   
    
    var chk = function()
    {        
        for( var i=0; i<n; ++i )
        {            
            for( var j=1; j<n-i; ++j )
            {                
                // 우하
                if( i+j < n )
                {
                    if( map[i]+j == map[i+j] || map[i]-j == map[i+j] )
                        return false;
                }
                
                // 좌하
                if( i-j > 0 )
                {
                    if( map[i]+j == map[i-j] || map[i]-j == map[i-j] )
                        return false;
                }
            }            
        }        
       
        return true;
    }
    
    var find = function( p_idx )
    {
        if( p_idx == n )
        {            
            if( chk() )     ++res;
        }
        else
        {
            for( var i=0; i<n; ++i )
            {
                if( !colhash[i] )
                {
                    colhash[i] = 1;

                    map[p_idx]= i;                
                    if( p_idx+1 <= n )      find(p_idx+1);                
                    map[p_idx]= -1;

                    colhash[i] = 0;
                }            
            }
        }        
    }
    
    find(0);    
    
    return res;
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 20. 22:36

https://leetcode.com/problems/n-queens/

 

N-Queens - 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

/**
 * @param {number} n
 * @return {string[][]}
 */
var solveNQueens = function(n) {
    var map = Array(n).fill(-1);
    var res = [];
    //var hash= {};
    
    var colhash = Array(n).fill(0);
    
    var save_map = function()
    {
        var tmp = [];
        for( var i=0; i<n; ++i )
        {
            var Qpos = map[i];
            var row = [];
            for( var j=0; j<n; ++j )
            {
                if( j == Qpos)      row.push("Q");
                else                row.push(".");
            }
            tmp.push(row.join(''));
        }
        res.push(tmp);
    }
    
    var chk = function()
    {        
        for( var i=0; i<n; ++i )
        {            
            for( var j=1; j<n-i; ++j )
            {                
                // 우하                
                {
                    if( map[i]+j == map[i+j] || map[i]-j == map[i+j] )
                        return false;
                }                
                
                // 좌하
                if( i-j > 0 )
                {
                    if( map[i]+j == map[i-j] || map[i]-j == map[i-j] )
                        return false;
                }
            }            
        }        
       
        return true;
    }
    
    var find = function( p_idx )
    {
        if( p_idx == n )
        {            
            if( chk() )     save_map();              
        }
        else
        {
            for( var i=0; i<n; ++i )
            {
                if( !colhash[i] )
                {
                    colhash[i] = 1;

                    map[p_idx]= i;                
                    if( p_idx+1 <= n )      find(p_idx+1);                
                    map[p_idx]= -1;

                    colhash[i] = 0;
                }            
            }
        }        
    }
    
    find(0);
    
    
    return res;
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 20. 22:34

https://leetcode.com/problems/jump-game-iii/

 

Jump Game III - 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

/**
 * @param {number[]} arr
 * @param {number} start
 * @return {boolean}
 */
var canReach = function(arr, start) {
    const hash = Array(arr.length).fill(false);
    
    const find = function( p_idx )
    {        
        if( p_idx < 0 || p_idx >= arr.length )  return false;        
        if( !arr[p_idx] )                       return true;                
                        
        if( !hash[p_idx] )
        {
            hash[p_idx] = true;
            return find( p_idx + arr[p_idx] ) || find( p_idx - arr[p_idx] );                
        }
        return false;
                
    }
    return find(start);
};
posted by Sense.J
:
알고리즘 공부 2020. 8. 20. 22:33

https://leetcode.com/problems/deepest-leaves-sum/

 

Deepest Leaves Sum - 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

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var deepestLeavesSum = function(root) {
    const sums = Array(Math.pow(10,3)).fill(0);
    let maxdepth = 0;
    const find = function( p_root, p_depth )
    {
        if( p_root )
        {
            sums[p_depth] += p_root.val;            
            
            find( p_root.left, p_depth+1)
            find( p_root.right, p_depth+1)
        }
        else
        {
            maxdepth = Math.max( maxdepth, p_depth-1 );
        }
    }
    find( root, 1 );
    
    return sums[maxdepth];
};
posted by Sense.J
: