LeetCode Weekly Contest 32解题思路

LeetCode Weekly Contest 32解题思路

详细代码可以fork下Github上leetcode项目,不定期更新。

赛题

本次周赛主要分为以下4道题:

581 Shortest Unsorted Continuous Subarray (4分)

Problem:

Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.

You need to find the shortest such subarray and output its length.

Example 1:

Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

Note:

  • Then length of the input array is in range [1, 10,000].
  • The input array may contain duplicates, so ascending order here means <=.

我的思路:
分别找寻数组中的最大值和最小值,然后递归,分为四种情况:

a. 首元素 == 最小值 && 尾元素 == 最大值;
b. 首元素 != 最小值 && 尾元素 == 最大值;
c. 首元素 == 最小值 && 尾元素 != 最大值;
d. 首元素 != 最小值 && 尾元素 != 最大值;
public int findUnsortedSubarray(int[] nums) {
        return helper(nums, 0, nums.length-1);
    }

    private int helper(int[] nums, int start, int end){
        if (start > end){
            return 0;
        }
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int i = start; i <= end; i++){
            min = Math.min(min, nums[i]);
            max = Math.max(max, nums[i]);
        }

        if (min == nums[start] && max == nums[end]){
            return helper(nums, start+1, end-1);
        }

        if (min == nums[start]){
            return helper(nums, start+1, end);
        }

        if (max == nums[end]){
            return helper(nums, start, end-1);
        }
        return end-start+1;
    }

优化思路:
对数组进行排序,然后直接进行比较,数组排序的时间复杂度为 O(nlogn) ,所以整个时间复杂度为 O(nlogn)

代码如下:

    public int findUnsortedSubarray(int[] nums) {
        int[] sorted = nums.clone();
        Arrays.sort(sorted);
        int len = 0;
        for (int i = 0; i < nums.length; i++){
            if (nums[i] == sorted[i]) len++;
            else break;
        }
        for (int j = nums.length-1; j >=0; j--){
            if (nums[j] == sorted[j]) len++;
            else break;
        }
        return Math.max(0, nums.length-len);
    }

582 Kill Process (6分)

Problem:

Given n processes, each process has a unique PID (process id) and its PPID (parent process id).

Each process only has one parent process, but may have one or more children processes. This is just like a tree structure. Only one process has PPID that is 0, which means this process has no parent process. All the PIDs will be distinct positive integers.

We use two list of integers to represent a list of processes, where the first list contains PID for each process and the second list contains the corresponding PPID.

Now given the two lists, and a PID representing a process you want to kill, return a list of PIDs of processes that will be killed in the end. You should assume that when a process is killed, all its children processes will be killed. No order is required for the final answer.

Example 1:

Input:
pid = [1, 3, 10, 5]
ppid = [3, 0, 5, 3]
kill = 5
Output: [5,10]
Explanation:
Kill 5 will also kill 10.

Note:

  • The given kill id is guaranteed to be one of the given PIDs.
  • n >= 1.

我的思路:
map+队列,因为PID的父类不会继承多个PPID,所以我们可以用PPID作为键值,把PPID的孩子们都找到存在Map中,KILL某个进程时,依次把它的孩子进程也杀死即可,因此存在队列中。代码如下:

public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int kill) {
        Map<Integer,List<Integer>> map = new HashMap<>();
        for (int i = 0; i < ppid.size(); i++){
            map.computeIfAbsent(ppid.get(i), k -> new ArrayList<Integer>()).add(pid.get(i));
        }
        List<Integer> ans = new ArrayList<>();
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(kill);
        while (!queue.isEmpty()){
            int target = queue.poll();
            ans.add(target);
            if (map.containsKey(target)){
                List<Integer> tmp = map.get(target);
                for (int num : tmp){
                    queue.offer(num);
                }
            }
        }
        return ans;
    }

583 Delete Operation for Two Strings (7分)

Problem:

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.

Example 1:

Input: “sea”, “eat”
Output: 2
Explanation: You need one step to make “sea” to “ea” and another step to make “eat” to “ea”.

Note:

  • The length of given words won’t exceed 500.
  • Characters in given words can only be lower-case letters.

最小编辑距离的简单版本,直接用DP解决即可。

思路:

