Skip to content

Commit 36f3619

Browse files
committed
Implement divisor-game
name: divisor-game url: https://leetcode.com/problems/divisor-game difficulty: 1 time: 100.0 ms time-rank: -1 % time-complexity: O(N) space: 5.8 MB space-rank: 99.96 % space-complexity: O(1) Fixes #304 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 41b326c commit 36f3619

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

divisor-game-test.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Friedt
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <gtest/gtest.h>
8+
9+
#include "divisor-game.cpp"
10+
11+
using namespace std;
12+
13+
TEST(DivisorGame, 2) { EXPECT_EQ(true, Solution().divisorGame(2)); }
14+
15+
TEST(DivisorGame, 3) { EXPECT_EQ(false, Solution().divisorGame(3)); }
16+
17+
TEST(DivisorGame, 4) { EXPECT_EQ(true, Solution().divisorGame(4)); }
18+
19+
TEST(DivisorGame, 5) { EXPECT_EQ(false, Solution().divisorGame(5)); }

divisor-game.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Friedt
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
// clang-format off
8+
// name: divisor-game
9+
// url: https://leetcode.com/problems/divisor-game
10+
// difficulty: 1
11+
// clang-format on
12+
13+
#include <climits>
14+
15+
using namespace std;
16+
17+
class Solution {
18+
public:
19+
bool divisorGame(int N) {
20+
enum player {
21+
ALICE,
22+
BOB,
23+
};
24+
25+
uint8_t loser;
26+
for (loser = ALICE; 1 != N; --N, loser ^= 1)
27+
;
28+
return loser == BOB;
29+
}
30+
};

0 commit comments

Comments
 (0)