Skip to content
Paras Gupta

Jan 15 - Daily Programming Diary

Diary, Programming1 min read

2:00

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? 😢

12:00

I started working on Leetcode Challenge for 14th. Used sliding window to get the result. Time complexity O(n) and space complexity O(1).

minimum-operations-to-reduce-x-to-zero.cpp
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 else
11 break;
12 }
13 else
14 sum += nums[l++];
15 return res == INT_MAX ? -1 : res;
16}
15:40

Now back on today's Leetcode Challenge. The problem seems simple with some basic arithmetic. Just get the nth integer with loop.

get-maximum-in-generated-array.cpp
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

22:30

Not gonna work tonight, need a break.

Goodnight!

EOD Checklist

  • Leetcode January Challenge
  • Raahee
  • This blog
© 2022 by Paras Gupta. All rights reserved.
Theme by LekoArts