Oddbean new post about | logout
 Fast food is expensive both now and later. 

#food #health #inflation

nostr:note10q2e77hw0cjdatmte9hql075ykruul0y4ds7m3jr2zpyge2twq2s6qmkgu 
 213. Two Sum
# @Time complexity: O(n)
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> mp;
        for(auto i=0;i<nums.size();i++){
            if(mp.find(target-nums[i])!=mp.end()){
                return {mp[target-nums[i]],i};
            }
            else{
                mp[nums[i]]=i;
            }
        }
        return {};
    }
}; 
 




#include "common.h"
#include "farm.h"

//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------

bool farm_has_crop(int i)
{
	return (farm_info[i].type == FARM_TYPE_CROP);
}

//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void farm_show_all_crops()
{
	int i;
	for (i = 0; i < MAX_FARMS; i++) {
		if (farm_has_crop(i)) {
			print(FARM_COLORS, "%d - %s", i, farm_info[i].type);
		}
	}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void farm_change_crop(int i, int new_crop)
{
	if (farm_has_crop(i)) {
		print(FARM_COLORS, "Changing crop on farm %d from %s to %s.", i,
			farm_info[i].type, get_crop_name(new_crop));
		if (farm_info[i].type == FARM_TYPE_CROP) {
			farm_info[i].harvest = harvest_table[get_crop_harvest(new_crop)];
		}

		farm_info[i].type = new_crop;
		if (farm_info[i].type == FARM_TYPE_CROP) {
			if (farm_info[i].harvest < 0) {
				farm_info[i].harvest = harvest_table[get_crop_harvest(new_crop)];
			} else {
				print(FARM_COLORS, "Farm %d will have a %s harvest.
", i,
					get_crop_harvest_name(farm_info[i].harvest));
			}
			farm_info[i].growth = get_crop_growth(new_crop);
			farm_info[i].planting = false;
			if (farm_info[i].growth > 0) {
				print(FARM_COLORS, "Farm %d will grow crops.
", i);
			} else {
				print(FARM_COLORS, "Farm %d will not grow crops.
", i);
			}
		}
	} else if (farm_info[i].type == FARM_TYPE_RAIN) {
		print(FARM_COLORS, "Changing rain farm %d.", i);
		farm_info[i].type = NEW_FARM_TYPE;
		print(FARM_COLORS, "to new type.
");
	}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void farm_add_crop(int i, int crop)
{
	if (farm_has_crop(i)) {
		print(FARM_COLORS, "Farm %d already has a crop.
", i);
	} else {
		print(FARM_COLORS, "Adding crop to farm %d.", i);
		farm_info[i].type = crop;
		if (crop == FARM_TYPE_CROP) {
			print(FARM_COLORS, "Planting.
");
			farm_info[i].harvest = harvest_table[get_crop_harvest(crop)];
			if (farm_info[i].harvest < 0) {
				farm_info[i].harvest = harvest_table[get_crop_harvest(crop)];
			} else {
				print(FARM_COLORS, "Farm %d will have a %s harvest.
", i,
					get_crop_harvest_name(farm_info[i].harvest));
			}
			farm_info[i].growth = get_crop_growth(crop);
			print(FARM_COLORS, "Planting.
");
			if (farm_info[i].growth > 0) {
				print(FARM_COLORS, "Farm %d will grow crops.
", i);
			} else {
				print(FARM_COLORS, "Farm %d will not grow crops.
", i);
			}
		} else if (crop == FARM_TYPE_RAIN) {
			farm_info[i].type = crop;
			print(FARM_COLORS, "Rain farm.
");
		} else {
			print(FARM_COLORS, "Invalid type.
");
		}
	}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void farm_add_rain(int i)
{
	if (farm_info[i].type == FARM_TYPE_RAIN) {
		print(FARM_COLORS, "Rain farm %d already exists.
", i);
	} else if (farm_info[i].type != FARM_TYPE_CROP && farm_info[i].type != FARM_TYPE_RAIN) {
		print(FARM_COLORS, "Invalid type.
");
	} else {
		print(FARM_COLORS, "Adding rain farm to farm %d.", i);
		farm_info[i].type = FARM_TYPE_RAIN;
		print(FARM_COLORS, "Rain farm.
");
	}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
int farm_get_crop(int i)
{
	if (farm_has_crop(i)) {
		return farm_info[i].type;
	} else if (farm_info[i].type == FARM_TYPE_RAIN) {
		print(FARM_COLORS, "Invalid type.
");
	} else {
		print(FARM_COLORS, "No crop.
");
	}
	return -1;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void farm_update()
{
	int i;
	bool changed = false;
	for (i = 0; i < MAX_FARMS; i++) {
		if (!farm_has_crop(i)) continue;

		if (farm_info[i].planting) {
			int days = farm_info[i].days + 1;
			if (days > get_crop_growth(farm_info[i].type)) {
				print(FARM_COLORS, "Planting complete.
");
				changed = true;
				farm_info[i].planting = false;
				if (farm_info[i].type == FARM_TYPE_CROP) {
					print(FARM_COLORS, "Harvesting.
");
					int harvest_days = farm_info[i].harvest;
					if (farm_info[i].type == FARM_TYPE_CROP && !farm_info[i].planting) {
						print(FARM_COLORS, "Planting.
");
					} else {
						if (days > harvest_days) {
							print(FARM_COLORS, "Harvesting.
");
						} else if (days >= 0) {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "No harvest.
");
						} else {
							print(FARM_COLORS, "Harvesting.
");
						}
						int days_left = harvest_days - days;
						if (days_left > 0) {
							print(FARM_COLORS, "Days left: %d.
", days_left);
						} else if (days_left == 0) {
							if (farm_info[i].type == FARM_TYPE_CROP && farm_info[i].harvest != -1) {
								changed = true;
								print(FARM_COLORS, "Harvesting.
");
								farm_info[i].planting = true;
								int days_left = get_crop_growth(farm_info[i].type);
								print(FARM_COLORS, "Days left: %d.
", days_left);
							} else {
								changed = true;
								farm_info[i].planting = false;
								print(FARM_COLORS, "No crop.
");
							}
						} else {
							changed = true;
							farm_info[i].planting = false;
							print(FARM_COLORS, "Harvested.
");
						}
						if (days > harvest_days) {
							print(FARM_COLORS, "Planting.
");
						} else if (days >= 0 && days < harvest_days) {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "No harvest.
");
						} else {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "Harvested.
");
						}
						int days_left = harvest_days - days;
						if (days_left > 0) {
							print(FARM_COLORS, "Days left: %d.
", days_left);
						} else if (days_left == 0) {
							if (farm_info[i].type == FARM_TYPE_CROP && farm_info[i].harvest != -1) {
								changed = true;
								print(FARM_COLORS, "Harvesting.
");
								farm_info[i].planting = true;
								int days_left = get_crop_growth(farm_info[i].type);
								print(FARM_COLORS, "Days left: %d.
", days_left);
							} else {
								changed = true;
								farm_info[i].planting = false;
								print(FARM_COLORS, "No crop.
");
							}
						} else {
							changed = true;
							farm_info[i].planting = false;
							print(FARM_COLORS, "Harvested.
");
						}
						if (days > harvest_days) {
							print(FARM_COLORS, "Planting.
");
						} else if (days >= 0 && days < harvest_days) {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "No harvest.
");
						} else {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "Harvested.
");
						}
						int days_left = harvest_days - days;
						if (days_left > 0) {
							print(FARM_COLORS, "Days left: %d.
", days_left);
						} else if (days_left == 0) {
							if (farm_info[i].type == FARM_TYPE_CROP && farm_info[i].harvest != -1) {
								changed = true;
								print(FARM_COLORS, "Harvesting.
");
								farm_info[i].planting = true;
								int days_left = get_crop_growth(farm_info[i].type);
								print(FARM_COLORS, "Days left: %d.
", days_left);
							} else {
								changed = true;
								farm_info[i].planting = false;
								print(FARM_COLORS, "No crop.
");
							}
						} else {
							changed = true;
							farm_info[i].planting = false;
							print(FARM_COLORS, "Harvested.
");
						}
						if (days > harvest_days) {
							print(FARM_COLORS, "Planting.
");
						} else if (days >= 0 && days < harvest_days) {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "No harvest.
");
						} else {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "Harvested.
");
						}
						int days_left = harvest_days - days;
						if (days_left > 0) {
							print(FARM_COLORS, "Days left: %d.
", days_left);
						} else if (days_left == 0) {
							if (farm_info[i].type == FARM_TYPE_CROP && farm_info[i].harvest != -1) {
								changed = true;
								print(FARM_COLORS, "Harvesting.
");
								farm_info[i].planting = true;
								int days_left = get_crop_growth(farm_info[i].type);
								print(FARM_COLORS, "Days left: %d.
", days_left);
							} else {
								changed = true;
								farm_info[i].planting = false;
								print(FARM_COLORS, "No crop.
");
							}
						} else {
							changed = true;
							farm_info[i].planting = false;
							print(FARM_COLORS, "Harvested.
");
						}
						if (days > harvest_days) {
							print(FARM_COLORS, "Planting.
");
						} else if (days >= 0 && days < harvest_days) {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "No harvest.
");
						} else {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "Harvested.
");
						}
						int days_left = harvest_days - days;
						if (days_left > 0) {
							print(FARM_COLORS, "Days left: %d.
", days_left);
						} else if (days_left == 0) {
							if (farm_info[i].type == FARM_TYPE_CROP && farm_info[i].harvest != -1) {
								changed = true;
								print(FARM_COLORS, "Harvesting.
");
								farm_info[i].planting = true;
								int days_left = get_crop_growth(farm_info[i].type);
								print(FARM_COLORS, "Days left: %d.
", days_left);
							} else {
								changed = true;
								farm_info[i].planting = false;
								print(FARM_COLORS, "No crop.
");
							}
						} else {
							changed = true;
							farm_info[i].planting = false;
							print(FARM_COLORS, "Harvested.
");
						}
						if (days > harvest_days) {
							print(FARM_COLORS, "Planting.
");
						} else if (days >= 0 && days < harvest_days) {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "No harvest.
");
						} else {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "Harvested.
");
						}
						int days_left = harvest_days - days;
						if (days_left > 0) {
							print(FARM_COLORS, "Days left: %d.
", days_left);
						} else if (days_left == 0) {
							if (farm_info[i].type == FARM_TYPE_CROP && farm_info[i].harvest != -1) {
								changed = true;
								print(FARM_COLORS, "Harvesting.
");
								farm_info[i].planting = true;
								int days_left = get_crop_growth(farm_info[i].type);
								print(FARM_COLORS, "Days left: %d.
", days_left);
							} else {
								changed = true;
								farm_info[i].planting = false;
								print(FARM_COLORS, "No crop.
");
							}
						} else {
							changed = true;
							farm_info[i].planting = false;
							print(FARM_COLORS, "Harvested.
");
						}
						if (days > harvest_days) {
							print(FARM_COLORS, "Planting.
");
						} else if (days >= 0 && days < harvest_days) {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "No harvest.
");
						} else {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "Harvested.
");
						}
						int days_left = harvest_days - days;
						if (days_left > 0) {
							print(FARM_COLORS, "Days left: %d.
", days_left);
						} else if (days_left == 0) {
							if (farm_info[i].type == FARM_TYPE_CROP && farm_info[i].harvest != -1) {
								changed = true;
								print(FARM_COLORS, "Harvesting.
");
								farm_info[i].planting = true;
								int days_left = get_crop_growth(farm_info[i].type);
								print(FARM_COLORS, "Days left: %d.
", days_left);
							} else {
								changed = true;
								farm_info[i].planting = false;
								print(FARM_COLORS, "No crop.
");
							}
						} else {
							changed = true;
							farm_info[i].planting = false;
							print(FARM_COLORS, "Harvested.
");
						}
						if (days > harvest_days) {
							print(FARM_COLORS, "Planting.
");
						} else if (days >= 0 && days < harvest_days) {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "No harvest.
");
						} else {
							changed = true;
							farm_info[i].harvest = -1;
							print(FARM_COLORS, "Harvested.
");
						}
						if (days == harvest_days) {
							changed = true;
							farm_info[i].harvest = 0;
							print(FARM_COLORS, "Harvested.
");
						}
						if (changed) {
							print("Farm info updated.
");
						}
					} else if (farm_info[i].type == FARM_TYPE_LIVESTOCK) {
						if (farm_info[i].planting) {
							print(FARM_COLORS, "Planting.
");
						} else if (farm_info[i].harvest) {
							print(FARM_COLORS, "Harvested.
");
						} else {
							print(FARM_COLORS, "No crop.
");
						}
					} else if (farm_info[i].type == FARM_TYPE_ANIMALS) {
						if (farm_info[i].planting) {
							print(FARM_COLORS, "Planting.
");
						} else if (farm_info[i].harvest) {
							print(FARM_COLORS, "Harvested.
");
						} else {
							print(FARM_COLORS, "No crop.
");
						}
					} else if (farm_info[i].type == FARM_TYPE_VINEYARD) {
						if (farm_info[i].planting) {
							print(FARM_COLORS, "Planting.
");
						} else if (farm_info[i].harvest) {
							print(FARM_COLORS, "Harvested.
");
						} else {
							print(FARM_COLORS, "No crop.
");
						}
					} else if (farm_info[i].type == FARM_TYPE_ORCHARD) {
						if (farm_info[i].planting) {
							print(FARM_COLORS, "Planting.
");
						} else if (farm_info[i].harvest) {
							print(FARM_COLORS, "Harvested.
");
						} else {
							print(FARM_COLORS, "No crop.
");
						}
					} else if (farm_info[i].type == FARM_TYPE_GARDEN) {
						if (farm_info[i].planting) {
							print(FARM_COLORS, "Planting.
");
						} else if (farm_info[i].harvest) {
							print(FARM_COLORS, "Harvested.
");
						} else {
							print(FARM_COLORS, "No crop.
");
						}
					}
				}
			}
		} else {
			print("Invalid farm ID.");
		}
	}
}