【Leetcode周赛】从contest100开始。

contest-100

contest-101 (Sep. 9, 2018)题号(900~903)

比赛情况记录:迅速的过了第一题和第二题,第三题卡住了,然后搞了好久比赛都结束了十分钟,才把第三题搞过了。第四题没看题。

这周的题目有点像OOD,900 和 901

【900】 RLE Iterator

给了一个数组A,偶数下标 i 的元素 A[i] 代表有 A[i] 个 A[i+1], 不断的求next(int n) 函数。返回next(n)的返回值。

题解:我是用了一个数组idx来标记下标的范围,比如idx[0] = 0, idx[1] = 5 表示在原始数组中[0, 5)的区间范围内的数字是A[1],然后用一个cur表示当前下标。

提交WA了两次,因为没注意数的范围,爆int了。

 

 1 class RLEIterator {
 2 public:
 3     RLEIterator(vector<int> A) {
 4         nums = A;
 5         idx.push_back(0);
 6         for (int i = 0; i < A.size();  i += 2) {
 7             idx.push_back(idx.back() +(long long) A[i]);
 8         }
 9         return;
10     }
11     
12     int next(int n) {
13         cur += (long long)n;
14         auto iter = lower_bound(idx.begin(), idx.end(), cur);
15         if (iter == idx.end()) {
16             return -1;
17         }
18         int target = iter - idx.begin();
19         return nums[target*2-1];
20         
21     }
22     vector<int> nums;
23     vector<long long> idx;
24     long long cur = 0;
25 };
26 
27 /**
28  * Your RLEIterator object will be instantiated and called as such:
29  * RLEIterator obj = new RLEIterator(A);
30  * int param_1 = obj.next(n);
31  */
View Code

 

【901】 Online Stock Span 

扫描二维码关注公众号,回复: 3095826 查看本文章

依次输入n个数,每次输入一个nums[i],求前面小于等于当前数字的最大长度。举个例子,

Input: ["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]]
Output: [null,1,1,1,2,1,4,6]
Explanation: 
First, S = StockSpanner() is initialized.  Then:
S.next(100) is called and returns 1,
S.next(80) is called and returns 1,
S.next(60) is called and returns 1,
S.next(70) is called and returns 2,
S.next(60) is called and returns 1,
S.next(75) is called and returns 4,
S.next(85) is called and returns 6.

Note that (for example) S.next(75) returned 4, because the last 4 prices
(including today's price of 75) were less than or equal to today's price.

题解:我一开始直接暴力,然后tle了。后来用了一个标记位,nums[i].first 代表输入,nums[i].second 代表当前元素的结果。如果 nums[i+1].first >= nums[pre].first,那么nums[i+1].second += nums[pre].second。pre不断往前移动,直到不满足小于等于的条件,

 1 class StockSpanner {
 2 public:
 3     StockSpanner() {
 4         
 5     }
 6     
 7     int next(int price) {
 8         nums.push_back(make_pair(price, 1));
 9         if (nums.size() == 1) { return 1; }
10         
11         int idx = nums.size() - 2, last = idx + 1;
12         while (idx >= 0) {
13             if (nums[idx].first <= nums[last].first) {
14                 nums[last].second += nums[idx].second;
15                 idx -= nums[idx].second;
16             } else {
17                 break;
18             }
19         }        
20         return nums.back().second;
21     }
22     vector<pair<int, int>> nums;
23 };
24 
25 /**
26  * Your StockSpanner object will be instantiated and called as such:
27  * StockSpanner obj = new StockSpanner();
28  * int param_1 = obj.next(price);
29  */
View Code

【902】 Numbers At Most N Given Digit Set 

 

 

【903】 Valid Permutations for DI Sequence

 

猜你喜欢

转载自www.cnblogs.com/zhangwanying/p/9613603.html
今日推荐