dp[i][j]:表示word1[0,i)和word2[0,j)的最小编辑距离。

两种情况:

if word1[i] == word2[j]; dp[i+1][j+1] = dp[i][j];
if word2[i] != word2[j]; dp[i+1][j+1] = 1 + max{dp[i][j+1],dp[i+1][j]}

代码如下:

public int minDistance(String word1, String word2) {
        int n = word1.length();
        int m = word2.length();

        int[][] dp = new int[n+1][m+1];
        for (int i = 0; i < n; i++){
            Arrays.fill(dp[i], 1<<30);
        }

        dp[0][0] = 0;
        for (int i = 0; i < n; i++){
            dp[i+1][0] = i+1;
        }
        for (int j = 0; j < m; j++){
            dp[0][j+1] = j+1;
        }

        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                if (word1.charAt(i) == word2.charAt(j)){
                    dp[i+1][j+1] = dp[i][j];
                }else{
                    dp[i+1][j+1] = Math.min(dp[i][j+1]+1, Math.min(dp[i+1][j]+1, dp[i][j]+2));
                }
            }
        }

        return dp[n][m];
    }

可优化的地方:

  • dp初始化为INF可以去除,只要把min中的+1提取出来即可。
  • dp[0][j]和dp[i][0]的更新可以合并到双重循环中。

