본문 바로가기

전체 글67

621. Task Scheduler class Solution { public: int leastInterval(vector& tasks, int n) { vector v(26); int freq = 0; for (auto c : tasks) freq = max(++v[c - 'A'], freq); int count = 0; for (int i = 0; i < v.size(); ++i) if (v[i] == freq) count++; int with_idle = (n + 1) * (freq - 1) + count; int no_idle = tasks.size(); return max(with_idle, no_idle); } }; max frequency tasks를 제외한 tasks에 대해, - (n + 1) 슬롯에서의 idle time을.. 2022. 8. 23.
437. Path Sum III /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int ans; void f(TreeNode* root, int targetSum, lon.. 2022. 8. 14.
1706. Where Will the Ball Fall class Solution { public: vector findBall(vector& grid) { int n = grid.size(); int m = grid[0].size(); vector ans(m); for (int i = 0; i < m; ++i) ans[i] = i; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int cur = ans[j]; if (cur == -1) continue; int next = cur + grid[i][cur]; if (next < 0 || m == next || grid[i][next] != grid[i][cur]) { ans[j] = -1; continue; } ans[j] = next; } } r.. 2022. 7. 31.
234. Palindrome Linked List /** * Definition for singly-linked list. * 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 *reverse(ListNode *cur) { ListNode *prev = nullptr; while (cur) { ListNode *next = cur->next; cur->next = prev; prev = cur;.. 2022. 7. 30.