Task Arrangement -- incomplete
来源:互联网 发布:淘宝联盟定金返佣 编辑:程序博客网 时间:2024/06/05 17:21
Task schedule: Given a sequence of task like A B C, and a coldtime, which means you need to wait for that much time to start next[same] task. Now---
Input: string , n
Output: the best task finishing sequence.
Eg" Input: AAABBB, 2
Output: AB_AB_AB ("_" means do nothing and wait.) Source: 点击打开链接
Maybe, first see a problem easier than this one. (This version was terrible wrong!)
#include <string>#include <iostream>#include <unordered_map>#include <vector>using namespace std;/* Give a task and some cool down time k (same task cool down distance). calcualte the total sum of time to finish the task.*/int totalTime(string str) { int count = str.size(); unordered_map<char, int> charToIndex; for(int i = 0; i < str.size(); ++i) { char tmp = str[i]; char index = i; if(charToIndex.find(tmp) == charToIndex.end()) { charToIndex.insert({tmp, index}); continue; } else { if(index - charToIndex[tmp] > 2) { charToIndex[tmp] = index; continue; } else { if(index - charToIndex[tmp] == 1) count += 2; else count += 1; charToIndex[tmp] = index; } } } return count;}int main(void) { cout << totalTime("AABCB") << endl;}
The index need to be shifted once found one repeation.
// task cool down time k#include "header.h"using namespace std;int taskCoolDown(vector<int>& nums, int k) { int totalTime = 0; int shift = 0; unordered_map<int, int> timeToIndex; for(int i = 0; i < nums.size(); ++i) { if(timeToIndex.find(nums[i]) == timeToIndex.end()) { timeToIndex.insert({nums[i], i + shift}); totalTime++; } else { int tmp = timeToIndex[nums[i]]; if(k + 1 - (i + shift - tmp) > 0) totalTime += k + 2 - (i + shift - tmp); else totalTime += 1; if(k + 1 - (i - tmp) > 0) shift += k + 1 - (i - tmp); else shift = 0; timeToIndex[nums[i]] = i + shift; } } return totalTime;}int main(void) { vector<int> nums{1, 1, 2, 1}; vector<int> nums_1{1, 2, 3, 1, 2, 3}; cout << taskCoolDown(nums, 2) << endl;}
0 0
- Task Arrangement -- incomplete
- Arrangement
- Interview Arrangement
- Interview Arrangement
- Problem Arrangement
- Instrction Arrangement
- Incomplete Chessboard
- TASK
- Task
- Task
- TASK
- task
- Task
- task
- Task,
- task
- Task
- task
- 一些关于NewSql的笔记(DTCC2016)
- python中sort与sorted区别
- 处理APP字体跟随系统字体大小改变的问题
- Java 内存分配策略
- JavaScript 各种遍历方式详解,有你不知道的黑科技
- Task Arrangement -- incomplete
- 这些地方你不需要使用 JavaScript
- 使用jquery 的deferred 报错
- argc和argv在main函数中的应用,及unistd.h
- ORACLE 跟踪文件详细解释
- Android中Button的基本用法
- iOS 代码命名规范 及Android 代码命名规范(1)iOS
- List的深度克隆
- 基数树(radix tree)