diff --git a/InSange/README.md b/InSange/README.md index 6e73fbf..48462ff 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -29,6 +29,10 @@ | 25차시 | 2024.08.10 | BFS | [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [#25](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/87)] | 26차시 | 2024.08.11 | 수학 | [Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/) | [#26](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/89)] | 27차시 | 2024.08.17 | 문자열 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens/) | [#27](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/91)] +| 28차시 | 2024.08.21 | 백트래킹 | [월드컵](https://www.acmicpc.net/problem/6987) | [#28](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/94)] +| 29차시 | 2024.08.25 | 문자열 | [Find the Closest Palindrome](https://leetcode.com/problems/find-the-closest-palindrome/) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/98)] +| 30차시 | 2024.09.06 | 문자열 | [Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) | [#30](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/100)] --- https://leetcode.com/problems/robot-collisions/ +Find the Closest Palindrome \ No newline at end of file diff --git "a/InSange/\353\254\270\354\236\220\354\227\264/564_Find the Closest Palindrome.cpp" "b/InSange/\353\254\270\354\236\220\354\227\264/564_Find the Closest Palindrome.cpp" new file mode 100644 index 0000000..825ee9d --- /dev/null +++ "b/InSange/\353\254\270\354\236\220\354\227\264/564_Find the Closest Palindrome.cpp" @@ -0,0 +1,74 @@ +#include +#include + +using namespace std; + +class Solution { +public: + long long Convert(long long& num) + { + string s = to_string(num); + int n = s.length(); + int l = (n - 1) / 2; + int r = n / 2; + while (l >= 0) s[r++] = s[l--]; + return stoll(s); + } + + long long UpPal(long long num) + { + long long left = 0; + long long right = num; + long long ans = INT_MIN; + + while (left <= right) + { + long long mid = (right + left) / 2; + long long palin = Convert(mid); + if (palin < num) + { + ans = palin; + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + return ans; + } + + long long DownPal(long long num) + { + long long left = num; + long long right = 1e18; + long long ans = INT_MIN; + + while (left <= right) { + long long mid = (right + left) / 2; + long long palin = Convert(mid); + if (palin > num) + { + ans = palin; + right = mid - 1; + } + else + { + left = mid + 1; + } + } + + return ans; + } + + string nearestPalindromic(string n) { + long long num = stoll(n); + long long a = UpPal(num); + long long b = DownPal(num); + + if (abs(a - num) <= abs(b - num)) return to_string(a); + + return to_string(b); + } +}; \ No newline at end of file diff --git "a/InSange/\353\260\261\355\212\270\353\236\230\355\202\271/6987.cpp" "b/InSange/\353\260\261\355\212\270\353\236\230\355\202\271/6987.cpp" new file mode 100644 index 0000000..2c52416 --- /dev/null +++ "b/InSange/\353\260\261\355\212\270\353\236\230\355\202\271/6987.cpp" @@ -0,0 +1,91 @@ +#include +#include + +using namespace std; + +vector> record; +int t; +bool draw_flag, flag; +vector> game = { {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, + {1, 2}, {1, 3}, {1, 4}, {1, 5}, + {2, 3}, {2, 4}, {2, 5}, + {3, 4}, {3, 5}, + {4, 5} }; + +bool CheckPlay(int round) +{ + if (round == 15) + { + for (int i = 0; i < 6; i++) + { + for (int j = 0; j < 3; j++) + { + if (record[i][j]) return false; + } + } + return true; + } + + int firstTeam, secondTeam; + firstTeam = game[round].first; + secondTeam = game[round].second; + + if (record[firstTeam][0] && record[secondTeam][2]) // first team win, second team lose + { + --record[firstTeam][0]; + --record[secondTeam][2]; + if (CheckPlay(round + 1)) return true; + ++record[firstTeam][0]; + ++record[secondTeam][2]; + } + + if (record[firstTeam][1] && record[secondTeam][1]) // first team draw, second team draw + { + --record[firstTeam][1]; + --record[secondTeam][1]; + if (CheckPlay(round + 1)) return true; + ++record[firstTeam][1]; + ++record[secondTeam][1]; + } + + if (record[firstTeam][2] && record[secondTeam][0]) // first team lose, second team win + { + --record[firstTeam][2]; + --record[secondTeam][0]; + if (CheckPlay(round + 1)) return true; + ++record[firstTeam][2]; + ++record[secondTeam][0]; + } + + return false; +} + +void Solve() +{ + record.assign(6, vector(3, 0)); + t = 4; + + while (t--) + { + for (int i = 0; i < 6; i++) + { + for (int j = 0; j < 3; j++) + { + cin >> record[i][j]; + } + } + + if (CheckPlay(0)) cout << 1 << " "; + else cout << 0 << " "; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + Solve(); + + return 0; +} \ No newline at end of file diff --git "a/InSange/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/3217_Delete Nodes From Linked List Present in Array.cpp" "b/InSange/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/3217_Delete Nodes From Linked List Present in Array.cpp" new file mode 100644 index 0000000..7062927 --- /dev/null +++ "b/InSange/\354\227\260\352\262\260 \353\246\254\354\212\244\355\212\270/3217_Delete Nodes From Linked List Present in Array.cpp" @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + + +struct ListNode { + int val; + ListNode* next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode* next) : val(x), next(next) {} +}; + +class Solution { +public: + ListNode* modifiedList(vector& nums, ListNode* head) { + ListNode* removeList = new ListNode(); + ListNode* removeHead; + unordered_map um; + + for (int& val : nums) um[val] = true; + + removeList->next = head; + removeHead = removeList; + + while (removeHead->next) + { + if (um[removeHead->next->val] == true) removeHead->next = removeHead->next->next; + else removeHead = removeHead->next; + } + + return removeList->next; + } +}; \ No newline at end of file