forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95d6873
commit f14984d
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/) | | | | ||
|