はじめに

この記事は「Eureka Advent Calendar 2020」14日目の記事です。

前日の記事は同じSRE Team の@marnie さんでCloudfront+Lambda@edgeを利用してサーバーレスに画像をリサイズ・変換して返却するCDNを構築したお話でした。

こんにちは、はじめまして、もしくはお久しぶりです。今年の3月からeureka SREチームでエンジニアをやっている @fukubaka0825 a.k.a nari です。

最近はフルリモートで働けることもあり、いったん1年半くらい住んでいたギークハウスというシェアハウスから離れてgoodroom ホテルパスつかってワーケーションを満喫したり、Oculus quest 2買ってfitxrで運動解消したり、巷で話題の …


Description

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.

Example:

int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3); // returns 4
kthLargest.add(5); // returns 5
kthLargest.add(10); // returns 5
kthLargest.add(9); // returns 8
kthLargest.add(4); …

Description

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

Solution

/* Approach 1: Stacks */
func makeParMap() map[string]string{
parMap := make(map[string]string)
parMap["}"] = "{"
parMap["]"] = "["
parMap[")"] = "("
return parMap…

Description

Given a linked list, determine if it has a cycle in it.

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Description

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

Solution

/*Go Approach 1: Horizontal scanning*/
/*Time complexity : O(S), where S is the sum of all characters in all strings.*/
/*Space complexity : O(1). We only used constant extra space. */
func longestCommonPrefix(strs []string) string {
if len(strs) == 0 {
return ""
}
// Assume prefix
prefix := strs[0]
for i := 1; i < len(strs); i++ {
for !strings.HasPrefix(strs[i], prefix) {
prefix = prefix[0:len(prefix)-1]
if len(prefix) == 0 {
return ""
}
}
}
return prefix
}

Description

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

Solution

At first, I answered with the following codes. ( It is the same method as I used in solving the 7. Reverse Integer. related article: https://medium.com/@fukubaka0825/go-leetcode-7-reverse-integer-eaba9692c9a


Description

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Solution

This solution code includes 3 steps.

  1. pop operation
  2. check beforehand whether or not appending push ope would cause overflow.
  3. push operation
/* [Go] Approach 1: Pop and Push Digits & Check before Overflow */
//MaxInt32 == 32767
func reverse(x int) int {
var rev int
for x != 0 {
//1.pop operation
pop := x % 10
x /= 10

//2.check beforehand whether or not appending push ope would cause overflow.
if ( rev > math.MaxInt32/10 || (rev == math.MaxInt32/10 && pop > 7)){
return 0
}
if ( rev < math.MinInt32/10 || (rev == math.MinInt32/10 && pop < -8)){
return 0
}

//3. …

Description

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution

/* [Go]  1.Brute Force O(n2) */
func twoSum(nums []int, target int) []int {
for i := 0; i < len(nums); i++{
for j := i + 1; j < len(nums); j++{
if nums[j] == target - nums[i]{
return []int{i,j}
}
}
}
return nil…

Image for post
Image for post
by Renée French CC BY 3.0

At First

Hi, everyone.

My name is Takashi Narikawa.(twitter:@fukubaka0825)

I work as a software engineer at Wano Co., Ltd.

This is my first post on medium.

This time, I would like to leave a memorandum about the trouble occurred when I install Go into the alpine image.

TL;DR

  • If you want to install Go on an alpine-based image, use apk instead of wget or curl command

What I wanted to do

  • I want to auto-deploy lambda with apex command on codebuild (CI) with hashicorp / terraform image
  • because I wrote lambda script in Go language, it is necessary to install Go in addition to the hashicorp / terraform image . …

About

TAKASHI NARIKAWA

Site Reliablity Engineer in Tokyo. SRE/devops/Go/AWS/Terraform

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store