[{"data":1,"prerenderedAt":260},["ShallowReactive",2],{"\u002Fblog\u002Fcode-log-day-14":3},{"id":4,"title":5,"author":6,"body":7,"date":244,"description":245,"draft":246,"extension":247,"image":248,"language":249,"meta":250,"navigation":134,"path":251,"seo":252,"stem":253,"tags":254,"__hash__":259},"blog\u002Fblog\u002FCode Log - Day 14.md","Code Log - Day 14","Caio Prado",{"type":8,"value":9,"toc":238},"minimark",[10,15,27,32,43,63,94,224,227,231,234],[11,12,14],"h2",{"id":13},"log","Log",[16,17,18,19,26],"p",{},"Today i finished a LeetCode problem from the ",[20,21,25],"a",{"href":22,"rel":23},"https:\u002F\u002Fleetcode.com\u002Fstudyplan\u002Ftop-interview-150\u002F",[24],"nofollow","Top 150 Interview Questions"," list and an assignment from my university.",[28,29,31],"h3",{"id":30},"leetcode","LeetCode",[16,33,34,35,39,40,42],{},"The LeetCode exercise i chose was: Rotate Array.\nIt requires a function that receive as input an integer array and their ",[36,37,38],"code",{},"k"," number of rotations to the right  rotate the array to the right by ",[36,41,38],{}," steps :",[44,45,46],"blockquote",{},[16,47,48,49,52,53,55,56,58,59,62],{},"Given an integer array ",[36,50,51],{},"nums",", rotate the array to the right by ",[36,54,38],{}," steps, where ",[36,57,38],{}," is non-negative. Could you do it in-place with ",[36,60,61],{},"O(1)"," extra space?",[16,64,65,66,68,69,71,72,74,75,80,81,83,84,87,88,90,91,93],{},"My approach was to subtract k from the array length and use the result as the number of digits in the end of the array that would be swapped with the ",[36,67,38],{}," first ones, and, then, i would \"sort\" the ones in the beginning that now are at the end of the array. However, this solution didn't work for some test cases, due to the subtraction of ",[36,70,38],{}," from the array length (when ",[36,73,38],{}," > length). An alternative, that works well and can be found ",[20,76,79],{"href":77,"rel":78},"https:\u002F\u002Fleetcode.com\u002Fproblems\u002Frotate-array\u002F?envType=study-plan-v2&envId=top-interview-150",[24],"here",", rotates de array by reversing all item and using a new ",[36,82,38],{}," value given by ",[36,85,86],{},"k % nums.length"," as a index to reverse the values before the new ",[36,89,38],{}," and then then values after the new ",[36,92,38],{},":",[95,96,101],"pre",{"className":97,"code":98,"language":99,"meta":100,"style":100},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","class Solution {\n    public void rotate(int[] nums, int k) {\n        \u002F\u002F ex.: nums = [1,2,3,4,5,6,7], k = 11\n        k %= nums.length; \u002F\u002F k = 11 % 7 = 4\n\n        reverse(nums, 0, nums.length - 1); \u002F\u002F [1,2,3,4,5,6,7] -> [7,6,5,4,3,2,1]\n        reverse(nums, 0, k - 1); \u002F\u002F [7,6,5,4,3,2,1] -> [4,5,6,7,3,2,1]\n        reverse(nums, k, nums.length - 1); \u002F\u002F [4,5,6,7,3,2,1] -> [4,5,6,7,1,2,3]\n    }\n\n    private void reverse(int[] nums, int left, int right) {\n        while (left \u003C right) {\n            int temp = nums[left];\n            nums[left] = nums[right];\n            nums[right] = temp;\n            left++;\n            right--;\n        }\n    }\n}\n","java","",[36,102,103,111,117,123,129,136,142,148,154,160,165,171,177,183,189,195,201,207,213,218],{"__ignoreMap":100},[104,105,108],"span",{"class":106,"line":107},"line",1,[104,109,110],{},"class Solution {\n",[104,112,114],{"class":106,"line":113},2,[104,115,116],{},"    public void rotate(int[] nums, int k) {\n",[104,118,120],{"class":106,"line":119},3,[104,121,122],{},"        \u002F\u002F ex.: nums = [1,2,3,4,5,6,7], k = 11\n",[104,124,126],{"class":106,"line":125},4,[104,127,128],{},"        k %= nums.length; \u002F\u002F k = 11 % 7 = 4\n",[104,130,132],{"class":106,"line":131},5,[104,133,135],{"emptyLinePlaceholder":134},true,"\n",[104,137,139],{"class":106,"line":138},6,[104,140,141],{},"        reverse(nums, 0, nums.length - 1); \u002F\u002F [1,2,3,4,5,6,7] -> [7,6,5,4,3,2,1]\n",[104,143,145],{"class":106,"line":144},7,[104,146,147],{},"        reverse(nums, 0, k - 1); \u002F\u002F [7,6,5,4,3,2,1] -> [4,5,6,7,3,2,1]\n",[104,149,151],{"class":106,"line":150},8,[104,152,153],{},"        reverse(nums, k, nums.length - 1); \u002F\u002F [4,5,6,7,3,2,1] -> [4,5,6,7,1,2,3]\n",[104,155,157],{"class":106,"line":156},9,[104,158,159],{},"    }\n",[104,161,163],{"class":106,"line":162},10,[104,164,135],{"emptyLinePlaceholder":134},[104,166,168],{"class":106,"line":167},11,[104,169,170],{},"    private void reverse(int[] nums, int left, int right) {\n",[104,172,174],{"class":106,"line":173},12,[104,175,176],{},"        while (left \u003C right) {\n",[104,178,180],{"class":106,"line":179},13,[104,181,182],{},"            int temp = nums[left];\n",[104,184,186],{"class":106,"line":185},14,[104,187,188],{},"            nums[left] = nums[right];\n",[104,190,192],{"class":106,"line":191},15,[104,193,194],{},"            nums[right] = temp;\n",[104,196,198],{"class":106,"line":197},16,[104,199,200],{},"            left++;\n",[104,202,204],{"class":106,"line":203},17,[104,205,206],{},"            right--;\n",[104,208,210],{"class":106,"line":209},18,[104,211,212],{},"        }\n",[104,214,216],{"class":106,"line":215},19,[104,217,159],{},[104,219,221],{"class":106,"line":220},20,[104,222,223],{},"}\n",[225,226],"hr",{},[28,228,230],{"id":229},"assignments","Assignments",[16,232,233],{},"The assignment was from the Distributed System class and it required to build a chat app using MQTTv5. It basically needed to send messages to an specified \"topic\" or subscribe to one. For this, the Eclipse Paho provides a publish and subscribe methods, so most part of the work was on the input string parsing.",[235,236,237],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":100,"searchDepth":113,"depth":113,"links":239},[240],{"id":13,"depth":113,"text":14,"children":241},[242,243],{"id":30,"depth":119,"text":31},{"id":229,"depth":119,"text":230},"2025-01-22","Solving LeetCode's Rotate Array and finishing the MQTTv5 chat app assignment with Eclipse Paho.",false,"md",null,"en",{},"\u002Fblog\u002Fcode-log-day-14",{"title":5,"description":245},"blog\u002FCode Log - Day 14",[255,256,30,99,257,258],"challenge","daily-coding","mqtt","docker","rWRZVKQvssByZp1ITYpbZ6oadelB7cBiBT204slgrCg",1785357633431]