Skip to content

Commit

Permalink
Problems number updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Apr 7, 2019
1 parent 7d0d405 commit f35825f
Show file tree
Hide file tree
Showing 27 changed files with 802 additions and 15 deletions.
6 changes: 6 additions & 0 deletions 1009-Complement-of-Base-10-Integer/cpp-1009/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.13)
project(A)

set(CMAKE_CXX_STANDARD 14)

add_executable(A main.cpp)
34 changes: 34 additions & 0 deletions 1009-Complement-of-Base-10-Integer/cpp-1009/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// Source : https://leetcode.com/problems/complement-of-base-10-integer/
/// Author : liuyubobobo
/// Time : 2019-03-16

#include <iostream>
#include <vector>

using namespace std;


/// Simulation
/// Time Complexity: O(logN)
/// Space Complexity: O(logN)
class Solution {
public:
int bitwiseComplement(int N) {

if(!N) return 1;

vector<int> binary;
while(N) binary.push_back(1 - N % 2), N /= 2;
reverse(binary.begin(), binary.end());

int res = 0;
for(int b: binary) res = res * 2 + b;
return res;
}
};


int main() {

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.13)
project(B)

set(CMAKE_CXX_STANDARD 14)

add_executable(B main.cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// Source : https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/
/// Author : liuyubobobo
/// Time : 2019-03-16

#include <iostream>
#include <vector>

using namespace std;


/// Using HashMap
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {

vector<int> rem(60, 0);
for(int t: time) rem[t % 60] ++;

long long res = 0;
if(rem[0]) res += (long long)rem[0] * (rem[0] - 1) / 2;
if(rem[30]) res += (long long)rem[30] * (rem[30] - 1) / 2;
for(int i = 1; i < 30; i ++)
res += (long long)rem[i] * rem[60 - i];
return res;
}
};


int main() {

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.13)
project(C)

set(CMAKE_CXX_STANDARD 14)

add_executable(C main.cpp)
56 changes: 56 additions & 0 deletions 1011-Capacity-To-Ship-Packages-Within-D-Days/cpp-1011/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/// Source : https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
/// Author : liuyubobobo
/// Time : 2019-03-16

#include <iostream>
#include <vector>
#include <numeric>

using namespace std;


/// Binary Search
/// Time Complexity: O(nlog(sum(weights)))
/// Space Complexity: O(1)
class Solution {
public:
int shipWithinDays(vector<int>& weights, int D) {

int l = *max_element(weights.begin(), weights.end()),
r = accumulate(weights.begin(), weights.end(), 0);
while(l < r){
// cout << "check " << l << " " << r << endl;
int mid = (l + r) / 2;
if(ok(weights, mid, D))
r = mid;
else
l = mid + 1;
}
return l;
}

private:
bool ok(const vector<int>& weights, int C, int D){

int d = 0, cur = 0;
for(int w: weights)
if(cur + w <= C) cur += w;
else d ++, cur = w;
if(cur) d ++;
return d <= D;
}
};


int main() {

vector<int> weight1 = {1,2,3,4,5,6,7,8,9,10};
cout << Solution().shipWithinDays(weight1, 5) << endl;
// 15

vector<int> weight2 = {1,2,3,1,1};
cout << Solution().shipWithinDays(weight2, 4) << endl;
// 3

return 0;
}
6 changes: 6 additions & 0 deletions 1012-Numbers-With-1-Repeated-Digit/cpp-1012/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.13)
project(D)

set(CMAKE_CXX_STANDARD 14)

add_executable(D main.cpp)
106 changes: 106 additions & 0 deletions 1012-Numbers-With-1-Repeated-Digit/cpp-1012/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/// Source : https://leetcode.com/problems/numbers-with-1-repeated-digit/
/// Author : liuyubobobo
/// Time : 2019-03-16

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;


