shit leetcode edge testcases
shit leetcode edge testcases
- Merge Intervals

try
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-26
* @modified
*
* @description 56. Merge Intervals
* @description 56. 合并区间
* @difficulty Medium
* @complexity O(n)
* @time O(n)
* @augments
* @example
* @link https://leetcode.com/problems/merge-intervals/
* @link https://leetcode-cn.com/problems/merge-intervals/
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
/**
* @param {number[][]} intervals
* @return {number[][]}
*/
var merge = function(intervals) {
const result = [];
if(intervals.length < 2) {
return intervals || [];
}
// sort
intervals.sort(([a0], [b0]) => a0 > b0 ? 1 : -1);
const map = new Map();
let temp = intervals[0];
for (let i = 1; i < intervals.length; i++) {
// [[1,3],[2,6],[8,10],[15,18]]
const [t0, t1] = temp;
const [a0, a1] = intervals[i];
// log(`t0, t1 =`, t0, t1)
// log(`a0, a1 =`, a0, a1)
if(a0 <= t1 && a1 > t1) {
// merge
result.push([t0, a1]);
map.set(JSON.stringify([t0, a1]))
temp = [t0, a1];
} else if (a0 > t1) {
// 重复 bug
if(!map.has(JSON.stringify([t0, t1]))) {
result.push([t0, t1]);
map.set(JSON.stringify([t0, t1]))
}
result.push([a0, a1]);
map.set(JSON.stringify([a0, a1]))
temp = [a0, a1];
} else {
result.push([a0, a1]);
map.set(JSON.stringify([a0, a1]))
temp = [a0, a1];
}
}
return result;
};
const intervals = [[1, 3], [2, 6], [8, 10], [ 15, 18]];
const test = merge(intervals);
log(`test =`, test);
// [[1, 6], [8, 10], [15, 18]]
log(`test =`, JSON.stringify(test) === JSON.stringify([[1, 6], [8, 10], [15, 18]]) ? `` : ``);
const intervals2 = [[1, 4], [4, 5]];
const test2 = merge(intervals2);
log(`test2 =`, test2);
// [[1, 5]]
log(`test =`, JSON.stringify(test2) === JSON.stringify([[1,5]]) ? `` : ``);
const intervals3 = [[1, 4], [5, 6]];
const test3 = merge(intervals3);
log(`test3 =`, test3);
// [[1,4],[5,6]]
log(`test =`, JSON.stringify(test3) === JSON.stringify([[1,4],[5,6]]) ? `` : ``);
const intervals4= [[1, 4], [0, 1]];
const test4 = merge(intervals4);
log(`test4 =`, test4);
// [[0,4]]
log(`test =`, JSON.stringify(test4) === JSON.stringify( [[0,4]]) ? `` : ``);
const intervals5= [[1, 4], [1, 5]];
const test5 = merge(intervals5);
log(`test5 =`, test5);
// [[1,5]]
log(`test =`, JSON.stringify(test5) === JSON.stringify([[1,5]]) ? `` : ``);
https://leetcode.com/problems/merge-intervals/
https://leetcode-cn.com/problems/merge-intervals/submissions/
refs
https://github.com/LeetCode-Feedback/LeetCode-Feedback/issues/1457
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
shit leetcode edge testcases的更多相关文章
- NOI前的考试日志
4.14 网络流专项测试 先看T1,不会,看T2,仙人掌???wtf??弃疗.看T3,貌似最可做了,然后开始刚,刚了30min无果,打了50分暴力,然后接着去看T1,把序列差分了一下,推了会式子,发现 ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Minimum Height Trees 最小高度树
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [LeetCode] The Skyline Problem
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- Leetcode: Matchsticks to Square && Grammar: reverse an primative array
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- Palindrome Pairs -- LeetCode 336
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
随机推荐
- 通俗易懂的解释:什么是API
API 全称 Application Programming Interface,即应用程序编程接口. 看到这里,急性子的小白同学马上就憋不住了:这不管是英文还是中文我每个字都懂啊,只是凑一块就不知道 ...
- pickle — Python object serialization
pickle - Python object serialization 消息队列
- 内联扩展 inline expansion An Inline Function is As Fast As a Macro 与宏的比较
让编译器直接将完整的函数体插入到每一个调用该函数的地方,从而提高函数调用的运行速度. 优秀的JIT编译器会通过侦测运行信息,仅将需要频繁运行的瓶颈部分进行编译,从而大大削减编译所需的时间. 而且,利用 ...
- 【LinuxShell】ps 命令浅析
前言 Linux上查看进程状态最常用的命令,本文对 ps 命令参数以及状态做一下简单介绍. 参数 ps a 显示现行终端机下的所有程序,包括其他用户的程序. ps -A 显示所有程序. ps c 列出 ...
- LOJ10043
题目描述 原题来自:HNOI 2002 Tiger 最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger 拿出了公司的账本,账本上记录了公司成 ...
- 004_C++常见错误类型总结(一)之最后几行错误
1.介绍 经常进行代码测试和静态代码分析的同学,应该会遇到这样的一个问题,就是一个程序段的最后几行,或者一个源文件末尾会出现错误.本文,结合专业的静态代码分析软件PSV-Studio提供错误类型代码库 ...
- Golang 版的ssh爆破小工具
源码如下: package main import ( "bufio" "flag" "fmt" "golang.org/x/cr ...
- Docker容器启动Mysql,Docker实现Mysql主从,读写分离
Docker容器启动Mysql,Docker实现Mysql主从,读写分离 一.Docker文件编排 二.配置主从复制 2.1 配置master 2.2 配置slave 三.验证主从复制 3.1 mas ...
- 周期性清除Spark Streaming流状态的方法
在Spark Streaming程序中,若需要使用有状态的流来统计一些累积性的指标,比如各个商品的PV.简单的代码描述如下,使用mapWithState()算子: val productPvStrea ...
- 读取固定ResourceBundle
private static ResourceBundle ssoBundle = ResourceBundle.getBundle("CASHHSSO");//默认根目录 pub ...