资讯专栏INFORMATION COLUMN

AO Rettiwt

Zoom / 1941人阅读

摘要:

Ways to complete Kraken Problem

Kraken is m*n grids on a rectangular board. From the top left to reach the bottom right corner while moving one grid at a time in either the down, right or down-right diagonal directions

Solution
public class Solution {
    public int krakenCount(int m, int n) {
        if (m == 0 || n == 0) return 0;
        if (m == 1 || n == 1) return 1;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) dp[i][0] = 1;
        for (int j = 0; j < n; j++) dp[0][j] = 1;
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = dp[i-1][j] + dp[i][j-1] + dp[i-1][j-1];
            }
        }
        return dp[m-1][n-1];
    }
}
Minimum Genetic Mutation Solution

Backtracking

public class Solution {
    public int minMutation(String start, String end, String[] bank) {
        Queue q = new LinkedList<>();
        q.offer(start);
        int count = 0;
        char[] genes = new char[]{"A","C","G","T"};
        Set set = new HashSet<>();
        for (String s: bank) set.add(s);
        while (!q.isEmpty()) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                String pre = q.poll();
                for (int j = 0; j < pre.length(); j++) {
                    for (char gene: genes) {
                        StringBuilder sb = new StringBuilder(pre);
                        sb.setCharAt(j, gene);
                        String cur = sb.toString();
                        if (end.equals(cur) && set.contains(end)) return count+1;
                        else if (set.contains(cur)) {
                            set.remove(cur);
                            q.offer(cur);
                        }
                    }
                }
            }
            count++;
        }
        return -1;
    }
}

BFS

public class Solution {
    public int minMutation(String start, String end, String[] bank) {
        if (start == null || end == null || bank == null || bank.length == 0 || start.length() != end.length()) return -1;
        return helper(start, end, bank, new ArrayList(), 0);
    }
    public int helper(String start, String end, String[] bank, List path, int count) {
        int min = -1;
        if (start.equals(end)) return 0;
        if (count >= end.length()) return min;
        for (String gene: bank) {
            if (!path.contains(gene) && isNext(start, gene)) {
                path.add(gene);
                int res = helper(gene, end, bank, path, count++);
                if (res != -1) min = Math.min(Integer.MAX_VALUE, res+1);
                path.remove(gene);
            }
        }
        return min;
    }
    public boolean isNext(String s1, String s2) {
        if (s1 == null || s2 == null || s1.length() != s2.length()) return false;
        int diff = 0;
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i) && ++diff > 1) return false;
        }
        return true;
    }
}
Longest Phrases in a Tweet Maximum Size Subarray Sum equals or less than K Using Queue
public class Solution {
    public int maxSubArrayLen(int[] nums, int k) {
        if (nums == null || nums.length == 0) return 0;
        Queue q = new LinkedList<>();
        int sum = 0, max = 0;
        for (int num: nums) {
            if (sum+num <= k) {
                q.offer(num);
                sum+=num;
            }
            else {
                while (sum+num > k) {
                    max = Math.max(max, q.size());
                    int pre = q.poll();
                    sum-=pre;
                }
                sum+=num;
                q.offer(num);
                max = Math.max(max, q.size());
            }
        }
        max = Math.max(max, q.size());
        return max;
    }
}
Brute Force
public class Solution {
    public int maxSubArrayLen(int[] nums, int k) {
        if (nums == null || nums.length == 0) return 0;
        int len = nums.length;
        int[] sum = new int[len+1];
        sum[0] = 0;
        for (int i = 1; i <= len; i++) {
            sum[i] = sum[i-1]+nums[i-1];
        }
        int max = 0;
        for (int i = 0; i < len; i++) {
            //if (sum[i] <= k) max = Math.max(max, i);
            for (int j = i+1; j <= len; j++) {
                if (sum[j]-sum[i] <= k) max = Math.max(max, j-i);
            }
        }
        return max;
    }
}
Information Masking Example

(111)222-3456 --> --3456
+123(444)555-6789 --> +--*-6789
333 444 5678 --> --5678
(333)444-5678 --> --5678

jackandrose@gmail .com --> je@gmail .com

