Menu Sidebar
Menu

Minimum Number of Operations to Convert Time

给两个string, 里面是二十四制的小时,求最小的步骤通过1、5、10、60把string current 变成 correct。

先变成数字, 然后贪婪从大到小。

class Solution {
    public int convertTime(String current, String correct) {
        String[] strs = current.split(":");
        String[] ss = correct.split(":");
        int curSum = Integer.valueOf(strs[0])*60 + Integer.valueOf(strs[1]);
        int corSum = Integer.valueOf(ss[0])*60 + Integer.valueOf(ss[1]);
        int diff = corSum - curSum;
        int res = 0;
        res += (diff / 60);
        diff = diff % 60;
        res += (diff / 15);
        diff = diff % 15;
        res += (diff / 5);
        diff = diff % 5;
        res += diff;
        return res;
    }
}

Minimum Health to Beat Game

给一个数组, 里面是受到的伤害, 给一个数字armor,是护甲, 可以在一轮伤害中使用, 然后抵挡伤害后,护甲作废. 求最大hp能活下来(hp >= 1);

这题先算伤害的和, 然后再用护甲去抗一下最大的伤害. 最大伤害 +1 是活下来的hp, 然后减去用的甲就是答案.

class Solution {
    public long minimumHealth(int[] damage, int armor) {
        long res = 0;
        long max = 0; 
        for(int n : damage){
            res += n; 
            max = Math.max(n, max);
        }
        return res - Math.min(max, armor)+1;
    }
}

Maximum Sum Score of Array

给一个array, 求每个index前边所有数的和和后边所有数字的和的最大值.

class Solution {
    public long maximumSumScore(int[] nums) {
        long sum = 0;
        for(int n : nums)
            sum += n;
        long res = Long.MIN_VALUE / 2;
        long pre = 0;
        for(int n : nums){
            pre += n;
            res = Math.max(res, Math.max(pre,sum));
            sum -= n;
        }
        return res;
    }
}

Can Place Flowers

给一个数组, 里面的1代表一个花, 花之间不能相邻别的花, 给一个数n, 求这个数组能不能放下n个花.

class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int m = 0; 
        while(true){
            boolean put = false;
            for(int i = 0; i < flowerbed.length; i++) {
                if(flowerbed[i] == 0){
                    if(((i - 1 >= 0 && flowerbed[i - 1] == 0) || i - 1 < 0) && ((i + 1 < flowerbed.length && flowerbed[i + 1] == 0) || (i + 1 >= flowerbed.length)) && n > 0){
                        flowerbed[i] = 1;
                        n--;w
                        put = true;
                        break;
                    } 
            }
            } 
            if(n == 0)
                return true;
            if(!put)
                return false;
        } 
    }
}

Find the Difference of Two Arrays

给两个数组, 求两个list, 第一个list是第一个数组中不含第二个数组的数字, 第二个list是第二个数字中不含第一个数组的数字.

class Solution {
    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        Set<Integer> set3 = new HashSet<>();
        Set<Integer> set4 = new HashSet<>();
        for(int n : nums1){
            set1.add(n); 
        }
        for(int n : nums2){
            set2.add(n);
        } 
        for(int n : nums1){
            set2.remove(n);
        }
        for(int n : nums2){
            set1.remove(n);
        } 
        List<Integer> l1 = new ArrayList<>(set1);
        List<Integer> l2 = new ArrayList<>(set2);
        List<List<Integer>> res = new ArrayList<>();
        res.add(l1);
        res.add(l2);
        return res;
    }
}

Sort Array By Parity II

给一个数组, 一半是奇数, 一半是偶数. 按照index的奇偶, 分配数字的奇偶.

class Solution {
    public int[] sortArrayByParityII(int[] nums) { 
        int[] res = new int[nums.length];
        int i = 0;
        int j = 1;
        for(int n : nums){
            if(n % 2 == 0){
                res[i] = n;
                i+=2;
            }
            else
            {
                res[j] = n;
                j+=2;
            }
        }
        return res;
    }
}

Long Pressed Name

给两个string, 一个是typed, 一个是name, 求一个是不是想输入name, 但是长按几个字符后, 变成了typed.

这题我用的是run length encode, 先encode两个变成a1b1c2这种形式, 然后看是不是length一样, 再看是不是name的数字小于typed的数字, 除此之外, 就是true.

class Solution {
    class Pair{
        Pair(char c, int a){
            this.c = c;
            this.a = a;
        }
        public String toString()
          {
        return this.c + " " + this.a;
          }
        char c;
        int a;
    }
    public boolean isLongPressedName(String name, String typed) {
        LinkedList<Pair> a = encode(name);
        LinkedList<Pair> b = encode(typed);
        if(a.size() != b.size())
            return false;
        int n = a.size();
        for(int i = 0; i < n; i++) {
            Pair ca = a.get(i);
            Pair cb = b.get(i);
            if(ca.c != cb.c)
                return false;
            if(ca.a > cb.a)
                return false;
        }
        return true;
    }
    
    public LinkedList<Pair>  encode(String s){
        LinkedList<Pair> list = new LinkedList<>();
        for(char c : s.toCharArray()){
            if(list.isEmpty())
                list.addLast(new Pair(c, 1));
            else{
                Pair pre = list.removeLast();
                if(c == pre.c){
                    pre.a++;
                    list.addLast(pre);
                }else
                {
                    list.addLast(pre);
                    list.addLast(new Pair(c, 1));
                }
            }
        }
        return list;
    }
}

Longest Harmonious Subsequence

定义一个和谐子序列为序列中最大值和最小值的差是1, 求最大和谐子序列.

这题是counting题目, 找到所有数字的count, 然后找一个数字和其的前后数字的count, 求最大即可.

class Solution {
    public int findLHS(int[] nums) { 
        Map<Integer, Integer> map = new HashMap<>();
        for(int n : nums)
            map.put(n, map.getOrDefault(n, 0) + 1);
        int res = 0;
        for(int n : nums){
            int nn = map.get(n);
            Integer np = map.getOrDefault(n - 1, null);
            Integer nm = map.getOrDefault(n + 1, null);
            if(np == null && nm == null)
                continue;
            if(np != null)
                res = Math.max(res, nn + np);
            if(nm != null)
                res = Math.max(res, nn + nm);
                
        }
        return res;
    }
}

Sort Even and Odd Indices Independently

排序一个数组, 偶数index的数字递增, 奇数index的数字递减.

class Solution {
    public int[] sortEvenOdd(int[] nums) {
        int n = nums.length;
        List<Integer> odd = new ArrayList<>();
        List<Integer> even = new ArrayList<>();
        for(int i = 0; i < nums.length; i++){
            if(i % 2 == 0)
                even.add(nums[i]);
            else
                odd.add(nums[i]);
        }
        Collections.sort(even);
        Collections.sort(odd, Collections.reverseOrder());
        int[] res = new int[n]; 
        int j = 0;
        int k = 0;
        for(int i = 0; i < nums.length; i++){
            if(i % 2 == 0)
                res[i] = even.get(j++);
            else
                res[i] = odd.get(k++);
        }
        return res;
    }
}

Keep Multiplying Found Values by Two

给一个数组和一个数字n, 求一直做n乘2, 直到不在数组里找到n.

class Solution {
    public int findFinalValue(int[] nums, int original) {
        Set<Integer> set = new HashSet<>();
        for(int n : nums)
            set.add(n);
        while(set.contains(original)){
            original *= 2;
        }
        return original;
    }
}
Newer Posts
Older Posts

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031