From f14984d6fc4cf7737865fdd67e387cd0a95e6679 Mon Sep 17 00:00:00 2001 From: Yubo Liu Date: Sun, 7 Jul 2019 11:36:23 -0700 Subject: [PATCH] 1103 added. --- .../cpp-1103/CMakeLists.txt | 6 +++ .../cpp-1103/main.cpp | 32 +++++++++++++++ .../cpp-1103/main2.cpp | 40 +++++++++++++++++++ readme.md | 2 + 4 files changed, 80 insertions(+) create mode 100644 1103-Distribute-Candies-to-People/cpp-1103/CMakeLists.txt create mode 100644 1103-Distribute-Candies-to-People/cpp-1103/main.cpp create mode 100644 1103-Distribute-Candies-to-People/cpp-1103/main2.cpp diff --git a/1103-Distribute-Candies-to-People/cpp-1103/CMakeLists.txt b/1103-Distribute-Candies-to-People/cpp-1103/CMakeLists.txt new file mode 100644 index 00000000..b1976006 --- /dev/null +++ b/1103-Distribute-Candies-to-People/cpp-1103/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.14) +project(A) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(A main2.cpp) \ No newline at end of file diff --git a/1103-Distribute-Candies-to-People/cpp-1103/main.cpp b/1103-Distribute-Candies-to-People/cpp-1103/main.cpp new file mode 100644 index 00000000..95b59190 --- /dev/null +++ b/1103-Distribute-Candies-to-People/cpp-1103/main.cpp @@ -0,0 +1,32 @@ +/// Source : https://leetcode.com/problems/distribute-candies-to-people/ +/// Author : liuyubobobo +/// Time : 2019-06-29 + +#include +#include + +using namespace std; + + +/// Simulation +/// Time Complexity: O(sqrt(candies)) +/// Space Complexity: O(1) +class Solution { +public: + vector distributeCandies(int candies, int num_people) { + + vector res(num_people, 0); + for(int c = 1, i = 0; candies; c ++, i ++){ + int x = min(c, candies); + res[i % num_people] += x; + candies -= x; + } + return res; + } +}; + + +int main() { + + return 0; +} \ No newline at end of file diff --git a/1103-Distribute-Candies-to-People/cpp-1103/main2.cpp b/1103-Distribute-Candies-to-People/cpp-1103/main2.cpp new file mode 100644 index 00000000..2c5a2f94 --- /dev/null +++ b/1103-Distribute-Candies-to-People/cpp-1103/main2.cpp @@ -0,0 +1,40 @@ +/// Source : https://leetcode.com/problems/distribute-candies-to-people/ +/// Author : liuyubobobo +/// Time : 2019-07-07 + +#include +#include +#include + +using namespace std; + + +/// Mathematics +/// See here for details: https://leetcode.com/problems/distribute-candies-to-people/solution/ +/// +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector distributeCandies(int candies, int num_people) { + + int p = (int)(sqrt(2 * candies + 0.25) - 0.5); + int last_remaining = candies - p * (p + 1) / 2; + int rows = p / num_people, cols = p % num_people; + + vector res(num_people); + for(int i = 0; i < num_people; i ++){ + res[i] = (i + 1) * rows + rows * (rows - 1) / 2 * num_people; + if(i < cols) res[i] += (i + 1) + rows * num_people; + } + res[cols] += last_remaining; + + return res; + } +}; + + +int main() { + + return 0; +} \ No newline at end of file diff --git a/readme.md b/readme.md index 9e8e0219..de53b226 100644 --- a/readme.md +++ b/readme.md @@ -750,6 +750,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 1099 | [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/) | [无] | [C++](1099-Two-Sum-Less-Than-K/cpp-1099/) | | | | | | | | | | +| 1103 | [Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/) | [solution](https://leetcode.com/problems/distribute-candies-to-people/solution/) | [C++](1103-Distribute-Candies-to-People/cpp-1103/) | | | +| | | | | | | | 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/) | [无] | [C++](1108-Defanging-an-IP-Address/cpp-1108/) | | | | 1109 | [Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [无] | [C++](1109-Corporate-Flight-Bookings/cpp-1009/) | | | | 1110 | [Delete Nodes And Return Forest](https://leetcode.com/problems/delete-nodes-and-return-forest/) | [无] | [C++](1110-Delete-Nodes-And-Return-Forest/cpp-1110/) | | |