Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 864ea35

Browse files
committed
update combination problem
1 parent e632bc9 commit 864ea35

File tree

3 files changed

+176
-1
lines changed

3 files changed

+176
-1
lines changed

‎Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CXX = g++
22
CPPFLAGS = -Wall -std=c++11 -O2
33
LD_FLAGS =
44

5-
FILE = algoexpert/05-jul
5+
FILE = algoexpert/08-jul
66
# FILE = contest/contest-195
77
SOURCES = $(FILE).cpp
88
OBJECTS = $(SOURCES:.cpp=.o)

‎july/08.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int ll;
6+
typedef pair<int, int> ii;
7+
typedef vector<int> vi;
8+
typedef vector<string> vs;
9+
10+
#define len(container) int((container).size())
11+
#define all(c) (c).begin(), (c).end()
12+
13+
const int M = 1e9 + 7;
14+
15+
template <typename ITER>
16+
void show(ITER begin, ITER end) {
17+
for (int i = 1; begin != end; i++) {
18+
printf("%d ", *(begin++));
19+
if (i % 20 == 0 or begin == end) printf("\n");
20+
}
21+
};
22+
23+
template <typename T>
24+
void addMod(T& a, T b) {
25+
a = (a + b) % M;
26+
}
27+
28+
inline bool isValid(int x, int y, int R, int C) {
29+
return x >= 0 && x < R && y >= 0 && y < C;
30+
}
31+
32+
struct TreeNode {
33+
int val;
34+
TreeNode* left;
35+
TreeNode* right;
36+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
37+
};
38+
39+
class Solution {
40+
public:
41+
vector<vector<int>> threeSum(vector<int>& nums) {
42+
sort(nums.begin(), nums.end());
43+
set<std::tuple<int, int, int>> res;
44+
45+
unordered_set<int> s(nums.begin(), nums.end());
46+
47+
for (int i = 0; i < nums.size() && nums[i] <= 0; i++) {
48+
int d = -nums[i];
49+
int l = i + 1, h = nums.size() - 1;
50+
while (l < h) {
51+
if (nums[l] + nums[h] == d) {
52+
res.insert({nums[i], nums[l], nums[h]});
53+
// OR should prune duplicate in this step:
54+
// while (lo < hi && num[lo] == num[lo+1]) lo++;
55+
l++;
56+
h--;
57+
} else if (nums[l] + nums[h] > d)
58+
h--;
59+
else
60+
l++;
61+
}
62+
}
63+
64+
vector<vector<int>> ans;
65+
for (auto [k, l, m] : res) {
66+
ans.push_back({k, l, m});
67+
}
68+
return ans;
69+
}
70+
};
71+
72+
int main(int argc, char* argv[]) {
73+
// Solution s = Solution();
74+
return 0;
75+
}

‎problems/39.combination-sum.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* @lc app=leetcode id=39 lang=cpp
3+
*
4+
* [39] Combination Sum
5+
*
6+
* https://leetcode.com/problems/combination-sum/description/
7+
*
8+
* algorithms
9+
* Medium (53.48%)
10+
* Likes: 3773
11+
* Dislikes: 115
12+
* Total Accepted: 535.7K
13+
* Total Submissions: 965.3K
14+
* Testcase Example: '[2,3,6,7]\n7'
15+
*
16+
* Given a set of candidate numbers (candidates) (without duplicates) and a
17+
* target number (target), find all unique combinations in candidates where the
18+
* candidate numbers sums to target.
19+
*
20+
* The same repeated number may be chosen from candidates unlimited number of
21+
* times.
22+
*
23+
* Note:
24+
*
25+
*
26+
* All numbers (including target) will be positive integers.
27+
* The solution set must not contain duplicate combinations.
28+
*
29+
*
30+
* Example 1:
31+
*
32+
*
33+
* Input: candidates = [2,3,6,7], target = 7,
34+
* A solution set is:
35+
* [
36+
* ⁠ [7],
37+
* ⁠ [2,2,3]
38+
* ]
39+
*
40+
*
41+
* Example 2:
42+
*
43+
*
44+
* Input: candidates = [2,3,5], target = 8,
45+
* A solution set is:
46+
* [
47+
* [2,2,2,2],
48+
* [2,3,3],
49+
* [3,5]
50+
* ]
51+
*
52+
*
53+
*/
54+
55+
#include <vector>
56+
57+
using namespace std;
58+
59+
// @lc code=start
60+
class Solution {
61+
public:
62+
void findCombination(vector<vector<int>>& res, const int start,
63+
const int target, vector<int>& local,
64+
const vector<int>& num) {
65+
if (target == 0) {
66+
res.push_back(local);
67+
return;
68+
}
69+
70+
for (int i = start; i < num.size(); i++) {
71+
if (num[i] > target) return;
72+
73+
local.push_back(num[i]);
74+
findCombination(res, i, target - num[i], local, num);
75+
local.pop_back();
76+
}
77+
}
78+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
79+
vector<vector<int>> res;
80+
vector<int> local;
81+
sort(candidates.begin(), candidates.end());
82+
findCombination(res, 0, target, local, candidates);
83+
return res;
84+
}
85+
86+
int combinationSum4(vector<int>& nums, int target) {
87+
const int N = nums.size();
88+
sort(all(nums));
89+
vector<ll> ways(target + 1, 0);
90+
ways[0] = 1;
91+
for (int i = 1; i <= target; ++i) {
92+
for (auto num : nums) {
93+
if (i < num) break;
94+
ways[i] = ways[i] + ways[i - num];
95+
}
96+
}
97+
return ways[target];
98+
}
99+
};
100+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /