zoukankan      html  css  js  c++  java
  • Weekly Contest 138

    1051. Height Checker

    Students are asked to stand in non-decreasing order of heights for an annual photo.

    Return the minimum number of students not standing in the right positions.  (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)

    Example 1:

    Input: [1,1,4,2,1,3]
    Output: 3
    Explanation: 
    Students with heights 4, 3 and the last 1 are not standing in the right positions.

    Note:

    1. 1 <= heights.length <= 100
    2. 1 <= heights[i] <= 100

    Approach #1: [Java]

    class Solution {
        public int heightChecker(int[] heights) {
            int[] copy = new int[heights.length];
            for (int i = 0; i < heights.length; ++i) {
                copy[i] = heights[i];
            }
            Arrays.sort(heights);
            int ret = 0;
            for (int i = 0; i < heights.length; ++i) {
                if (copy[i] != heights[i])
                    ret++;
            }
            
            return ret;
        }
    }
    

      

    1052. Grumpy Bookstore Owner

    Today, the bookstore owner has a store open for customers.length minutes.  Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.

    On some minutes, the bookstore owner is grumpy.  If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0.  When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.

    The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.

    Return the maximum number of customers that can be satisfied throughout the day.

    Example 1:

    Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
    Output: 16
    Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes. 
    The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.

    Note:

    • 1 <= X <= customers.length == grumpy.length <= 20000
    • 0 <= customers[i] <= 1000
    • 0 <= grumpy[i] <= 1

    Approach #1: [Java]

    class Solution {
        public int maxSatisfied(int[] customers, int[] grumpy, int X) {
            int s = 0, e = 0;
            int getFromX = 0;
            int res = 0;
            for (int i = 0; i <= grumpy.length - X; ++i) {
                int temp = 0;
                for (int j = i; j < i + X; ++j) {
                    if (grumpy[j] == 1) {
                        temp += customers[j];
                    }
                }
                if (temp > getFromX) {
                    getFromX = temp;
                    s = i;
                    e = i + X;
                }
            }
            
            for (int i = 0; i < s; ++i) {
                if (grumpy[i] == 0)
                    res += customers[i];
            }
            for (int i = s; i < e; ++i) {
                res += customers[i];
            }
            for (int i = e; i < grumpy.length; ++i) {
                if (grumpy[i] == 0)
                    res += customers[i];
            }
            
            return res;
        }
    }
    

      

    1053. Previous Permutation With One Swap

    Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.

    Example 1:

    Input: [3,2,1]
    Output: [3,1,2]
    Explanation: Swapping 2 and 1.
    

    Example 2:

    Input: [1,1,5]
    Output: [1,1,5]
    Explanation: This is already the smallest permutation.
    

    Example 3:

    Input: [1,9,4,6,7]
    Output: [1,7,4,6,9]
    Explanation: Swapping 9 and 7.
    

    Example 4:

    Input: [3,1,1,3]
    Output: [1,3,1,3]
    Explanation: Swapping 1 and 3.

    Note:

    1. 1 <= A.length <= 10000
    2. 1 <= A[i] <= 10000

    Approach #1: [Java]

    class Solution {
        public int[] prevPermOpt1(int[] A) {
            int len = A.length;
            int left = len - 2, right = len - 1, tmp;
            while (left >= 0 && A[left] <= A[left+1]) left--;
            if (left < 0) return A;
            while (A[left] <= A[right]) right--;
            while (A[right-1] == A[right]) right--;
            tmp = A[left];
            A[left] = A[right];
            A[right] = tmp;
            return A;
        }
    }
    

      

    1054. Distant Barcodes

    In a warehouse, there is a row of barcodes, where the i-th barcode is barcodes[i].

    Rearrange the barcodes so that no two adjacent barcodes are equal.  You may return any answer, and it is guaranteed an answer exists.

    Example 1:

    Input: [1,1,1,2,2,2]
    Output: [2,1,2,1,2,1]
    

    Example 2:

    Input: [1,1,1,1,2,2,3,3]
    Output: [1,3,1,3,2,1,2,1]

    Note:

    1. 1 <= barcodes.length <= 10000
    2. 1 <= barcodes[i] <= 10000

    Approach #1: PriorityQueue [Java]

    class Solution {
        public int[] rearrangeBarcodes(int[] barcodes) {
            Map<Integer, Integer> memo = new HashMap<>();
            for (int i : barcodes) {
                memo.put(i, memo.getOrDefault(i, 0) + 1);
            }
            Queue<Integer> pq = new PriorityQueue<>((a, b)->(memo.get(b) - memo.get(a)));
            for (int i : memo.keySet()) {
                pq.add(i);
            }
            int[] ans = new int[barcodes.length];
            int k = 0;
            int count = 0;
            Queue<Integer> wait = new LinkedList<>();
            while (!pq.isEmpty()) {
                while (!pq.isEmpty() && count < 2) {
                    int first = pq.poll();
                    ans[k++] = first;
                    memo.put(first, memo.get(first) - 1);
                    wait.add(first);
                    count++;
                }
                while (!wait.isEmpty() && wait.size() != 1) {
                    if (memo.get(wait.peek()) > 0) {
                        pq.add(wait.poll());
                    } else {
                        wait.poll();
                    }
                }
                count = 0;
            }
            return ans;
        }
    }
    

      

    Reference:

    https://docs.oracle.com/javase/9/docs/api/java/util/PriorityQueue.html

    https://blog.csdn.net/top_code/article/details/8650729 

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    后缀数组 (Suffix Array) 学习笔记
    Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
    [ USACO 2013 OPEN ] Photo
    清华集训2016做题记录
    「UNR#2」黎明前的巧克力
    「UNR#1」奇怪的线段树
    Atcoder Grand Contest 018 E
    「NOI2015」小园丁与老司机
    「集训队作业2018」三角形
    Codeforces 878 E. Numbers on the blackboard
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10925683.html
Copyright © 2011-2022 走看看