leetcode(c++)(DFS)
#include <iostream> #include <stack> #include <vector> using namespace std; struct TreeNode{ TreeNode* left = nullptr, *right = nullptr; int val = 0; TreeNode(int v):val(v){} }; vector<int>inorder(TreeNode* root) { vector<int>res; stack<TreeNode*>st; while(root != nullptr || !st.empty()) { while(nullptr != root) { st.push(root); root = root->left; } root = st.top(); st.pop(); res.emplace_back(root->val); root = root->right; } return res; } void print(const vector<int>&nums) { for(auto && num : nums) { cout << num << " "; } cout << endl; } void dfs(vector<vector<int>>& res,const vector<int>&nums,vector<int>&tmp,int start) { res.emplace_back(tmp); for(int i = start; i < nums.size(); ++i) { tmp.emplace_back(nums[i]); dfs(res,nums,tmp,i + 1); tmp.pop_back(); } return ; } vector<vector<int>>subset(const vector<int>& nums) { vector<vector<int>>res; vector<int>tmp; dfs(res,nums,tmp,0); return res; } void print(const vector<vector<int>>& nums) { for(auto && num : nums) { for(auto && n : num) { cout << n << " "; } cout << endl; } } bool isContain(const vector<int>& nums,int val) { for(auto &num : nums) { if(num == val)return true; } return false; } void dfsII(const vector<int>& nums,vector<vector<int>>& res,vector<int>& tmp,int start) { res.emplace_back(tmp); for(int i = start; i < nums.size(); ++i) { if(i != start && nums[i] == nums[i-1])continue; tmp.emplace_back(nums[i]); dfsII(nums,res,tmp,start + 1); tmp.pop_back(); } } vector<vector<int>>subsetII(vector<int>& nums) { vector<vector<int>>res; vector<int>tmp; sort(nums.begin(),nums.end()); dfsII(nums,res,tmp,0); res.pop_back(); return res; } void help(vector<int>& nums,vector<vector<int>>&res,vector<int>&tmp,int index) { if(tmp.size() == nums.size())res.emplace_back(tmp); else { for(int i = 0; i < nums.size(); ++i) { if(isContain(tmp,nums[i]))continue; tmp.emplace_back(nums[i]); help(nums,res,tmp,index + 1); tmp.pop_back(); } } } vector<vector<int>>permute(vector<int>& nums) { vector<vector<int>>res; vector<int>tmp; sort(nums.begin(),nums.end()); help(nums,res,tmp,0); return res; } void help(vector<int>& nums,vector<vector<int>>& res,vector<int>& tmp,int index, vector<bool>& vis) { if(tmp.size() == nums.size())res.emplace_back(tmp); else { for(int i = 0; i < nums.size(); ++i) { if(vis[i] || i > 0 && nums[i] == nums[i-1]&& !vis[i-1])continue; vis[i] = true; tmp.emplace_back(nums[i]); help(nums,res,tmp,index + 1, vis); vis[i] = false; tmp.pop_back(); } } } vector<vector<int>>permuteUnique(vector<int>& nums) { vector<vector<int>>res; vector<int>tmp; sort(nums.begin(),nums.end()); vector<bool>vis(nums.size(),false); help(nums,res,tmp,0,vis); return res; } void help(vector<vector<int>>& res,vector<int>&tmp,int index,int n, int k) { if(tmp.size() + ( n - index + 1) < k) { return; } if(tmp.size() == k) { res.emplace_back(tmp); return ; } tmp.emplace_back(index); help(res,tmp,index+1,n,k); tmp.pop_back(); help(res,tmp,index+1,n,k); return ; } vector<vector<int>>combine(int n, int k) { vector<vector<int>>res; vector<int>tmp; help(res,tmp,1,n,k); return res; } void print(const vector<vector<string>>&board) { for(auto strs : board) { for(auto str:strs) { cout << str << " "; } cout << endl; } } bool isValid(vector<vector<string>>&board,int i, int j, char c) { for(int row = 0; row < 9; ++row) { if(board[row][j][0] == c)return false; } for(int col = 0; col < 9; ++col) { if(board[i][col][0] == c)return false; } int rowIndex = (i / 3) * 3,colIndex = (j / 3) * 3; for(int m = rowIndex; m < rowIndex + 3; ++m) { for(int n = colIndex; n < colIndex + 3; ++n) { if(board[m][n][0]==c)return false; } } return true; } bool solve(vector<vector<string>>&board) { int m = board.size(),n = board[0].size(); for(int i = 0; i < m; ++i) { for(int j = 0;j < n; ++j) { if(board[i][j] == ".") { for(char c = '1'; c <= '9'; ++c) { if(isValid(board,i,j,c)) { board[i][j][0] = c; if(solve(board))return true; else board[i][j] = "."; } } return false; } } } return true; } vector<vector<string>> solveSudoku(vector<vector<string>>& board) { solve(board); return board; } vector<string>construct(vector<vector<char>>&board) { vector<string>res; for(int i = 0; i < board.size(); ++i) { string s = ""; for(int j = 0; j < board[i].size(); ++j) s.push_back(board[i][j]); res.emplace_back(s); } return res; } bool valid(vector<vector<char>>&board,int x,int y) { for(int i = 0; i < board.size(); ++i) { for(int j = 0; j < y; ++j) { if(board[i][j] == 'Q' &&((x+j) == (y + i) || (x == i) || (x + y) == (i + j))) return false; } } return true; } void dfs(vector<vector<char>>&board,int colIndex,vector<vector<string>>& res) { if(colIndex == board.size()) { res.emplace_back(construct(board)); return; } for(int i = 0; i < board.size(); ++i) { if(valid(board,i,colIndex)) { board[i][colIndex] = 'Q'; dfs(board,colIndex + 1,res); board[i][colIndex] = '.'; } } } vector<vector<string>>Nqueens(int n) { vector<vector<char>>board(n,vector<char>(n,'.')); vector<vector<string>>res; dfs(board,0,res); return res; } int g_res = INT_MAX; int getMax(const vector<int>& sum) { int res = INT_MIN; for(auto num : sum) { res = max(res,num); } return res; } void dfs(const vector<int>& jobs,int pos,vector<int>& sum) { if(pos < 0) { g_res = min(g_res,getMax(sum)); return; } if(getMax(sum)>=g_res)return; for(int i = 0; i < sum.size(); ++i) { if(i > 0 && sum[i] == sum[i-1])continue; sum[i] += jobs[pos]; dfs(jobs,pos-1,sum); sum[i] -= jobs[pos]; } } int minTimerequired(vector<int>&jobs,int k) { sort(jobs.begin(),jobs.end()); int n = jobs.size(); vector<int>v(k); dfs(jobs,n-1,v); return g_res; } int b_res = 0; void dfs(const vector<int>&tasks,int taskId, int sessionCnt,vector<int>& sessions,int sessionTime) { if(sessionCnt > b_res)return; if(taskId < 0) { b_res = min(b_res,sessionCnt); return; } for(int i = 0; i < sessionCnt; ++i) { if(sessions[i] + tasks[taskId] < sessionTime) { sessions[i] += tasks[taskId]; dfs(tasks,taskId - 1, sessionCnt,sessions,sessionTime); sessions[i] -= tasks[taskId]; } } sessions[sessionCnt] += tasks[taskId]; dfs(tasks,taskId-1,sessionCnt + 1, sessions,sessionTime); sessions[sessionCnt] -= tasks[taskId]; } int minSessions(vector<int>& tasks, int sessionTime) { sort(tasks.begin(),tasks.end()); vector<int>sessions(tasks.size()); b_res = tasks.size(); dfs(tasks,tasks.size()-1,0,sessions,sessionTime); return b_res; } int main() { //LeetCode94 TreeNode n0(1); TreeNode n1(2); TreeNode n2(3); n0.right = &n1; n1.left = &n2; print(inorder(&n0)); //LeetCode78 vector<int>nums{1,2,3}; print(subset(nums)); //LeetCode90 nums = {1,2,2}; print(subsetII(nums)); // LeetCode46 nums = {1,2,3}; print(permute(nums)); //LeetCode47 cout << "======" << endl; nums = {1,1,2}; print(permuteUnique(nums)); //LeetCode77 cout << "======" << endl; int n = 4, k = 2; print(combine(n,k)); //LeetCode37 cout << "======" << endl; vector<vector<string>>grid= {{"5","3",".",".","7",".",".",".","."}, {"6",".",".","1","9","5",".",".","."}, {".","9","8",".",".",".",".","6","."}, {"8",".",".",".","6",".",".",".","3"}, {"4",".",".","8",".","3",".",".","1"}, {"7",".",".",".","2",".",".",".","6"}, {".","6",".",".",".",".","2","8","."}, {".",".",".","4","1","9",".",".","5"}, {".",".",".",".","8",".",".","7","9"}}; print(solveSudoku(grid)); //LeetCode51 n = 4; print(Nqueens(n)); //LeetCode1723 vector<int>jobs{1,2,4,7,8}; k = 2; cout << minTimerequired(jobs,k) << endl; //LeetCode1986 vector<int>tasks{3,1,3,1,1}; int sessionTime = 8; cout << minSessions(tasks,sessionTime) << endl; return 0; }