/// Combination Mathematics
/// Time Complexity: O(log(N)^2)
/// Space Complexity: O(logN)
class Solution {
public:
int numDupDigitsAtMostN(int N) {

vector<int> diff(10, 0);
for(int i = 1; i <= 9; i ++)
diff[i] = get_diff(i);
for(int i = 1; i < 10; i ++) diff[i] += diff[i - 1];
// for(int e: diff) cout << e << " "; cout << endl;

vector<int> power10(10, 1);
for(int i = 1; i < 10; i ++) power10[i] = power10[i - 1] * 10;
// for(int e: power10) cout << e << " "; cout << endl;

vector<int> num;
while(N) num.push_back(N % 10), N /= 10;
reverse(num.begin(), num.end());
// for(int e: num) cout << e << " "; cout << endl;

int res = power10[num.size() - 1] - 1 - diff[num.size() - 1];
// cout << res << endl;

unordered_set<int> digits;
for(int i = 0; i < num.size(); i ++){

if(i == num.size() - 1){
for(int d = 0; d <= num[i]; d ++)
if(digits.count(d)) res ++;
break;
}
else if(num[i]){
int tres = (num[i] - (i == 0)) * power10[num.size() - 1 - i];
if(tres){
tres -= howmanydiff(num.size() - i, digits, num[i], i != 0);
res += tres;
}
}

if(!digits.count(num[i]))
digits.insert(num[i]);
else{
res += 1 + get_num(num, i + 1);
break;
}
}
return res;
}

private:
int howmanydiff(int n, const unordered_set<int>& digits, int first, bool canZero){

int res = 0;
for(int i = canZero ? 0 : 1; i < first; i ++)
if(!digits.count(i)) res ++;
n --;

int cur = 10 - (digits.size() + 1);
while(n --)
res *= cur--;
return res;
}

int get_num(const vector<int>& num, int s){
int res = 0;
for(int i = s; i < num.size(); i ++)
res = res * 10 + num[i];
return res;
}

int get_diff(int n){

int res = 9;
n --;

int cur = 9;
while(n --) res *= cur --;
return res;
}
};


int main() {

cout << Solution().numDupDigitsAtMostN(20) << endl; // 1
cout << Solution().numDupDigitsAtMostN(100) << endl; // 10
cout << Solution().numDupDigitsAtMostN(1000) << endl; // 262
cout << Solution().numDupDigitsAtMostN(11) << endl; // 1
cout << Solution().numDupDigitsAtMostN(101) << endl; // 11
cout << Solution().numDupDigitsAtMostN(110) << endl; // 12

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.13)
project(A)

set(CMAKE_CXX_STANDARD 14)

add_executable(A main.cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// Source : https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/
/// Author : liuyubobobo
/// Time : 2019-03-23

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;


/// Linear Scan
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
bool canThreePartsEqualSum(vector<int>& A) {

int sum = accumulate(A.begin(), A.end(), 0);
if(sum % 3) return false;

int cur = 0, i = 0, j = A.size() - 1;
do{
cur += A[i ++];
}while(i < A.size() && cur != sum / 3);

if(i == A.size()) return false;

cur = 0;
do{
cur += A[j --];
}while(j >= 0 && cur != sum / 3);
if(j < 0) return false;

return i <= j;
}
};


int main() {

return 0;
}
6 changes: 6 additions & 0 deletions 1014-Best-Sightseeing-Pair/cpp-1014/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.13)
project(C)

set(CMAKE_CXX_STANDARD 14)

add_executable(C main2.cpp)
48 changes: 48 additions & 0 deletions 1014-Best-Sightseeing-Pair/cpp-1014/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// Source : https://leetcode.com/problems/best-sightseeing-pair/
/// Author : liuyubobobo
/// Time : 2019-03-23

#include <iostream>
#include <vector>

using namespace std;


/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int maxScoreSightseeingPair(vector<int>& A) {

int n = A.size();

vector<int> B = A;
for(int i = 0; i < n; i ++) B[i] -= i;
for(int i = n - 2; i >= 0; i --)
B[i] = max(B[i], B[i + 1]);

int res = A[0] + B[1];
for(int i = 1; i + 1 < n; i ++)
res = max(res, A[i] + i + B[i + 1]);
return res;
}
};


int main() {

vector<int> A1 = {8,1,5,2,6};
cout << Solution().maxScoreSightseeingPair(A1) << endl;
// 11

vector<int> A2 = {3,7,2,3};
cout << Solution().maxScoreSightseeingPair(A2) << endl;
// 9

vector<int> A3 = {1, 3, 5};
cout << Solution().maxScoreSightseeingPair(A3) << endl;
// 7

return 0;
}
Loading

0 comments on commit f35825f

Please sign in to comment.