955. Delete Columns to Make Sorted II

We are given an array A of N lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have an array A = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef","vyz"].

Suppose we chose a set of deletion indices D such that after deletions, the final array has its elements in lexicographic order (A[0] <= A[1] <= A[2] ... <= A[A.length - 1]).

Return the minimum possible value of D.length.

 

Example 1:

Input: ["ca","bb","ac"]
Output: 1
Explanation:
After deleting the first column, A = ["a", "b", "c"].
Now A is in lexicographic order (ie. A[0] <= A[1] <= A[2]).
We require at least 1 deletion since initially A was not in lexicographic order, so the answer is 1.

Example 2:

Input: ["xc","yb","za"]
Output: 0
Explanation:
A is already in lexicographic order, so we don't need to delete anything.
Note that the rows of A are not necessarily in lexicographic order:
ie. it is NOT necessarily true that (A[0][0] <= A[0][1] <= ...)

Example 3:

Input: ["zyx","wvu","tsr"]
Output: 3
Explanation:
We have to delete every column.

Note:

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

Appraoch #1: C++.

class Solution {
public:
int minDeletionSize(vector<string>& A) {
int N = A.size();
int W = A[0].length();
int ans = 0;
vector<string> temp(N, "");
for (int i = 0; i < W; ++i) {
vector<string> cur = temp;
for (int j = 0; j < N; ++j) {
cur[j] += A[j][i];
}
if (isSorted(cur))
temp = cur;
else
ans++;
}
return ans;
}
private:
bool isSorted(vector<string> str) {
for (int i = 1; i < str.size(); ++i) {
if (str[i] < str[i-1]) return false;
}
return true;
}
};

  

Analysis:

we use a container to store the lexicographic, In each time we check whether the lexicographic string add the current columns contenting the statement. If yes, we add the currrent char to the lexicographic. If no, the answer +1.

Complexity Analysis

Time Complexity: O(NW^2), where NN is the length of A, and WW is the length of A[i].

Space Complexity: O(NW).

953. Verifying an Alien Dictionary

In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.

Example 1:

Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.

Example 2:

Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.

Example 3:

Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less than any other character (More info).

Note:

  1. 1 <= words.length <= 100
  2. 1 <= words[i].length <= 20
  3. order.length == 26
  4. All characters in words[i] and order are english lowercase letters.

Approach #2: C++.

class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
int mapping[26];
for (int i = 0; i < order.length(); ++i) {
mapping[order[i]-'a'] = i;
}
for (auto& word : words) {
for (auto& c : word) {
c = mapping[c-'a'];
}
}
return is_sorted(words.begin(), words.end());
}
};  

Analysis:

std::is_sorted

954. Array of Doubled Pairs

Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.

 

Example 1:

Input: [3,1,3,6]
Output: false

Example 2:

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

Example 3:

Input: [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].

Example 4:

Input: [1,2,4,16,8,4]
Output: false

Note:

  1. 0 <= A.length <= 30000
  2. A.length is even
  3. -100000 <= A[i] <= 100000

Approach #1: C++.

class Solution {
public:
bool canReorderDoubled(vector<int>& A) {
int len = A.size();
unordered_map<int, int> mp;
for (int i = 0; i < len; ++i) {
mp[A[i]]++;
}
sort(A.begin(), A.end());
for (int i = 0; i < len; ++i) {
if (mp[A[i]] > 0) {
if (mp[2*A[i]] > 0) {
mp[A[i]]--;
mp[2*A[i]]--;
}
}
}
for (int i = 0; i < len; ++i) {
if (mp[A[i]] > 0) return false;
}
return true;
}
};

Analysis:

more difference ways.

956. Tallest Billboard(can't understand)

You are installing a billboard and want it to have the largest height.  The billboard will have two steel supports, one on each side.  Each steel support must be an equal height.

You have a collection of rods which can be welded together.  For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.

Return the largest possible height of your billboard installation.  If you cannot support the billboard, return 0.

Example 1:

Input: [1,2,3,6]
Output: 6
Explanation: We have two disjoint subsets {1,2,3} and {6}, which have the same sum = 6.

Example 2:

Input: [1,2,3,4,5,6]
Output: 10
Explanation: We have two disjoint subsets {2,3,5} and {4,6}, which have the same sum = 10.

Example 3:

Input: [1,2]
Output: 0
Explanation: The billboard cannot be supported, so we return 0.

Note:

  1. 0 <= rods.length <= 20
  2. 1 <= rods[i] <= 1000
  3. The sum of rods is at most 5000.
 

Appraoch #1: C++.

class Solution {
public:
int tallestBillboard(vector<int>& rods) {
unordered_map<int, int> dp;
dp[0] = 0;
for (int x : rods) {
unordered_map<int, int> cur(dp);
for (auto it : cur) {
int d = it.first;
dp[d+x] = max(dp[d+x], cur[d]);
dp[abs(d-x)] = max(dp[abs(d-x)], cur[d] + min(d, x));
}
}
return dp[0];
}
};

  

Weekly Contest 114的更多相关文章

  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. Windows Server 2008 R2换SID要注意

    今天刚装Windows2008R2,准备做实验.同样,我对虚拟机采用了母盘和差异磁盘.在新建好的虚拟机上使用NewSID执行更新SID操作时,一切正常,但当更新完并重启进入系统后,竟然蓝屏了.   原 ...

  2. (转)更改Web.config中对上传文件大小限制

    .net上传超过200K的图片的时候,会跳转到404,但是url没有错误,真J8的坑啊. 本文转载自:http://www.cnblogs.com/zwffff/archive/2009/04/29/ ...

  3. .net core 环境安装

    NET Core开发环境搭建 使用VS2015开发.NET Core项目,环境的搭建可以参考官网,大致安装步骤如下: 1.首先你得装个vs2015 并且保证已经升级至 update3及以上, 2.vs ...

  4. [更新中]【fit-flow使用总结】djang开发中git flow使用总结

    djang开发中git flow使用总结 初次接触可以先看看此链接上关于git flow的东西http://danielkummer.github.io/git-flow-cheatsheet/ind ...

  5. 第十章 深入理解Session与Cookie

    理解Cookie 理解Session Cookie安全问题 分布式Session框架 Cookie压缩 表单重复提交问题 多终端Session统一

  6. java 字符串和集合互相转换

    今天在写项目的时候遇到一个问题,就是要把得到的一个集合转换成字符串,发现 import org.apache.commons.lang.StringUtils; 有这么一个简单的方法:String s ...

  7. JavaScript组合模式---引入

    首先:使用一个例子来引入组合模式,需求为(1)有一个学校有2个班(一班,二班)(2)每个班级分2个小组(一班一组,一班二组,二班一组,二班二组)(3)学校计算机教室有限,每一个小组分着来上课 然后:根 ...

  8. Ajax入门(一)从0开始到一次成功的GET请求

    什么是服务器 网页浏览过程分析 一个完整的HTTP请求过程,通常有下面7个步骤 建立TCP连接 Web浏览器向Web服务器发送请求命令 Web浏览器发送请求头信息 Web服务器- 应答 Web服务器- ...

  9. Solr各组件之间的关系图

    原文地址:http://blog.csdn.net/clj198606061111/article/details/20854419

  10. 2-3 zookeeper文件夹主要目录介绍

    zookeeper-3.4.11.jar.zookeeper-3.4.11.jar.md5.zookeeper-3.4.11.sha1都是通过打包或者编译之后产生的相关的文件.那么maven相关的东西 ...