— Diary, Programming — 1 min read
I'm not supposed to be working right now and I should probably sleep but I just got an awesome idea. Make a discord bot that uses a playlist to feed to groovy. Why can't I play my liked songs on discord? 😢
I started working on Leetcode Challenge for 14th. Used sliding window to get the result. Time complexity O(n)
and space complexity O(1)
.
1int minOperations(vector<int> &nums, int x) {2 int sum = accumulate(begin(nums), end(nums), 0);3 int l = 0, r = 0, res = INT_MAX, sz = nums.size();4 while (l <= r)5 if (sum >= x) {6 if (sum == x)7 res = min(res, l + sz - r);8 if (r < sz)9 sum -= nums[r++];10 else11 break;12 }13 else14 sum += nums[l++];15 return res == INT_MAX ? -1 : res;16}
Now back on today's Leetcode Challenge. The problem seems simple with some basic arithmetic. Just get the nth integer with loop.
1int getMaximumGenerated(int n)2{3 if (n <= 1)4 return n;5 int arr[n + 1];6 arr[0] = 0;7 arr[1] = 1;8 int m = 1;9 for (int i = 2; i <= n; i++) {10 if (i % 2 == 0) {11 arr[i] = arr[i / 2];12 } else {13 arr[i] = arr[i / 2] + arr[(i / 2) + 1];14 }15 m = max(m, arr[i]);16 }17 return m;18}
Time complexity O(n)
and space O(1)
break
Not gonna work tonight, need a break.
Goodnight!