优化代码如下:

    public int minDistance(String word1, String word2) {
        int n = word1.length();
        int m = word2.length();

        int[][] dp = new int[n+1][m+1];
        for (int i = 0; i <= n; i++){
            for (int j = 0; j <= m; j++){
                if (i == 0 || j == 0){
                    dp[i][j] = i + j;
                    continue;
                }
                if (word1.charAt(i-1) == word2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1];
                }else{
                    dp[i][j] = 1 + Math.min(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[n][m];
    }

587 Erect the Fence (9分)

Problem:

There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden. Your job is to fence the entire garden using the minimum length of rope as it is expensive. The garden is well fenced only if all the trees are enclosed. Your task is to help find the coordinates of trees which are exactly located on the fence perimeter.

Example 1:

Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
Output: [[1,1],[2,0],[4,2],[3,3],[2,4]]
Explanation:
alt text

Example 2:

Input: [[1,2],[2,2],[4,2]]
Output: [[1,2],[2,2],[4,2]]
Explanation:
alt text
Even you only have trees in a line, you need to use rope to enclose them.

Note:

  • All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group.
  • All input integers will range from 0 to 100.
  • The garden has at least one tree.
  • All coordinates are distinct.
  • Input points have NO order. No order required for output.

寻找凸包问题,暂且还没学习,先把代码贴上,重新开篇文章专门研究下凸包。

代码如下:

public List<Point> outerTrees(Point[] points) {
        Point first = points[0];
        int firstIndex = 0;
        // Find the leftmost point
        for (int i = 0; i < points.length; i++) {
            Point point = points[i];
            if (point.x < first.x) {
                first = point;
                firstIndex = i;
            }
        }

        Set<Point> answer = new HashSet<>();
        Point cur = first;
        int curIndex = firstIndex;
        answer.add(first);

        do {
            Point next = points[0];
            int nextIndex = 0;
            for (int i = 1; i < points.length; i++) {
                if (i == curIndex)
                    continue;
                Point p = points[i];
                int cross = crossProductLength(p, cur, next);
                if (nextIndex == curIndex || cross > 0 ||
                // Handle multi points in a line
                        (cross == 0 && distance(p, cur) > distance(next, cur))) {
                    next = p;
                    nextIndex = i;
                }
            }
            // Handle multi points in a line
            for (int i = 0; i < points.length; i++) {
                Point p = points[i];
                int cross = crossProductLength(p, cur, next);
                if (i != curIndex && cross == 0) {
                    answer.add(p);
                }
            }

            cur = next;
            curIndex = nextIndex;
        } while (curIndex != firstIndex);

        return new ArrayList<>(answer);
    }

    private int crossProductLength(Point A, Point B, Point C) {
        // Get the vectors' coordinates.
        int BAx = A.x - B.x;
        int BAy = A.y - B.y;
        int BCx = C.x - B.x;
        int BCy = C.y - B.y;

        // Calculate the Z coordinate of the cross product.
        return (BAx * BCy - BAy * BCx);
    }

    private int distance(Point p1, Point p2) {
        return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
    }

补充:(2017-05-16)
上述代码用到了jarvisMarch方法,是凸包计算中的一个 O(nh) 的算法。关于凸包的更多算法可参考博文【算法细节系列(18):凸包的三种计算】。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这道题是一道字符串处理题。给定一个字符串 s,它的所有的字符都是小写的英文字母。要求把这个字符串变成一个按照字典序排序的字符串,并且要求在变换过程中只能在原来的字符串中交换两个相邻的字符。 解题思路: - 从前往后扫描字符串 s,找到第一个不满足字典序的字符,记为字符 x。 - 从 x 往后扫描,找到最后一个比 x 大的字符 y,将 x 与 y 交换。 - 将 x 后面的字符串倒序排列,这样就得到了字典序更大的字符串。 下面是 Java 代码的实现: ``` class Solution { public String nextPermutation(String s) { char[] chars = s.toCharArray(); // 从后往前找到第一个不满足字典序的字符 x int i = chars.length - 2; while (i >= 0 && chars[i] >= chars[i + 1]) { i--; } // 如果 i < 0,说明原来的字符串已经是字典序最大的字符串,直接返回倒序排列的字符串 if (i < 0) { reverse(chars, 0, chars.length - 1); return new String(chars); } // 从 x 往后扫描,找到最后一个比 x 大的字符 y int j = chars.length - 1; while (j > i && chars[j] <= chars[i]) { j--; } // 将 x 与 y 交换 swap(chars, i ### 回答2: 题目:LeetCode第38题:报数 题目描述: 给定一个正整数n,输出报数序列前n个数。 报数规则:从1开始报数,数到3的倍数时报Fizz,数到5的倍数时报Buzz,数到同时是3和5的倍数时报FizzBuzz,其他情况下则直接报数。 解题思路: 使用循环遍历1到n的所有数字,按照报数规则进行判断并输出。 具体步骤如下: 1. 创建一个StringBuilder对象res,用于存储报数序列。 2. 使用for循环从1遍历到n。 3. 判断当前数字是否同时是3和5的倍数,如果是,则将"FizzBuzz"添加到res中。 4. 判断当前数字是否是3的倍数,如果是,则将"Fizz"添加到res中。 5. 判断当前数字是否是5的倍数,如果是,则将"Buzz"添加到res中。 6. 如果以上条件都不满足,则将当前数字转换为字符串并添加到res中。 7. 循环结束后,将res转换为字符串并返回。 Java代码如下: ```java public String countAndSay(int n) { StringBuilder res = new StringBuilder(); for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 == 0) { res.append("FizzBuzz"); } else if (i % 3 == 0) { res.append("Fizz"); } else if (i % 5 == 0) { res.append("Buzz"); } else { res.append(Integer.toString(i)); } } return res.toString(); } ``` 以上代码可以将1到n的报数序列输出,并按照题目要求进行相应转换。 ### 回答3: 题目要求是根据给定的正整数 n,返回一个字符串,该字符串包含从 1 到 n 的所有数字对应的字符串,并且满足以下条件: 1. 如果数字能被 3 整除,则使用字母 "Fizz" 替代该数字。 2. 如果数字能被 5 整除,则使用字母 "Buzz" 替代该数字。 3. 如果数字能同时被 3 和 5 整除,则使用字母 "FizzBuzz" 替代该数字。 解题思路: 利用循环遍历从 1 到 n 的所有数字,使用条件语句判断每个数字是否满足以上三个条件,然后根据条件替换数字并存入结果字符串中,最后返回结果。 Java代码如下: ```java class Solution { public String fizzBuzz(int n) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= n; i++) { if (i % 3 == 0 && i % 5 == 0) { result.append("FizzBuzz"); } else if (i % 3 == 0) { result.append("Fizz"); } else if (i % 5 == 0) { result.append("Buzz"); } else { result.append(i); } if (i != n) { result.append(" "); } } return result.toString(); } } ``` 这段代码使用StringBuilder来构建结果字符串,判断每个数字是否满足条件,并根据条件拼接对应的字符串,每个数字之间用空格隔开。最后将StringBuilder转换成String并返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值