984. String Without AAA or BBB

Given two integers A and B, return any string S such that:

  • S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters;
  • The substring 'aaa' does not occur in S;
  • The substring 'bbb' does not occur in S.

Example 1:

Input: A = 1, B = 2
Output: "abb"
Explanation: "abb", "bab" and "bba" are all correct answers.

Example 2:

Input: A = 4, B = 1
Output: "aabaa"

Note:

  1. 0 <= A <= 100
  2. 0 <= B <= 100
  3. It is guaranteed such an S exists for the given A and B.

Approach #1:

class Solution {
public:
string strWithout3a3b(int A, int B) {
string ans = "";
char a = 'a';
char b = 'b';
if (B > A) {
swap(A, B);
swap(a, b);
}
while (A != 0 || B != 0) {
if (A > 0) ans += a, A--;
if (A > B) ans += a, A--;
if (B > 0) ans += b, B--;
if (B > A) ans += b, B--;
} return ans;
}
};

  

981. Time Based Key-Value Store

Create a timebased key-value store class TimeMap, that supports two operations.

1. set(string key, string value, int timestamp)

  • Stores the key and value, along with the given timestamp.

2. get(string key, int timestamp)

  • Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
  • If there are multiple such values, it returns the one with the largest timestamp_prev.
  • If there are no values, it returns the empty string ("").

Example 1:

Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
Output: [null,null,"bar","bar",null,"bar2","bar2"]
Explanation:  
TimeMap kv;  
kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1  
kv.get("foo", 1); // output "bar"  
kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"  
kv.set("foo", "bar2", 4);  
kv.get("foo", 4); // output "bar2"  
kv.get("foo", 5); //output "bar2"  

Example 2:

Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]]
Output: [null,null,null,"","high","high","low","low"]

Note:

  1. All key/value strings are lowercase.
  2. All key/value strings have length in the range [1, 100]
  3. The timestamps for all TimeMap.set operations are strictly increasing.
  4. 1 <= timestamp <= 10^7
  5. TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.

Approach #1:

class TimeMap {
public:
/** Initialize your data structure here. */
vector<string> ans;
map<string, vector<pair<int, string>>> mp;
TimeMap() { } void set(string key, string value, int timestamp) {
mp[key].push_back({timestamp, value});
} string get(string key, int timestamp) {
if (mp.count(key)) {
for (int i = mp[key].size()-1; i >= 0 ; --i) {
if (mp[key][i].first <= timestamp) {
return mp[key][i].second;
}
}
}
return "";
} }; /**
* Your TimeMap object will be instantiated and called as such:
* TimeMap* obj = new TimeMap();
* obj->set(key,value,timestamp);
* string param_2 = obj->get(key,timestamp);
*/

  

982. Triples with Bitwise AND Equal To Zero

Given an array of integers A, find the number of triples of indices (i, j, k) such that:

  • 0 <= i < A.length
  • 0 <= j < A.length
  • 0 <= k < A.length
  • A[i] & A[j] & A[k] == 0, where & represents the bitwise-AND operator.

Example 1:

Input: [2,1,3]
Output: 12
Explanation: We could choose the following i, j, k triples:
(i=0, j=0, k=1) : 2 & 2 & 1
(i=0, j=1, k=0) : 2 & 1 & 2
(i=0, j=1, k=1) : 2 & 1 & 1
(i=0, j=1, k=2) : 2 & 1 & 3
(i=0, j=2, k=1) : 2 & 3 & 1
(i=1, j=0, k=0) : 1 & 2 & 2
(i=1, j=0, k=1) : 1 & 2 & 1
(i=1, j=0, k=2) : 1 & 2 & 3
(i=1, j=1, k=0) : 1 & 1 & 2
(i=1, j=2, k=0) : 1 & 3 & 2
(i=2, j=0, k=1) : 3 & 2 & 1
(i=2, j=1, k=0) : 3 & 1 & 2

Note:

  1. 1 <= A.length <= 1000
  2. 0 <= A[i] < 2^16

Approach #1:

class Solution {
public:
int countTriplets(vector<int>& A) {
int size = A.size();
int ans = 0;
unordered_map<int, int> mp;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
++mp[A[i] & A[j]];
}
}
for (int i = 0; i < size; ++i) {
for (auto m : mp) {
if ((A[i] & m.first) == 0)
ans += m.second;
}
}
return ans;
}
};

  

983. Minimum Cost For Tickets

In a country popular for train travel, you have planned some train travelling one year in advance.  The days of the year that you will travel is given as an array days.  Each day is an integer from 1 to 365.

Train tickets are sold in 3 different ways:

  • a 1-day pass is sold for costs[0] dollars;
  • a 7-day pass is sold for costs[1] dollars;
  • a 30-day pass is sold for costs[2] dollars.

The passes allow that many days of consecutive travel.  For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.

Return the minimum number of dollars you need to travel every day in the given list of days.

Example 1:

Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.

Example 2:

Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.

Note:

  1. 1 <= days.length <= 365
  2. 1 <= days[i] <= 365
  3. days is in strictly increasing order.
  4. costs.length == 3
  5. 1 <= costs[i] <= 1000

Approach #1:

class Solution {
public:
int mincostTickets(vector<int>& days, vector<int>& costs) {
vector<int> dp(366, 0);
vector<bool> isday(366, false);
for (int day: days) {
isday[day] = true;
}
for (int i = 1; i <= 365; ++i) {
if (!isday[i]) {
dp[i] = dp[i-1];
continue;
}
dp[i] = costs[0] + dp[i-1];
if (i >= 7) {
dp[i] = min(dp[i], costs[1]+dp[i-7]);
} else {
dp[i] = min(dp[i], costs[1]);
}
if (i >= 30) {
dp[i] = min(dp[i], costs[2]+dp[i-30]);
} else {
dp[i] = min(dp[i], costs[2]);
}
}
return dp[365];
}
};

  

最新文章

  1. 基于modelsim-SE的简单仿真流程—上
  2. TYVJ P1080 N皇后
  3. List 集合线程安全测试
  4. eclipse代码自动提示设置、如何配置eclipse的代码自动提示功能(同时解决自动补全变量名的问题)?
  5. IE6/7中setAttribute不支持class/for/rowspan/colspan等属性
  6. 利用ajax在javascript中获取后台的值
  7. 标准C++的string类使用
  8. Shell 终端ANSI控制码
  9. 1 认识Jquery
  10. windows命名管道
  11. RN九宫格
  12. [LeetCode&amp;Python] Problem 237. Delete Node in a Linked List
  13. 《Linux内核分析》第二周学习报告
  14. springboot 文件上传 java.io.IOException: The temporary upload location [/tmp/xx] is not valid
  15. notepadd添加插件
  16. git使用经验(一)
  17. Python在线教程(廖雪峰)
  18. HDU 5699 二分+线性约束
  19. python+requests+json 接口测试思路示例
  20. Session值的存储与删除

热门文章

  1. SPI子系统分析之三:驱动模块
  2. C# Attribute的用法
  3. Excel VBA 入门(零)
  4. 蒟蒻LQL的博客
  5. Mysql设置auto_increment_increment和auto_increment_offset
  6. linux系统文件的链接
  7. spring4-2-bean配置-3-自动装配
  8. centos 命令行和图形桌面模式的切换
  9. phpmailer配置qq邮箱
  10. 修复jqgrid setgridparam postdata 的多次查询条件累加