Line data Source code
1 : // Copyright (c) 2023 The Authors. All rights reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // https://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : // Authors: liubang (it.liubang@gmail.com)
16 : // Created: 2023/05/05 00:51
17 :
18 :
19 : #include <vector>
20 :
21 : #include <gtest/gtest.h>
22 :
23 : namespace {
24 : class Solution {
25 : public:
26 4 : int hardestWorker(int n, const std::vector<std::vector<int>>& logs) {
27 4 : int ret = logs[0][0];
28 4 : int max_time = logs[0][1];
29 17 : for (int i = 1; i < logs.size(); ++i) {
30 13 : int tmp = logs[i][1] - logs[i - 1][1];
31 13 : if (tmp == max_time)
32 2 : ret = std::min(ret, logs[i][0]);
33 11 : else if (tmp > max_time) {
34 3 : max_time = tmp;
35 3 : ret = logs[i][0];
36 : }
37 : }
38 4 : return ret;
39 : }
40 : };
41 : } // namespace
42 :
43 4 : TEST(Leetcode, leetcode) {
44 1 : Solution s;
45 7 : EXPECT_EQ(1, s.hardestWorker(10, {{0, 3}, {2, 5}, {0, 9}, {1, 15}}));
46 7 : EXPECT_EQ(3, s.hardestWorker(26, {{1, 1}, {3, 7}, {2, 12}, {7, 17}}));
47 5 : EXPECT_EQ(0, s.hardestWorker(2, {{0, 10}, {1, 20}}));
48 10 : EXPECT_EQ(
49 1 : 12, s.hardestWorker(70, {{36, 3}, {1, 5}, {12, 8}, {25, 9}, {53, 11}, {29, 12}, {52, 14}}));
50 9 : }
|