Solution
public class Solution {
    public static String emailMask(String email) {
        StringBuilder sb = new StringBuilder();
        sb.append("E:");
        sb.append(email.charAt(0));
        sb.append("*****");
        int lastIndex = email.lastIndexOf("@")-1;
        sb.append(email.substring(lastIndex));
        return sb.toString();
    }
    public static String phoneMask(String phone) {
        StringBuilder sb = new StringBuilder();
        sb.append("P:");
        boolean hasCode = false;
        if (phone.charAt(0) == "+") {
            hasCode = true;
            phone = phone.substring(1);
        }
        if (phone.charAt(0) == "(") phone = phone.substring(1);
        String delimiters = "D";
        String[] nums = phone.trim().split(delimiter);
        if (hasCode) sb.append("+");
        int n = nums.length;
        for (int i = 0; i < n-1; i++) {
            int len = nums[i].length();
            for (int i = 0; i < len; i++) sb.append("*");
            sb.append("-");
        }
        sb.append(nums[n-1]);
        return sb.toString();
    }
    public static void main(String args[] ) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input;
        while ((input = br.readLine()) != null){
            //String input = br.readLine();
            String[] inputs = input.trim().split(":");
            if (inputs[0].trim().equals("E")) System.out.println(emailMask(inputs[1].trim()));
            else if (inputs[0].trim().equals("P")) System.out.println(phoneMask(inputs[1].trim()));
        }
        br.close();
    }
}
First Unique Character in a String
public class Solution {
    public int firstUniqChar(String s) {
        if (s == null || s.length() == 0) return -1;
        int[] dict = new int[26];
        for (int i = 0; i < s.length(); i++) {
            dict[s.charAt(i)-"a"]++;
        }
        for (int i = 0; i < s.length(); i++) {
            if (dict[s.charAt(i)-"a"] == 1) return i;
        }
        return -1;
    }
}
Tweet Recommendation Hacking Time Apache Log Success Rates Evaluate Expression Tree Timeseries Data Aggregation SQL Student/Department
SELECT d.DEPT_NAME, COUNT(s.STUDENT_ID) as STUDENT_COUNT
FROM Departments (AS) d
LEFT JOIN Students (AS) s on d.DEPT_ID = s.DEPT_ID
GROUP BY d.DEPT_ID
ORDER BY STUDENT_COUNT DESC, d.DEPT_NAME;
ORDERS
SELECT o.customerNumber AS customer
FROM ORDERS AS o
GROUP BY customerNumber
ORDER BY count(orderNumber) DESC
limit 1;

OR

SELECT customerNumber 
FROM ORDERS
WHERE ROWNUM <= 1
GROUP BY customerNumber
ORDER BY COUNT(orderNumber) DESC;
Investments in 2012
SELECT ROUND(SUM(TIV_2012), 2)
FROM Insurance
WHERE Insurance.PID IN
(SELECT PID
FROM Insurance I1, Insurance I2
WHERE I1.TIV_2011 = I2.TIV_2011 AND I1.PID != I2.PID)
AND Insurance.PID NOT IN
(SELECT I1.PID
FROM Insurance I1, Insurance I2
WHERE I1.LAT = I2.LAT AND I1.LON = I2.LON AND I1.PID != I2.PID);
Employee/Department
SELECT d.Name, COUNT(e.ID) AS ID_COUNT
FROM Department (AS) d
LEFT JOIN Employee (AS) e
ON(WHERE) e.DEPT_ID = d.DEPT_ID
GROUP BY d.Name
ORDER BY ID_COUNT DESC, NAME
Parent/Child/Tree
SELECT Id, CASE
WHEN M IS NULL THEN "Leaf"
WHEN P_id IS NULL THEN "Root"
ELSE "Inner"
END AS TypeNode
FROM
(
SELECT DISTINCT hijo.*, padre.P_id AS M
FROM Tree hijo
LEFT JOIN Tree padre
ON (hijo.Id = padre.P_id)
)
ORDER BY Id;

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69783.html

相关文章

  • 作用域链

    摘要:作用域链作用域就近原则在写下声明就能确定的,叫做词法作用域词法作用域可以确定是哪个,但不能确定的值关于作用域链,浏览器内部究竟发生了什么例子声明前置调用调用例子声明前置调用调用例子声明前置调用调用例子,,,,,,声明前置调用调用调用 作用域链 作用域:就近原则在写下声明就能确定的,叫做词法作用域 var a = 1 function bar(){ var a = 2 conso...

    zgbgx 评论0 收藏0
  • JavaScript-变量提升(AO、GO)编译、闭包

    摘要:是一个解释型语言上明确的说,是一个轻量级的解释型的面向对象的将函数视为一级公民的语言。全局代码在执行的时候,先是变量提升,在全局作用域内添加属性,然后是函数以函数声明创建的函数提升,再是代码执行。那么,很显然,闭包其实就是一个函数。 JavaScript是一个解释型语言 MDN上明确的说,JavaScript是一个轻量级的、解释型的、面向对象的、将函数视为一级公民的语言。 那么,既然j...

    plokmju88 评论0 收藏0
  • JavaScript的预编译过程与作用域

    摘要:词法作用域是一种静态作用域这个例子的结果按静态作用域来分析执行函数,先从函数内部查找是否有局部变量,如果没有,就根据书写的位置,查找上面一层的代码,也就是等于,所以结果会打印。静态作用域,决定的是作用域链的顺序。 博客原文地址:https://finget.github.io/2018/03/01/javascriptPrecompile/看不明白的地方欢迎提问,有理解的不对的地方希望...

    ziwenxie 评论0 收藏0
  • js 词法分析,词法作用域

    摘要:引擎会在代码执行前进行词法分析,所以事实上,运行分为此法分析和执行两个阶段。词法作用域所谓词法作用域是说,其作用域为在定义时词法分析时就确定下来的,而并非在执行时确定。 先来看个常见的面试题如下: var a = 10; function test(){ alert(a); //undefined var a = 20; alert(a); //20 } te...

    2450184176 评论0 收藏0

发表评论

0条评论

Zoom

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<