Skip to content

Commit

Permalink
Bag of Tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Mar 4, 2024
1 parent 64b42d9 commit bdda048
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ add_task(average-value-of-even-numbers-that-are-divisible-by-three)
add_task(average-waiting-time)
add_task(avoid-flood-in-the-city)
add_task(backspace-string-compare)
add_task(bag-of-tokens)
add_task(balance-a-binary-search-tree)
add_task(balanced-binary-tree)
add_task(base-7)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@
| 944. | [Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/) | [C++](./solutions/delete-columns-to-make-sorted/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> | |
| 946. | [Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/) | [C++](./solutions/validate-stack-sequences/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Stack-64337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 947. | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) | [C++](./solutions/most-stones-removed-with-same-row-or-column/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Union Find-337f3b?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> | |
| 948. | [Bag of Tokens](https://leetcode.com/problems/bag-of-tokens/) | [C++](./solutions/bag-of-tokens/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogN) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Two Pointers-7f6633?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
| 949. | [Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits/) | [C++](./solutions/largest-time-for-given-digits/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Enumeration-7f7a33?style=flat-square'/> | |
| 950. | [Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/) | [C++](./solutions/reveal-cards-in-increasing-order/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Queue-6b337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 951. | [Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [C++](./solutions/flip-equivalent-binary-trees/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 944. | [Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/) | [C++](./solutions/delete-columns-to-make-sorted/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> | |
| 946. | [Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/) | [C++](./solutions/validate-stack-sequences/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Stack-64337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 947. | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) | [C++](./solutions/most-stones-removed-with-same-row-or-column/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Union Find-337f3b?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> | |
| 948. | [Bag of Tokens](https://leetcode.com/problems/bag-of-tokens/) | [C++](./solutions/bag-of-tokens/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogN) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Two Pointers-7f6633?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
| 949. | [Largest Time for Given Digits](https://leetcode.com/problems/largest-time-for-given-digits/) | [C++](./solutions/largest-time-for-given-digits/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Enumeration-7f7a33?style=flat-square'/> | |
| 950. | [Reveal Cards In Increasing Order](https://leetcode.com/problems/reveal-cards-in-increasing-order/) | [C++](./solutions/reveal-cards-in-increasing-order/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Queue-6b337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 951. | [Flip Equivalent Binary Trees](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [C++](./solutions/flip-equivalent-binary-trees/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
Expand Down Expand Up @@ -1095,4 +1096,3 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 1389. | [Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/) | [C++](./solutions/create-target-array-in-the-given-order/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Simulation-7e7f33?style=flat-square'/> | |
| 1392. | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/) | [C++](./solutions/longest-happy-prefix/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N) / O(1)| <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Rolling Hash-6b7f33?style=flat-square'/> <img src='https://img.shields.io/badge/String Matching-7f3346?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Function-647f33?style=flat-square'/> | |
| 1393. | [Capital Gain/Loss](https://leetcode.com/problems/capital-gainloss/) | [SQL](./extra/sql/capital-gainloss/solution.sql), [PY](./extra/pandas/capital-gainloss/solution.py) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 1394. | [Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/) | [C++](./solutions/find-lucky-integer-in-an-array/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
1 change: 1 addition & 0 deletions solutions/bag-of-tokens/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_bag_of_tokens test.cpp)
27 changes: 27 additions & 0 deletions solutions/bag-of-tokens/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <ranges>
#include <vector>

// Time: O(NlogN)
// Space: O(N)

class Solution {
public:
static int bagOfTokensScore(std::vector<int> tokens, int power) {
std::ranges::sort(tokens);
int ans = 0;
for (int i = 0, j = tokens.size() - 1; i <= j;) {
if (tokens[i] <= power) {
power -= tokens[i++];
++ans;
} else if (ans && i < j) {
power += tokens[j--];
--ans;
} else {
break;
}
}
return ans;
}
};
34 changes: 34 additions & 0 deletions solutions/bag-of-tokens/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::vector<int> tokens;
int power;
int expected;
};

std::vector<TestCase> test_cases{
{
.tokens{100},
.power = 50,
.expected = 0,
},
{
.tokens{200, 100},
.power = 150,
.expected = 1,
},
{
.tokens{100, 200, 300, 400},
.power = 200,
.expected = 2,
},
};

for (const auto &[tokens, power, expected] : test_cases) {
const auto actual = Solution::bagOfTokensScore(tokens, power);
REQUIRE(expected == actual);
}
}

0 comments on commit bdda048

Please sign in to comment.