楼主: sicilianee
跳转到指定楼层
上一主题 下一主题
收起左侧

记录

🔗
 楼主| sicilianee 2017-9-22 14:43:38 | 只看该作者
全局:
0918 2.  2 Keys Keyboard

/**
* @param {number} n
* @return {number}
*/
var minSteps = function(n) {
    if (n === 1) {return 0;}
    let min = n;
    for (let i = n - 1; i > 1; i--) {
        if (n % i === 0) {
            min = Math.min(minSteps(i) + minSteps(n / i), min)
        }
    }
    return min;
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-23 12:29:12 | 只看该作者
全局:
0919 1. Random Pick Index

class Solution {
    int[] nums;

    public Solution(int[] nums) {
        this.nums = nums;
    }
   
    public int pick(int target) {
        int count = 0;
        int index = 0;
        for (int i = 0; i < this.nums.length; i++) {
            int num = this.nums[i];
            if (num == target) {
                count++;
                int random = (int) (Math.random() * count);
                if (random == 0) {
                    index = i;
                }
            }
        }
        return index;
    }
}

/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(nums);
* int param_1 = obj.pick(target);
*/
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-23 15:07:12 | 只看该作者
全局:
0919 2. Burst Balloons

/**
* @param {number[]} nums
* @return {number}
*/
var maxCoins = function(nums) {
    const len = nums.length;
    nums = [1, ...nums, 1];
    const dp = Array(len + 2).fill().map(() => Array(len + 2).fill(0));
    for (let range = 1; range <= len; range++) {
        for (let i = 1; i + range - 1 <= len; i++) {
            let j = i + range - 1;
            if (range === 1) {
                dp[i][j] = nums[i] * nums[i - 1] * nums[i + 1];
            } else {
                for (let k = i; k <= j; k++) {
                    const value = nums[k] * nums[i - 1] * nums[j + 1] + dp[i][k - 1] + dp[k + 1][j];
                    dp[i][j] = Math.max(dp[i][j], value);
                }
            }
        }
    }
    return dp[1][len];
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-24 01:43:15 | 只看该作者
全局:
0920 1.  Second Minimum Node In a Binary Tree

/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var findSecondMinimumValue = function(root) {
    if (root == null) {return -1;}
    let second = Number.POSITIVE_INFINITY;
    recurse(root);
    return second === Number.POSITIVE_INFINITY ? -1 : second;
   
    function recurse (node) {
        if (node == null) {
            return;
        }
        if (node.val > root.val) {
            second = Math.min(second, node.val);
        }
        recurse(node.left);
        recurse(node.right);
    }
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-24 02:52:01 | 只看该作者
全局:
0920 2. Shopping Offers

/**
* @param {number[]} price
* @param {number[][]} special
* @param {number[]} needs
* @return {number}
*/
var shoppingOffers = function(price, special, needs) {
    let wePay = 0;
    for (let i = 0; i < needs.length; i++) {
        wePay += needs[i] * price[i];
    }
    const savings = special.map(specialSet => {
        // const [...nums, specialPrice] = specialSet;  // Rest must be the last in an array
        const nums = specialSet.slice(0, specialSet.length - 1);
        const specialPrice = specialSet[specialSet.length - 1];
        let originalPrice = 0;
        for (let i = 0; i < nums.length; i++) {
            originalPrice += nums[i] * price[i];
        }
        return originalPrice - specialPrice;
    });
    recurse(wePay, 0);
    return wePay;
   
   
    function recurse (pay, i) {
        for (let j = i; j < special.length; j++) {
            const aSpecial = special[j];
            const good = needs.every((need, index) => need >= aSpecial[index]);
            if (good) {
                for (let k = 0; k < needs.length; k++) {
                    needs[k] = needs[k] - aSpecial[k];
                }
                pay = pay - savings[j];
                wePay = Math.min(pay, wePay);
                recurse(pay, i);
                pay = pay + savings[j];
                for (let k = 0; k < needs.length; k++) {
                    needs[k] = needs[k] + aSpecial[k];
                }
            }
        }
    }
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-24 02:59:44 | 只看该作者
全局:
sicilianee 发表于 2017-9-24 02:52
0920 2. Shopping Offers

/**

// I saw people using the needs list as key to memorize, maybe you could turn it into dp?

Using the function itself to recurse. With this needs, return the min price you will pay. Other input won't change.
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-24 03:17:14 | 只看该作者
全局:
0921 1.  Longest Word in Dictionary through Deleting

/**
* @param {string} s
* @param {string[]} d
* @return {string}
*/
var findLongestWord = function(s, d) {
    let word = ''
    d.forEach(aWord => {
        if (validate(s, aWord)) {
            if (aWord.length > word.length || (aWord.length === word.length && aWord < word)) {
                word = aWord;
            }
        }
    });
    return word;
};

function validate (str, word) {
    let i = 0;
    let j = 0;
    while (i < word.length && j < str.length) {
        if (word[i] === str[j]) {
            i++;
            j++;
        } else {
            j++;
        }
    }
    return i === word.length
}
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-24 03:20:00 | 只看该作者
全局:
sicilianee 发表于 2017-9-24 03:17
0921 1.  Longest Word in Dictionary through Deleting

/**

Only one pointer i is enough. No need for maintaining j in a while loop fashion since j always increase 1.
Just loop chars of the str and update i in the word.
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-24 03:32:24 | 只看该作者
全局:
0921 2. Bulb Switcher

/**
* @param {number} n
* @return {number}
*/
var bulbSwitch = function(n) {
    return Math.floor(Math.sqrt(n));
};
回复

使用道具 举报

🔗
 楼主| sicilianee 2017-9-24 06:47:17 | 只看该作者
全局:
0922 1. Longest Palindromic Subsequence

/**
* @param {string} s
* @return {number}
*/
var longestPalindromeSubseq = function(s) {
    const len = s.length;
    const dp = Array(len).fill().map(() => Array(len).fill(0));
    for (let step = 1; step <= len; step++) {
        for (let i = 0; i + step - 1 < len; i++) {
            let j = i + step - 1;
            if (step === 1) {
                dp[i][j] = 1;
                continue; // !!!! You either continue or put others in an else statement
            }
            if (s[i] === s[j]) {
                dp[i][j] = dp[i + 1][j - 1] + 2;
            } else {
                dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
            }
        }
    }
    return dp[0][len - 1];
};
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表