Advertisement
aqibm

Untitled

Apr 8th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. class RLE{
  2.  
  3. public:
  4.  
  5. vector<vector<int>> encode(vector<int>& input){
  6. int sizeInput = input.size();
  7. int prev = -1;
  8. int count = 0;
  9. vector<vector<int>> ans(3);
  10. for(int index = 0;index<sizeInput;index++){
  11. if(input[index]==prev){
  12. count++;
  13. }
  14. else{
  15. if(prev!=-1){
  16. ans[0].push_back(prev);
  17. ans[1].push_back(count);
  18. ans[2].push_back(index);
  19. }
  20. prev = input[index];
  21. count = 1;
  22. }
  23. }
  24. }
  25. ans[2].push_back(sizeInput);
  26. ans.push_back({prev,count});
  27. return ans;
  28. }
  29.  
  30. int getValueAt(vector<vector<int>>& encodedInput, int index){
  31. // binary search on encodedInput[2]
  32. index += 1;
  33. int indexInEncoded = lower_bound(encodedInput[2].begin(),encodedInput[2].end(),index) - encodedInput[2].begin();
  34. return encodedInput[0][indexInEncoded];
  35.  
  36. }
  37.  
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement