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

Weekly Contest 138的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  5. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

  6. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  7. LeetCode Weekly Contest 47

    闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...

  8. 75th LeetCode Weekly Contest Champagne Tower

    We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...

  9. LeetCode之Weekly Contest 102

    第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...

随机推荐

  1. mysql主从复制,主主复制,级联复制,半同步复制

    -------------------------------------------------------------------------------主从复制----------------- ...

  2. 2020年12月-第01阶段-前端基础-HTML常用标签

    1. HTML常用标签 首先 HTML和CSS是两种完全不同的语言,我们学的是结构,就只写HTML标签,认识标签就可以了. 不会再给结构标签指定样式了. HTML标签有很多,这里我们学习最为常用的,后 ...

  3. Linux下查看文件内容的几种常用命令

    [常用] 1,cat     由第一行开始显示内容,并将所有内容输出 cat的功能是将文件从第一行开始连续的将内容输出在屏幕上.但是cat并不常用,原因是当文件大,行数比较多时,屏幕无法全部容下时,只 ...

  4. LAB1 启动操作系统

    从机器上电到运行OS发生了什么? 在电脑主板上有一个Flash块,存放了BIOS的可执行代码.它是ROM,断电不会丢掉数据.在机器上电的时候,CPU要求内存控制器从0地址读取数据(程序第一条指令)的时 ...

  5. Flask模板注入

    Flask模板注入 Flask模板注入漏洞属于经典的SSTI(服务器模板注入漏洞). Flask案例 一个简单的Flask应用案例: from flask import Flask,render_te ...

  6. 海岸线、科赫曲线、turtle、递归

    本章绘图要点: turtle模块:python标准库自带的一个模块,可用来绘制二维图形.该模块封装了底层的数据处理逻辑,向外提供了更符合手工绘图习惯的接口函数,适用于绘制对质量.精度要求不高的图形. ...

  7. RichText实现动态输入关键字高亮颜色显示

    int a = 0; string[] kc = new string[40] { "private","protected","public&quo ...

  8. Ingress-nginx工作原理和实践

    本文记录/分享 目前项目的 K8s 部署结构和请求追踪改造方案 这个图算是一个通用的前后端分离的 k8s 部署结构: Nginx Ingress 负责暴露服务(nginx前端静态资源服务), 根据十二 ...

  9. sqli-labs系列——第五关

    less5 更改id后无果,不能用union联合查询 此处用报错注入 报错注入的概念:(1). 通过floor报错 and (select 1 from (select count(*),concat ...

  10. [系统重装日志2]win10系统安装pytorch0.4.1(gpu版本)

    目录 0,资源整理 1,安装最新版的显卡驱动 2,安装visual studio 3,安装cuda 4,安装cudnn,配置环境变量 5,安装pytorch 6,安装torchvision 7,验证 ...