Skip to content

Commit

Permalink
1103 added.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Jul 7, 2019
1 parent 95d6873 commit f14984d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 1103-Distribute-Candies-to-People/cpp-1103/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.14)
project(A)

set(CMAKE_CXX_STANDARD 14)

add_executable(A main2.cpp)
32 changes: 32 additions & 0 deletions 1103-Distribute-Candies-to-People/cpp-1103/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// Source : https://leetcode.com/problems/distribute-candies-to-people/
/// Author : liuyubobobo
/// Time : 2019-06-29

#include <iostream>
#include <vector>

using namespace std;


/// Simulation
/// Time Complexity: O(sqrt(candies))
/// Space Complexity: O(1)
class Solution {
public:
vector<int> distributeCandies(int candies, int num_people) {

vector<int> 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;
}
40 changes: 40 additions & 0 deletions 1103-Distribute-Candies-to-People/cpp-1103/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// Source : https://leetcode.com/problems/distribute-candies-to-people/
/// Author : liuyubobobo
/// Time : 2019-07-07

#include <iostream>
#include <vector>
#include <cmath>

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<int> 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<int> 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;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ email: [[email protected]](mailto:[email protected])
| | | | | | |
| 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/) | | |
Expand Down

0 comments on commit f14984d

Please sign in to comment.